I am currently creating a class to execute command remotely on a linux machine using ssh(paramiko). The following is the code I am using
def connect(self):
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(self.ip, port=self.port, username=self.user,password=self.password,timeout=self.timeout)
time.sleep(10)
return ssh
def runCommands(self,commands):
ssh=self.connect()
channel = ssh.invoke_shell()
time.sleep(10)
for command in commands:
wait=0
while channel.send_ready()!=True:
wait+=1
time.sleep(self.timeout)
if wait==5:
return 0
channel.send(command+'\n')
channel.send("exit\n")
return 1
My question here is if the command run into an error for example if I use 'mkdir a': "file exist error" is encountered, How can I handle it. I tried using channel.recv(buff_size) but the problem here is I could not differentiate between error and normal messages.
Thanks in advance
Copyright Notice:Content Author:「user3527110」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/36934413/how-can-i-handle-error-while-executing-remote-command-in-linux