Python Expect模块 - Learning Correcting Improving - ITeye技术网站
Python中有一个pExpect
模块,和TCL中的expect命令差不多,通过expect可以使你的脚本和shell命令之间进行交互。下面python脚本,使用了expect与ssh和scp交互来完成任务。
- #!/usr/bin/python
- import pexpect
- import sys
- import time
- import os
- now = time.strftime("%m%d%y_%I%M%S%p", time.localtime())
- if sys.argv[1] == '-c':
- foo = pexpect.spawn('scp -r %s user@address.org:/home/user/' % sys.argv[2])
- foo.expect('.ssword:*')
- foo.sendline('password')
- foo.interact()
- elif sys.argv[1] == '-b':
- os.mkdir("/home/user/BKUP/foo.com%s" % now, 0700)
- foo = pexpect.spawn('scp -r user@foo.org:/RemoteBox/user/%s /LocalBox/user/Bup/foo%s/' % (sys.argv[2], TimeStamp))
- foo.expect('.*ssword:')
- foo.sendline('Passwd_to_server')
- foo.interact()
- elif sys.argv[1] == '-p':
- foo = pexpect.spawn('ssh foo@bar.com')
- foo.expect('.*')
- foo.sendline('password')
- foo.interact()
- else:
- foo = pexpect.spawn('ssh default@default.org')
- foo.expect('.*ssword:')
- foo.sendline('password')
- foo.interact()
发表评论
评论