How can I handle error while executing remote command in linux
NickName:user3527110 Ask DateTime:2016-04-29T17:19:25

How can I handle error while executing remote command in linux

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

More about “How can I handle error while executing remote command in linux” related questions

How can I handle error while executing remote command in linux

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.

Show Detail

Error executing Linux command using tcl

I have the following TCL script which executes a Linux command to format a file. exec sed -r '2,$s/(.{55} )/\1\n\t/g' $formatfileName | sed 's/ $//' > $formatfileName I get an error saying can't ...

Show Detail

Paramiko exec_command() not executing command on remote linux server

I am working on a Python 3.7 script to connect and execute commands on the remote Linux server. I have used paramiko and ssh2.session libraries, the script was able to connect to the remote serve...

Show Detail

Error while executing linux command using Java

I'm trying to execute the following command using Java Process acquirInterfaces = Runtime.getRuntime().exec("dumpcap -D"); and getting error as java.io.IOException: Cannot run program "dumpcap":...

Show Detail

getting command not found error while executing a command using subprocess in cron

While executing a command through subprocess in the linux command line it is working but while executing through cron it is raising an error command not found. Why is this happening? Code: import

Show Detail

Run linux command in remote machine from java

I´m working with application in java. I can execute linux command (bash) on my machine host, but i want to execute this command in a remote machine like ssh. I´m ussing this code Process process =

Show Detail

Executing SSH with the Linux/Unix at command

I place this ssh call in the following a shell script on our Linux box named "tstz" and then call it with the linux "at" command in order to schedule it for later execution. tstz script: #! /bin...

Show Detail

Connecting to a remote linux machine from windows using SSH give permission denied error

So I am here new to ssh. I tried making a localhost connection on my Windows 10 device and it worked but I want to connect to a remote Linux machine.For this I am executing a statement for ssh on t...

Show Detail

Linux executes a command on a remote Windows server without SSH/Telnet

I am building a Jenkins server. One of the deployment steps is to execute a command on the production server to download the UAT-tested artifacts to the required folders. Jenkins is running on CentOS

Show Detail

PowerShell script executing batch script on remote server

I am executing a PowerShell script which executes a batch script at remote server. But in PowerShell script I am not able to handle any failure that could occur in batch script. The batch script is

Show Detail