FileNotFoundError: [WinError 2] python 3.4 [英] FileNotFoundError: [WinError 2] python 3.4
问题描述
我正在尝试通过 python 通过 Plink 自动接受 ssh-rsa 密钥.但我收到错误
下面是我的代码
def __init__(self, ipaddress, option, user, password, command=""):"""构造函数创建与主机的连接."""self.ipaddress = ipaddressself.option = 选项self.user = 用户self.password = 密码self.command = 命令self.key()定义键(自己):命令1 = ['回声','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):"""建立一个设备的 ssh 连接,发送命令,关闭连接并返回 stdout,stderr 元组."""尝试:打印(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() 函数错误.以下是错误:
文件C:\Python34\lib\subprocess.py",第 859 行,在 __init__ 中错误:restore_signals、start_new_session)错误:文件C:\Python34\lib\subprocess.py",第 1112 行,在 _execute_child错误:启动信息)错误:FileNotFoundError:[WinError 2] 系统找不到指定的文件
在 Windows 中,要在子进程中使用 echo
,您需要使用 shell=True
.这是因为 echo 不是单独的可执行文件,而是 Windows 命令行的内置命令.示例 -
process1 = subprocess.Popen(command1,stdout=subprocess.PIPE,shell=True)
另外,请注意,您应该只在绝对必要时使用 shell=True
(在这种情况下,在子进程的窗口中使用 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)输出,_ = process.communicate(b'y')
I am trying to auto accept ssh-rsa key through Plink via python. But I am getting error
Below is my code
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)
But i am getting error for Key() function for the line process1. Below is the error :
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
In Windows , to use echo
in subprocess, you would need to use shell=True
. This is because echo is not a separate executable, but rather a built-in command for the windows command line. Example -
process1 = subprocess.Popen(command1,stdout=subprocess.PIPE,shell=True)
Also, please do note, you should only use shell=True
when absolutely necessary (as in this case , to use echo
in windows in subprocess).
Though as a whole, you can directly pass in y
to your second command using PIPE
and .communicate()
. Example -
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')
这篇关于FileNotFoundError: [WinError 2] python 3.4的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!