Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ssh/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python pexpect不按步骤执行命令_Python_Ssh_Pexpect - Fatal编程技术网

Python pexpect不按步骤执行命令

Python 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

我有一个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
    变量由命令行参数定义,并打印结果
运行完整个代码后,Python将输出

b'\r\nbash: line 0: cd: srakrnSRV: No such file or directory\r\n'
这是不期望的,因为目录应该是先前创建的

此外,我还尝试在远程主机中手动创建
srakrnSRV
文件夹。再次运行命令后,
scp
功能似乎也没有运行。唯一运行的pexpect命令是最后一个
ssh
函数


如何使其有序执行?提前谢谢

您可能缺乏通过ssh执行命令的权限。还有一种可能是您的程序在提示出现之前发送scp。

您可能没有通过ssh执行命令的权限。还有一种可能性是,您的程序在提示符出现之前发送scp