Using Plink (PuTTy) for SSH via Python

I am trying to write a python script that will be SSH on the server and execute a command. I use Python 2.6 for Windows and installed plink and paegent (for ssh keys) and added them all to my path.

If I go to the command line and type:

plink username@host -i key.ppk open vnc://www.example.com/ 

I see the desired behavior - the VNC viewer opens on my Mac (server).

However, if I tried two approaches to do this programmatically through Python, and none of them work:

Approach 1 (os):

 import os ff=os.popen("plink user@host -i key.ppk",'w') print >>ff, r"open vnc://www.example.com" ff.flush() 

Approach 2 (subprocess):

 import subprocess ff=subprocess.Popen("plink user@host -i key.ppk",shell=False,stdin=subprocess.PIPE) ff.stdin.write(r"open vnc://www.example.com") ff.stdin.flush() 

None of the approaches generate an error, but open a VNC window. However, I believe that they both successfully connect to the remote host.

What am I doing wrong?

+8
python subprocess putty ssh plink
source share
3 answers

In the second approach, use

 ff.communicate("open vnc://www.example.com\n") 
+7
source share

I use a matrix to automate command execution via SSH on a remote PC.

0
source share

I would try:

 Popen("plink user@host -i key.ppk", shell=True) Popen("open vnc://www.example.com", shell=True) 
-one
source share

All Articles