[Tutor] Remote access from Windows PC to a Linux box
Mike Baker
mibaker88 at gmail.com
Tue Mar 30 18:29:00 CEST 2010
Hi,
I'm trying to connect to a Linux box from my Windows machine and execute a
series of commands - (ls, pwd, cat 'somefile', etc...). I'm using Putty to
do the ssh and have set up with Putty's Pagent agent to allow me to enter a
passphrase once per session to handle security keys between the two boxes
(so, no passwords needed for my remote scripts).
I have code that will give me a remote prompt on the Linux machine where I
can manually enter commands. This works great, but I want a script to always
execute the same series of commands without having to do so manually. I
also have code that will execute a single command like cat a file and write
the ouput to a new file. However, when I try to use the communicate object
in subprocess, my window hangs.
Here is my working code:
# module name data_collect.y
#
import subprocess
def simp_tst0(s_name):
# Opens a remote connection to "s_name and gives a prompt.
# Works great for executing linux commands.
# Does not exit gracefully when you type exit. The python
# prompt hangs when it gets to the r.communicate command
#
cmmnd_0="C:\\Progra~1\\putty\\plink %s" % s_name
r = subprocess.Popen("%s" % cmmnd_0,shell=False)
(r_stdout, r_stderr) = r.communicate("dir")
#status=r.poll() #Locks up if you try to poll here
print r_stdout
return r
def cat_remote(s_name, file2cat):
# This simple test file opens a remote connection to "s_name", does a
cat on
# file "file2cat" and writes the cat to an output file (out2.txt).
cmmnd_2="C:\\Progra~1\\putty\\plink %s cat %s" % (s_name, file2cat)
q = subprocess.Popen("%s" % cmmnd_2, stdout=open('out2.txt','w'))
def simp_tst3(s_name):
# Runs the initial subprocess.Popen command - creates proc.
# Hangs when you try to use proc.communicate
proc = subprocess.Popen(['C:\\Progra~1\\putty\\plink','Sula'],
shell=True,
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
)
#Either of the next two commands cause window to hang
#proc.stdin.write("dir")
#(stdout_value, stderr_value) = proc.communicate(input="dir")[0]
return proc
Thanks,
Mike
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20100330/694e6e1c/attachment.html>
More information about the Tutor
mailing list