Using Plink (PuTTy) to SSH through Python - python

I am trying to write a python script that will SSH to a server and execute a command. I am using Python 2.6 on Windows, and have installed plink and paegent (for ssh keys) and added them all to my path.
If I go to the command prompt and type:
plink username#host -i key.ppk
open vnc://www.example.com/
I see the desired behavior-- a VNC viewer opens on my Mac (server).
However, if I have tried two approaches to do this programmatically through Python and neither is working:
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()
Neither approach produces an error, but neither opens the VNC window. However, I believe they both successfully connect to the remote host.
What am I doing wrong?

In the second approach, use
ff.communicate("open vnc://www.example.com\n")

I use fabric for automation of runnning commands via SSH on a remote PC.

I'd try :
Popen("plink user#host -i key.ppk", shell=True)
Popen("open vnc://www.example.com", shell=True)

Related

Running command using Plink through Python and serial port?

I can open a new cmd window and connect with plink and serial port through Python.
import os
import subprocess
os.system("start cmd /k plink.exe -serial COM4 -sercfg 115200,8,n,1,N")
Still good here, but when I want to run ifconfig, it didn't work.
os.system("ifconfig")
First, consider using native Python serial connection implementation, like pySerial, instead of running a console application (plink).
See Full examples of using pySerial package.
Anyway if you run a Windows batch file that does what your Python code, it won't do what you want either:
start cmd /k plink.exe -serial COM4 -sercfg 115200,8,n,1,N
ifconfig
The ifconfig is not a top-level command. It's something that needs to be executed by Plink.
You have to feed the command to the plink standard input, see:
Execute a command on device over serial connection with Plink

How to communicate via SSH connection and plink in Python? [duplicate]

I am trying to write a python script that will SSH to a server and execute a command. I am using Python 2.6 on Windows, and have installed plink and paegent (for ssh keys) and added them all to my path.
If I go to the command prompt and type:
plink username#host -i key.ppk
open vnc://www.example.com/
I see the desired behavior-- a VNC viewer opens on my Mac (server).
However, if I have tried two approaches to do this programmatically through Python and neither is working:
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()
Neither approach produces an error, but neither opens the VNC window. However, I believe they both successfully connect to the remote host.
What am I doing wrong?
In the second approach, use
ff.communicate("open vnc://www.example.com\n")
I use fabric for automation of runnning commands via SSH on a remote PC.
I'd try :
Popen("plink user#host -i key.ppk", shell=True)
Popen("open vnc://www.example.com", shell=True)

Executing a python script on multpile nodes by using ssh command in BASH

keygen etc. so I can just ssh remotehost w/o using password, shell =BASH
I am using a for loop to ssh over multiple nodes (remote host) and wish to execute a script but it dosent seem to work
for i in {1..10};
do
ssh -f node$i "python script.py $i"
done
the terminal script hangs up and nothing happens
Also I can manually ssh and use python. The PYTHONPATH etc are configured for enodes.
There was cshell on nodes, so i used .cshrc wit exec /bin/bash which atleast when i log manually gives me bash shell, so problem doesent seem to be there.
I
Instead of wrapping python script in a shell script, you should probably have a python script that connects to all the remote hosts via ssh and executes stuff.
Paramiko is a very good framework for this kind of use-case. It will be much easier to maintain your script this was in the long run.
Use -o BatchMode=yes
and maybe you need to force allocate pseudo-tty and -n to prevent reading from stdin

Error when opening interactive ssh shell from python

I am trying to open an interactive ssh shell through fabric.
Requirements:
Use fabrics hosts in the connection string to remote
Open fully interactive shell in current terminal
Works on osx and ubuntu
No need for data transfer between fabric/python and remote. So fabric task can end in background.
So far:
fabfile.py:
def test_ssh():
from subprocess import Popen
Popen('ssh user#1.2.3.4 -i "bla.pem"', shell=True)
In terminal:
localprompt$ fab test_ssh
localprompt$ tcsetattr: Input/output error
[remote ubuntu welcome here]
remoteprompt$ |
Then if I try to input a command on the remote prompt it is executed locally and I drop back to the local prompt.
Does anyone know a solution?
Note: I am aware of fabrics open_shell, but this does not work for me since the stdout lags behind, rendering this unusable.
A slight modification does the trick:
def test_ssh():
from subprocess import call
call('ssh user#1.2.3.4 -i "bla.pem"', shell=True)
As the answer to this question suggests the error suggests the error comes from the inability of ssh to connect to the stdin/out of a process in the background.
With call the fabric task does not end in the background, but I am fine with that as long as as it is not interfering with my stdin/out.

run ssh command remotely without redirecting output

I want to run a python script on my server (that python script has GUI). But I want to start it from ssh. Something like this:
ssh me#server -i my_key "nohup python script.py"
... > let the script run forever
BUT it complains "unable to access video driver" since it is trying to use my ssh terminal as output.
Can I somehow make my commands output run on server machine and not to my terminal... Basically something like "wake-on-lan functionality" -> tell the server you want something and he will do everything using its own system (not sending any output back)
What about
ssh me#server -i my_key "nohup python script.py >/dev/null 2>&1"
You can use redirection to some remote logfile instead of /dev/null of course.
? :)
EDIT: GUI applications on X usually use $DISPLAY variable to know where they should be displayed. Moreover, X11 display servers use authorization to permit or disallow applications connecting to its display. Commands
export DISPLAY=:0 && xhost +
may be helpful for you.
Isn't it possible for you to rather use python ssh extension instead of calling external application?
It would:
run as one process
guarantee that invocation will be the same among all possible system
lose the overhead from "execution"
send everything trough ssh (you won't have to worry about input like "; possibly local executed command)
If not, go with what Piotr Wades suggested.

Categories