这是一个非常基本的示例,说明如何使用pexpect:
import sysimport pexpectimport getpasspassword = getpass.getpass("Enter password:")child = pexpect.spawn('ssh -l root 10.x.x.x "ls /"')i = child.expect([pexpect.TIMEOUT, "password:"])if i == 0: print("Got unexpected output: %s %s" % (child.before, child.after)) sys.exit()else: child.sendline(password)print(child.read())
输出:
Enter password:binbootdevetcexporthomeinitrd.imginitrd.img.oldliblib64lost+foundmediamntoptprocrootrunsbinselinuxsrvsystmpusrvarvmlinuzvmlinuz.old
还有更详细的例子在这里。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)