LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   python2.x : automating ssh with subporcess call (https://www.linuxquestions.org/questions/programming-9/python2-x-automating-ssh-with-subporcess-call-4175523841/)

pix9 10-30-2014 06:52 PM

python2.x : automating ssh with subporcess call
 
Hi Friends,
I am new to python, kind of migrating from bash to python. here is thing I am trying to do with my pythong script.

ssh system and get some info from that system. I want it all automated. here is where I am strucked
script is pormpting me for ssh password

given below is my script
OS : fedora20
sshd daemon is running.
Code:

#!/usr/bin/python
from subprocess import Popen, PIPE
from sys import argv
script, ip = argv
username="test"
passwd="password"
command = "ssh -l %r %r 'echo hello >> /tmp/test.txt'" % (username,ip)

proc = Popen([command], stdin=PIPE,stdout=PIPE,stderr=PIPE,shell=True)

#proc.communicate(passwd)
proc.communicate("password\n")

How I execute script
Code:

$python ./python_ssh.py 192.168.1.20
test@192.168.1.20's password:

when I run above script it prompts me for password. Kindly suggest me where I am doing wrong, if any good e-book or link for learning subprocess is also welcome.

regards

evo2 10-30-2014 07:58 PM

Hi,

I think your approach is less than ideal. You should not be treating passwords like that. Look into using RSA authentication. Also, using Popen or similar should be a last resort - use existing libraries. See for example https://wiki.python.org/moin/SecureShell

Evo2.


All times are GMT -5. The time now is 08:55 AM.