繁体   English   中英

FileNotFoundError:[WinError 2] python 3.4

[英]FileNotFoundError: [WinError 2] python 3.4

我试图通过python通过Plink自动接受ssh-rsa密钥。 但是我得到了错误

下面是我的代码

def __init__(self, ipaddress, option, user, password, command=""):
    """
    Constructor creates the connection to the host.
    """
    self.ipaddress = ipaddress
    self.option = option
    self.user = user
    self.password = password
    self.command = command
    self.key()

def key(self):
    command1 = ['echo', 'y']
    process1 = subprocess.Popen(command1,stdout=subprocess.PIPE)
    command2 = ['plink', '-ssh', self.option, '-pw', self.password, '%s@%s'%(self.user, self.ipaddress), '\"exit\"']
    process2 = subprocess.Popen(command2,stdin=process1.stdout,stdout=subprocess.PIPE)

def sendSingleCommand(self, command):
    """
    Set up a ssh connection a device, send command, close connection and return stdout,stderr tuple.
    """
    try:
        print("plink -ssh %s -pw %s %s@%s %s" \
                % (self.option, self.password, self.user, self.ipaddress, command))
        self.process = subprocess.Popen("plink -ssh %s -pw %s %s@%s %s" \
                % (self.option, self.password, self.user, self.ipaddress, command), shell=False, stdin=subprocess.PIPE, stdout=subprocess.PIPE)

但是我在行process1的Key()函数中遇到错误。 下面是错误:

File "C:\Python34\lib\subprocess.py", line 859, in __init__  
Error:     restore_signals, start_new_session)  
Error:   File "C:\Python34\lib\subprocess.py", line 1112, in _execute_child  
Error:     startupinfo)  
Error: FileNotFoundError: [WinError 2] The system cannot find the file specified  

在Windows中,要在子进程中使用echo ,您将需要使用shell=True 这是因为echo不是独立的可执行文件,而是Windows命令行的内置命令。 范例-

process1 = subprocess.Popen(command1,stdout=subprocess.PIPE,shell=True)

另外,请注意,仅在绝对必要时才应使用shell=True (在这种情况下,请在子进程的Windows中使用echo )。


虽然总的来说,您可以使用PIPE.communicate()y直接传递给第二个命令。 范例-

def key(self):
    command2 = ['plink', '-ssh', self.option, '-pw', self.password, '%s@%s'%(self.user, self.ipaddress), '\"exit\"']
    process2 = subprocess.Popen(command2,stdin=subprocess.PIPE,stdout=subprocess.PIPE)
    output, _ = process.communicate(b'y')

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM