Skip to content Skip to sidebar Skip to footer

Run Multiple Commands In A Single Ssh Session Using Popen And Save The Output In Separate Files

I am trying to run several commands in a single ssh session and save the output of each command in a different file. The code that works (but it saves all the output in a single f

Solution 1:

A single ssh session runs a single command e.g., /bin/bash on the remote host -- you can pass input to that command to emulate running multiple commands in a single ssh session.

Your code won't run even a single command. There are multiple issues in it:

  1. ssh may read the password directly from the terminal (not its stdin stream). conn.communicate('password') in your code writes to ssh's stdin therefore ssh won't get the password.

    There are multiple ways to authenticate via ssh e.g., use ssh keys (passwordless login).

  2. stdout = output.append('a') doesn't redirect ssh's stdout because .append list method returns None.

    It won't help you to save output of several commands to different files. You could redirect the output to remote files and copy them back later: ls >ls.out; uname -a >uname.out; pwd >pwd.out.

    A (hacky) alternative is to use inside stream markers (echo <GUID>) to differentiate the output from different commands. If the output can be unlimited; learn how to read subprocess' output incrementally (without calling .communicate() method).

  3. for i in cmd: open(i,'w') is pointless. It opens (and immediately closes on CPython) multiple files without using them.

    To avoid such basic mistakes, write several Python scripts that operate on local files.

Solution 2:

SYS_STATS={"Number of CPU Cores":"cat /proc/cpuinfo|grep -c 'processor'\n",
               "CPU MHz":"cat /proc/cpuinfo|grep 'cpu MHz'|head -1|awk -F':' '{print $2}'\n",
               "Memory Total":"cat /proc/meminfo|grep 'MemTotal'|awk -F':' '{print $2}'|sed 's/ //g'|grep -o '[0-9]*'\n",
               "Swap Total":"cat /proc/meminfo|grep 'SwapTotal'|awk -F':' '{print $2}'|sed 's/ //g'|grep -o '[0-9]*'\n"}


def get_system_details(self,ipaddress,user,key):
        _OutPut={}
        values=[]
        sshProcess = subprocess.Popen(['ssh','-T','-o StrictHostKeyChecking=no','-i','%s' % key,'%s@%s'%(user,ipaddress),"sudo","su"],
                                      stdin=subprocess.PIPE, stdout = subprocess.PIPE, universal_newlines=True,bufsize=0)

        for element inself.SYS_STATS1:
            sshProcess.stdin.write("echo END\n")
            sshProcess.stdin.write(element)

        sshProcess.stdin.close()
        for element in sshProcess.stdout:
            if element.rstrip('\n')!="END":
                values.append(element.rstrip('\n'))
        mapObj={k: v for k, v in zip(self.SYS_STATS_KEYS, values)}

        return mapObj

Post a Comment for "Run Multiple Commands In A Single Ssh Session Using Popen And Save The Output In Separate Files"