Python Expect模块

xiaoxiao2024-10-26  15

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() 相关资源:敏捷开发V1.0.pptx
转载请注明原文地址: https://www.6miu.com/read-5018589.html

最新回复(0)