一、安装expect
yum install -y expect
二、python编写定时任务脚本
def remote_put(local_path, remote_path, pwd, port = None, limit = None, timeout = -1):
"""
port:远程ssh端口,默认ssh端口 22
limit:限速(kb), 默认不限速
pwd:远程机器密码,
timeout:超时时间(s),默认 -1 永不超时
"""
scp_put = 'set timeout %d\nspawn scp ' % timeout
if port is not None: scp_put += '-P %d ' % port
if limit is not None: scp_put += '-l %d ' % (limit * 8)
scp_put += local_path + ' ' + remote_path + '\n'
scp_put += 'expect "(yes/no)?" {\n' \
'send "yes\r"\n' \
'expect "password:"\n' \
'send "%s\r"\n' \
'} "password:" {send "%s\r"}\n' \
'expect eof\n' \
'exit'
os.system("echo '%s' > scp_put.cmd" % (scp_put % (pwd, pwd)))
os.system('expect scp_put.cmd')
os.system('rm scp_put.cmd')