1
我有這個使用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.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>
命令。變量filename
由命令行參數定義,並將結果打印出來。
運行整個代碼後,Python的打印出
b'\r\nbash: line 0: cd: srakrnSRV: No such file or directory\r\n'
未預計,隨着目錄應預先創建的。
此外,我試圖在我的遠程主機上手動創建srakrnSRV
文件夾。再次運行該命令後,看起來scp
函數也沒有運行。唯一的運行pexpect coomand是最後的ssh
功能。
如何使它按順序執行?提前致謝!