Skip to content

Instantly share code, notes, and snippets.

@ayuLiao
Last active December 11, 2018 08:21
Show Gist options
  • Select an option

  • Save ayuLiao/0f1f785c12ec9498b26dd874a3698248 to your computer and use it in GitHub Desktop.

Select an option

Save ayuLiao/0f1f785c12ec9498b26dd874a3698248 to your computer and use it in GitHub Desktop.
scp因为安全考虑,每次使用前需要输入密码,但我希望脚本自动化时,可以自动输入密码 此时可以使用python的pexpect自动输入密码
'''
path 本地文件夹路径,scp使用了-r
user 服务器的用户名
passwd 服务器的密码
tpath 上传到服务器的路径
'''
def scp(path, user,ip, passwd, tpath):
passwd_key = '.*assword.*'
shell = 'scp -r {path} {user}@{ip}:{tpath}'.format(
path=path, user=user, ip=ip, tpath=tpath
)
try:
child = pexpect.spawn(shell)
child.expect(passwd_key)
child.sendline(passwd)
child.expect(pexpect.EOF)
print('SCP update success')
except:
print('SCP update faild')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment