python-pexpect模块的使用(ssh服务器 自动登录批量执行scp)

!/usr/bin/env python

import pexpect
import sys
ip=sys.argv[1]

dbname=sys.argv[2]

dbhost= sys.argv[3:]

child = pexpect.spawn ( ‘ssh -p 10022 -o “StrictHostKeyChecking no” uss@%s@%s’ %(ip)) #取消首次ssh公钥验证
child.expect(‘.*password:’,timeout=30)
child.sendline(‘**888’)
child.expect (‘$’)
child.sendline(‘sudo su -‘)
child.expect (‘.*for jhwu:’)
child.sendline (‘***8’)
child.expect (’ ~]#’)
child.sendline(‘scp -P 10022 -o “StrictHostKeyChecking no” [email protected]:/usr/lib64/ttt.so /root’)
child.expect(‘.*password:’,timeout=40)
child.sendline(‘***88’)
child.expect(‘#’)

child.expect(pexpect.EOF)

print child.before
child.sendline(‘exit’)

你可能感兴趣的:(python)