2

Here are the code snippets I'm testing in python3:

ssh = subprocess.Popen(["ssh", "%s" % HOST, COMMAND],
                   shell=False,
                   stdout=subprocess.PIPE,
                   stderr=subprocess.PIPE)

and

subprocess.Popen(['ssh', "me" + '@' + 123.45.67.891, '-p', "22"]

When I run it it asks for the password over and over again:

Password:
Password:
Password:

I tested connection and this works:

ssh [email protected] "uname -a"

NOTE: Trying to avoid installing more libraries for something simple

EDIT: Here is the entire code:

#!/usr/bin/python3
import subprocess
import sys

HOST="123.45.67.891"
# Ports are handled in ~/.ssh/config since we use OpenSSH
COMMAND="uname -a"

ssh = subprocess.run(["ssh", "123.45.67.891", "uname -a"],
                    shell=False,
                    stdout=subprocess.PIPE,
                    stderr=subprocess.PIPE,
                    check=False)

result = ssh.stdout.readlines()
if result == []:
 error = ssh.stderr.readlines()
 print >>sys.stderr, "ERROR: %s" % error
else:
 print (result)

EDIT2: When I cntl-C I get this:

Password: Traceback (most recent call last):
File "./sshtest2.py", line 13, in <module>
check=False)
File "/usr/lib/python3.6/subprocess.py", line 405, in run
stdout, stderr = process.communicate(input, timeout=timeout)
File "/usr/lib/python3.6/subprocess.py", line 843, in communicate
stdout, stderr = self._communicate(input, endtime, timeout)
File "/usr/lib/python3.6/subprocess.py", line 1514, in _communicate
ready = selector.select(timeout)
File "/usr/lib/python3.6/selectors.py", line 376, in select
fd_event_list = self._poll.poll(timeout)
KeyboardInterrupt
7
  • You might want to specify the IP as a string aswell.... Also, why do you think the connection fails if it asks for a password? What is really the problem here?
    – Panki
    Commented Jul 11, 2019 at 14:14
  • @Panki I put in the password 3 times and then it fails. It never executes a remote command.
    – Marinaio
    Commented Jul 11, 2019 at 14:27
  • You'll need to include where your variables (HOST, COMMAND) are set. Also, you are not connecting anything to stdin. Or does Popen do that automatically?
    – Panki
    Commented Jul 11, 2019 at 14:29
  • @Panki I posted one of my tests. I hard coded IP just to test this and the connection is made, but never passes the password phase.
    – Marinaio
    Commented Jul 11, 2019 at 14:36
  • uname -a should most likely be 2 arguments. Try it like this: ssh = subprocess.run(["ssh", "123.45.67.891", "uname", "-a"]. Also, how do you provide the password to subprocess.run?
    – Panki
    Commented Jul 11, 2019 at 14:42

0

You must log in to answer this question.

Browse other questions tagged .