Opening Shell script in Python

0

I'm automating tasks and I'm new to Python, so I'd like your help because I need to run a Shell script on another machine and this command I need to give inside a Python code.

I thought about using something like:

 #!/usr/bin/python

 # -*- coding: utf8 -*- 
 import subprocess 

 subprocess.call('ssh user@host', shell=True)

But this is the reason why I do not know how to implement Login and Password (as well as "yes" of encryption).

    
asked by anonymous 01.08.2017 / 07:37

1 answer

0

I imagine you want to make an SSH connection via python. If so, the pxssh module does exactly what you want. For example, to run 'ls -l' and to print output, you need to do something like this:

import pxssh
s = pxssh.pxssh()
if not s.login ('localhost', 'myusername', 'mypassword'):
    print "SSH falha no login."
    print str(s)
else:
    print "SSH sucesso no login"
    s.sendline ('ls -l')
    s.prompt()         
    print s.before     
    s.logout()

Some useful links:

link link

    
01.08.2017 / 19:11