ssh using sshpass in python seems to not work
Question: I have a python script which is supposed to ssh in to a client and execute a
bash from the client. As a test scenario I am using just 1 machine to connect
but the objective is to connect to several clients and execute bash scripts
from those machines.

My Python code:

    
    
     import os 
     import subprocess
     import time
    
    
    def ssh_login_execute():
        if device['PWD'] != "":
                run=('sshpass -p %s ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -t -p %s %s@%s' % (device['PWD'], device['PORT'], device['USER'], device['IP']))
        else:
                run=('ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -t -p %s %s@%s' % (device['PORT'], device['USER'], device['IP']))
    
        cmd = ('cd %s' % (script_path))
    
        run2=run.split()
        run2.append(cmd)
        t=subprocess.Popen(run2, stdout=subprocess.PIPE, stderr=open(os.devnull, 'w'))
        print "I am in 192.168.1.97"
        execute_tg()
        return t
    
    def execute_tg():
       path = "/home/"
       os.chdir(path)
       print os.getcwd()
       cmd=("sh my_script.sh")
       t=subprocess.Popen(cmd.split(), stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
    
    if __name__ == "__main__":
        device = {}
        device['PORT']=22
        device['PWD']= "abcd"
        device['USER']= "root"
        device['IP']= "192.168.1.97"
        script_path= "/home/"
        ssh_login_execute()
    

On running the code "python script.py", I see output as:

    
    
      I am in 192.168.1.97
      /home/
      Output is sh: 0: Can't open my_script.sh
    

Although the "my_script.sh" is in /home directory in 192.168.1.97. How do I
get rid of this issue and at the same time make it scalable to ssh to multiple
clients and execute bash.


Answer: Your script `my_script.sh` is probably not in `/home/` as expected in the
code.

    
    
       path = "/home/"
       os.chdir(path)
       print os.getcwd()
       cmd=("sh my_script.sh")
    

Also it should print the current directory as well with `print os.getcwd()`.
You should change those values based on the real location of your script.

