2

I HAVE to use SUBPROCESS for SSH 'cuz can't change from python 3.1(which unfortunately have virtually NO support for ssh libraries) due to project restrictions.

I have installed openSSH on both local and remote machine running over windows.

Problem: I am able to attempt ssh but NOT able to enter password and subsequent command e.g. dir and fetch result.

proc = subprocess.Popen("ssh -t -t remote_ip_here\n", shell = False, stdin = subprocess.PIPE, stdout=subprocess.PIPE)
#Entering password
proc.stdin.write(bytes("abc123\n","utf-8"))
#entering command
self.text , self.error = proc.communicate() // here I tried readlines() but it didnt returned any response.
print(self.text.decode("utf-8"))
proc.stdin.write(bytes("dir\n","utf-8"))

Any clue?

Chirag Dhyani
  • 863
  • 11
  • 24
  • 1
    I would recommend [pexpect](http://www.noah.org/python/pexpect/) for such scenarios – gmfreak Aug 06 '14 at 09:58
  • @Chirag And why not just `ssh remote_address remote_cmd` and add a SSH keypair? – Salem Aug 06 '14 at 10:20
  • thx gmfreak, I am trying pexpect. – Chirag Dhyani Aug 06 '14 at 10:38
  • @Salem: There is a requirement related to the password. – Chirag Dhyani Aug 06 '14 at 10:39
  • Appears like p-expect if for Linix and not for Windows. – Chirag Dhyani Aug 06 '14 at 10:56
  • 1. `ssh` asks the password directly from a terminal (outside of standard streams i.e., it is pointless to write to `p.stdin` -- ssh doesn't read password from it e.g., try [python script that uses `msvcrt.getch()` to read input](http://stackoverflow.com/a/20981435/4279), to understand how ssh works). See the first reason in [Q: Why not just use a pipe (popen())?](http://pexpect.readthedocs.org/en/latest/FAQ.html#whynotpipe) 2. If pexpect/winpexpect/pty -based solution doesn't work for you; you could try key-based authentication for ssh or `paramiko` (ssh library) that works on Python 3. – jfs Aug 06 '14 at 13:29

1 Answers1

2

Sending a password over SSH or SCP with subprocess.Popen

As you want to use windows you should look at a pexpect port: https://bitbucket.org/geertj/winpexpect/wiki/Home

Community
  • 1
  • 1
Rhand
  • 901
  • 7
  • 20