系统批量运维管理器pexpect的使用
# pip install pexpect 或 # easy_install pexpect
1 #!/usr/bin/env python
2 import pexpect
3 child = pexpect.spawn('/usr/bin/scp /root/pexpect/haha 172.16.65.201:/tmp/')
4 child.expect('password:')
5 child.sendline('rootroot')
6 child.expect(pexpect.EOF) #匹配结束语句
1 #!/usr/bin/env python 2 import pexpect 3 import sys 4 child = pexpect.spawn('ssh root@172.16.65.201') 5 fout = file('mylog.txt','w') 6 child.logfile = fout #输出写到日志文件 7 #child.logfile = sys.stdout #输出到标准输出 (好像只能同时二选一) 8 9 child.expect('password:') 10 child.sendline('rootroot') 11 child.expect('#') 12 child.sendline('ls /home') 13 child.expect('#') 14 #print "before:"+child.before 15 #print "after:"+child.after