淡淡回忆 发表于 2015-4-27 11:36:19

Python pexpect 模块

  Python Expect模块 - Learning Correcting Improving - ITeye技术网站

Python Expect模块

博客分类:
[*]Python

PythonSSHOSTcl脚本
  Python中有一个pExpect
模块,和TCL中的expect命令差不多,通过expect可以使你的脚本和shell命令之间进行交互。
  下面python脚本,使用了expect与ssh和scp交互来完成任务。
  



Python代码 http://scribefire-next/javascripts/syntaxhighlighter/clipboard_new.swf?clipboard=%23!%2Fusr%2Fbin%2Fpython%0A%0Aimport%20pexpect%0Aimport%20sys%0Aimport%20time%0Aimport%20os%0A%0Anow%20%3D%20time.strftime(%22%25m%25d%25y_%25I%25M%25S%25p%22%2C%20time.localtime())%0A%0Aif%20sys.argv%5B1%5D%20%3D%3D%20 http://scribefire-next/images/icon_star.pnghttp://scribefire-next/images/spinner.gif
[*]#!/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 == '-c':
[*]    foo = pexpect.spawn('scp -r %s user@address.org:/home/user/' % sys.argv)
[*]    foo.expect('.ssword:*')
[*]    foo.sendline('password')
[*]    foo.interact()
[*]
[*]elif sys.argv == '-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, TimeStamp))
[*]    foo.expect('.*ssword:')
[*]    foo.sendline('Passwd_to_server')
[*]    foo.interact()
[*]elif sys.argv == '-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()
#!/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 == '-c':
foo = pexpect.spawn('scp -r %s user@address.org:/home/user/' % sys.argv)
foo.expect('.ssword:*')
foo.sendline('password')
foo.interact()
elif sys.argv == '-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, TimeStamp))
foo.expect('.*ssword:')
foo.sendline('Passwd_to_server')
foo.interact()
elif sys.argv == '-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()
  


分享到:
http://scribefire-next/images/sina.jpg
http://scribefire-next/images/tec.jpg

JBoss目录结构说明
|
从GAE datastore联想到key-value database ...

[*]2009-04-06 13:52:25
[*]浏览 951
[*]评论(0)
[*]论坛回复 / 浏览 (0 / 3267)
[*]相关推荐

评论
  

发表评论
http://scribefire-next/images/login_icon.png 您还没有登录,
页: [1]
查看完整版本: Python pexpect 模块