Python pexpect不按步骤执行命令
我有一个Python3代码,它使用PexpectPython pexpect不按步骤执行命令,python,ssh,pexpect,Python,Ssh,Pexpect,我有一个Python3代码,它使用Pexpect import pexpect import getpass import sys def ssh(username,password,host,port,command,writeline): child = pexpect.spawn("ssh -p {} {}@{} '{}'".format(port,username,host,command)) child.expect("password: ") child.s
import pexpect
import getpass
import sys
def ssh(username,password,host,port,command,writeline):
child = pexpect.spawn("ssh -p {} {}@{} '{}'".format(port,username,host,command))
child.expect("password: ")
child.sendline(password)
if(writeline):
print(child.read())
def scp(username,password,host,port,file,dest):
child = pexpect.spawn("scp -P {} {} {}@{}:{}".format(port,file,username,host,dest))
child.expect("password: ")
child.sendline(password)
try:
filename = sys.argv[1]
print("=== sendhw remote commander ===")
username = input("Username: ")
password = getpass.getpass("Password: ")
ssh(username,password,"some.host.net","22","mkdir ~/srakrnSRV",False)
scp(username,password,"some.host.net","22",filename,"~/srakrnSRV")
ssh(username,password,"some.host.net","22","cd srakrnSRV && sendhw {}".format(filename),True)
except IndexError:
print("No homework name specified.")
我的目标是:
- 使用
功能将SSH导入主机,创建目录SSH
,然后srakrnSRV
- 将文件上载到先前创建的
目录中srakrnSRV
插入cd
,并执行srakrnSRV
命令。sendhw
变量由命令行参数定义,并打印结果filename
b'\r\nbash: line 0: cd: srakrnSRV: No such file or directory\r\n'
这是不期望的,因为目录应该是先前创建的
此外,我还尝试在远程主机中手动创建srakrnSRV
文件夹。再次运行命令后,scp
功能似乎也没有运行。唯一运行的pexpect命令是最后一个ssh
函数
如何使其有序执行?提前谢谢 您可能缺乏通过ssh执行命令的权限。还有一种可能是您的程序在提示出现之前发送scp。您可能没有通过ssh执行命令的权限。还有一种可能性是,您的程序在提示符出现之前发送scp