Execute Commands on Remote Computers over Network

D

dylpkls91

I have been researching this topic and come up with some code to make
it work. It uses SSL and requires the 3rd party package Paramiko (which
requires PyCrypto). However, at this moment I have no network to test
the code on! Trying to connect to localhost (127.0.0.1) fails. Before I
buy network components, I want to be sure that this code will work.
Will someone with two networked computers try it out for me? Thanks!
Code:

def remotely_execute_command(ipaddr, port, user, passwd=None, cmd):
import paramiko
transport = paramiko.Transport(ipaddr + ":" + port)
transport.connect(username=user, password=passwd)
channel = transport.open_session()
channel.exec_command(cmd)
print "Command " + cmd + " executed by user " + user + " with
password " + passwd + " at " + host + ":" + port
 
D

Dennis Lee Bieber

I have been researching this topic and come up with some code to make
it work. It uses SSL and requires the 3rd party package Paramiko (which
requires PyCrypto). However, at this moment I have no network to test
the code on! Trying to connect to localhost (127.0.0.1) fails. Before I
buy network components, I want to be sure that this code will work.

Are you sure you have something on localhost that is listening on
whatever port you attempted to talk to?

Where is the traceback of the failure...
print "Command " + cmd + " executed by user " + user + " with
password " + passwd + " at " + host + ":" + port

Ugh!

print "Command %s executed by user %s with password %s at %s:%s" %
(cmd, user, passwd, host, port)
--
Wulfraed Dennis Lee Bieber KD6MOG
(e-mail address removed) (e-mail address removed)
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff: (e-mail address removed))
HTTP://www.bestiaria.com/
 
L

Lawrence D'Oliveiro

"dylpkls91 said:
I have been researching this topic and come up with some code to make
it work. It uses SSL and requires the 3rd party package Paramiko (which
requires PyCrypto).

Why not just spawn an invocation of SSH?
 
D

dylpkls91

Lawrence said:
Why not just spawn an invocation of SSH?

@Dennis: yes, I know, but I wasn't thinking, and it doesn't solve the
problem.

@Lawrence: I'm sorry, but I'm a newbie. Can you explain what this
means, and how I could do it in Python? Thank you very much.
 
L

Lawrence D'Oliveiro

"dylpkls91 said:
Can you explain what this means, and how I could do it in Python?

SSH is the standard means of remotely executing commands on one *nix
system from another <http://www.openssh.com/>. It is included with all
self-respecting *nix systems these days. You can set up a trust
relationship between particular accounts on two systems, so one can
connect to the other without a password. All communication is encrypted
to lock out eavesdroppers.
 
S

Steve Holden

Lawrence said:
SSH is the standard means of remotely executing commands on one *nix
system from another <http://www.openssh.com/>. It is included with all
self-respecting *nix systems these days. You can set up a trust
relationship between particular accounts on two systems, so one can
connect to the other without a password. All communication is encrypted
to lock out eavesdroppers.

Also note that Cygwin gives you openssh in a Unix-shell-like
environment. Has its own Python too, though you don't do things in
Cygwin for performance, usually.

regards
Steve
 
D

dylpkls91

I'm sorry, I should have clarified. I want to execute commands on
networked computers running <i>Windows XP</i>. Thank you for you
concern anyway. Does this mean that the Paramiko module only works on
Unix systems?
 
B

Ben Sizer

dylpkls91 said:
I have been researching this topic and come up with some code to make
it work. It uses SSL and requires the 3rd party package Paramiko (which
requires PyCrypto). However, at this moment I have no network to test
the code on! Trying to connect to localhost (127.0.0.1) fails.

Where is the code that is supposed to receive this connection? There
has to be something running on the target PC to accept incoming
commands. Paramiko appears to use SSH, which (as far as I know)
connects to a computer running an sshd program or equivalent and
executes commands via that program. If there's no sshd running on the
target, you can't do anything.
 
D

dylpkls91

Ben said:
Paramiko appears to use SSH, which (as far as I know)
connects to a computer running an sshd program or equivalent and
executes commands via that program. If there's no sshd running on the
target, you can't do anything.

Thanks for the clarification. Can I get 'sshd' or an equivalent for
Windows?
 
D

dylpkls91

Thank you all for your responses.

I have managed to figure out a solution using XML RPC which fits my
needs perfectly.
 
G

Gerhard Fiedler

Thank you all for your responses.

I have managed to figure out a solution using XML RPC which fits my
needs perfectly.

BTW, if you want to test networking between two machines on one single
(Windows) computer, try VirtualPC. (Free from MS.)

Gerhard
 
C

costello.bob

dylpkls91 said:
Thank you all for your responses.

I have managed to figure out a solution using XML RPC which fits my
needs perfectly.

Mind posting it for us lesser beings? ;)
 
D

dylpkls91

Mind posting it for us lesser beings? ;)

Not at all. I have yet to test it on networked computers, but it works
fine when I run both scripts on my machine.

The hard part now is getting the server code to run as a Windows
service- argh!!!
I can get it installed and started using modified code from:
http://www.schooltool.org/products/...-a-windows-service/schooltool-service.py/view

but for some reason when the server is a service the client refuses to
connect properly!
Grrr... anybody know why?

Here is the code for the server, the machine that will execute the
commands:

import SimpleXMLRPCServer, os
server = SimpleXMLRPCServer.SimpleXMLRPCServer(("localhost", 8000))
server.register_function(lambda command: os.popen4(command)[1].read(),
"execute")
server.serve_forever()

And here's the code for the client, the computer that will tell the
server what command to execute:

import xmlrpclib
server = xmlrpclib.Server("http://" + raw_input("Enter the IP address
of the server: ") + ":8000")
output = server.execute(raw_input("Enter a command for the server to
execute: "))
print output
 
S

Steve Holden

dylpkls91 said:
Not at all. I have yet to test it on networked computers, but it works
fine when I run both scripts on my machine.

The hard part now is getting the server code to run as a Windows
service- argh!!!
I can get it installed and started using modified code from:
http://www.schooltool.org/products/...-a-windows-service/schooltool-service.py/view

but for some reason when the server is a service the client refuses to
connect properly!
Grrr... anybody know why?
Because there are specific requirements for Windows services, that your
program isn't comlpying with.
Here is the code for the server, the machine that will execute the
commands:

import SimpleXMLRPCServer, os
server = SimpleXMLRPCServer.SimpleXMLRPCServer(("localhost", 8000))
server.register_function(lambda command: os.popen4(command)[1].read(),
"execute")
server.serve_forever()

And here's the code for the client, the computer that will tell the
server what command to execute:

import xmlrpclib
server = xmlrpclib.Server("http://" + raw_input("Enter the IP address
of the server: ") + ":8000")
output = server.execute(raw_input("Enter a command for the server to
execute: "))
print output

I'm afraid you'll need to Google for "python windows service" or
similar: in essence the main thing the service has to do (besides serve)
is ensure a continuous flow of messages through the system. It's ugly,
but people have done it before.

regards
Steve
 
F

Frank Millman

Steve said:
Because there are specific requirements for Windows services, that your
program isn't comlpying with.


I'm afraid you'll need to Google for "python windows service" or
similar: in essence the main thing the service has to do (besides serve)
is ensure a continuous flow of messages through the system. It's ugly,
but people have done it before.

I use something called 'srvany' -
http://support.microsoft.com/kb/q137890/

I have a socket server program, written in pure Python, which I need to
run as a service. 'srvany' enables me to do this without using any of
the Win32 extensions or other complications.

I have not run it in a heavy-duty environment so I don't know if there
are any problems with it. I have it set up on my Windows 2003 Server,
and after a restart, my server program is automatically started as a
service and any client can connect straight away.

Frank Millman
 
L

Laszlo Nagy

I'm afraid you'll need to Google for "python windows service" or
similar: in essence the main thing the service has to do (besides serve)
is ensure a continuous flow of messages through the system. It's ugly,
but people have done it before.
Also don't forget to "Allow the service interact with the desktop" -
otherwise you will not be able to start GUI applications from the service.

Laszlo
 
D

dylpkls91

Frank said:

I am familiar with srvany. Seems like it is a whole bunch easier than
the PyWin32 stuff.

I'll write a wrapper script that can be used like this:
createservice.py -myservicename -myservicepath
or like this:
import createservice
createservice.install_if_needed("myservicename", "myservicepath")


I'll have my XMLRPC programcall createservice.py's install_if_needed
method.
There you have it: a foolproof, automatic, no-hassle service. Thanks
Frank!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top