Posted January 17
I need to have a script that automatically download a file via scp. The file to be downloaded has current date on it and the script must download the file for the current date everyday.
At first I was using expect, but it won't work on cron(since it needs tty and cron doesn't run on tty). So I ended up using python's pexpect
The code goes like this:
#!/usr/bin/env python
import pexpect
import time
from datetime import date
today = date.today();
REMOTE_FILE="/root/backup/backup-%s.sql.gz" % (today.isoformat())
LOCAL_FILE="/root/dump_backup/."
USER="root"
HOST="192.168.10.1"
PASS="thiisisthepassword"
CMD="scp %s@%s:%s %s" % (USER, HOST, REMOTE_FILE, LOCAL_FILE)
child = pexpect.spawn(CMD, timeout=None)
child.expect('password:')
child.sendline(PASS)
Hope it can help someone.
