gpt4 book ai didi

python - 文件未找到错误 : [WinError 2] python 3. 4

转载 作者:行者123 更新时间:2023-12-01 04:31:57 27 4
gpt4 key购买 nike

我正在尝试通过 python 通过 Plink 自动接受 ssh-rsa key 。但我收到错误

下面是我的代码

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 (如本例中,在子进程的窗口中使用 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')

关于python - 文件未找到错误 : [WinError 2] python 3. 4,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32268030/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com