Reputation: 13
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
Upvotes: 1
Views: 3485
Reputation: 90889
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')
Upvotes: 2