473,385 Members | 1,647 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Execute Commands on Remote Computers over Network

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

Jun 27 '06 #1
15 7500
In article <11**********************@c74g2000cwc.googlegroups .com>,
"dylpkls91" <dp******@pacbell.net> wrote:
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?
Jun 27 '06 #2

Lawrence D'Oliveiro wrote:
In article <11**********************@c74g2000cwc.googlegroups .com>,
"dylpkls91" <dp******@pacbell.net> wrote:
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?


@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.

Jun 27 '06 #3
In article <11**********************@u72g2000cwu.googlegroups .com>,
"dylpkls91" <dp******@pacbell.net> wrote:
Lawrence D'Oliveiro wrote:
In article <11**********************@c74g2000cwc.googlegroups .com>,
"dylpkls91" <dp******@pacbell.net> wrote:
>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?


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.
Jun 29 '06 #4
Lawrence D'Oliveiro wrote:
In article <11**********************@u72g2000cwu.googlegroups .com>,
"dylpkls91" <dp******@pacbell.net> wrote:

Lawrence D'Oliveiro wrote:
In article <11**********************@c74g2000cwc.googlegroups .com>,
"dylpkls91" <dp******@pacbell.net> wrote:
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?


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.


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
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Love me, love my blog http://holdenweb.blogspot.com
Recent Ramblings http://del.icio.us/steve.holden

Jun 29 '06 #5
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?

Jul 12 '06 #6
dylpkls91 wrote:
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.

--
Ben Sizer

Jul 13 '06 #7

Ben Sizer wrote:
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?

Jul 13 '06 #8
Thank you all for your responses.

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

Jul 13 '06 #9
On 2006-07-13 20:46:16, dylpkls91 wrote:
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

Jul 14 '06 #10

dylpkls91 wrote:
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? ;)

Jul 16 '06 #11
co**********@gmail.com wrote:
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/s...ervice.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

Jul 17 '06 #12
dylpkls91 wrote:
co**********@gmail.com wrote:
>>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/s...ervice.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
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://holdenweb.blogspot.com
Recent Ramblings http://del.icio.us/steve.holden

Jul 18 '06 #13

Steve Holden wrote:
dylpkls91 wrote:

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/s...ervice.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.
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

Jul 18 '06 #14
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

Jul 18 '06 #15
Frank Millman wrote:
I use something called 'srvany' -
http://support.microsoft.com/kb/q137890/
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!

Jul 19 '06 #16

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: jake | last post by:
How can I copy a file securely (encryption) from remote computers easily (Mac and Linux) to my Win 2000 Server once a day?
15
by: Madhanmohan S | last post by:
Hi All, I want to run a command line appplication from C#. When i start the application, it will go into specific mode. After that, i have to give commands to use the application. How Can This Be...
1
by: Faraz | last post by:
Hey guys. How cam I interface with the environment in C#. I want to execute commands in a (separate) process, just like java Runtime.exec(string command) does. Thanks in advance,
3
by: Just D. | last post by:
I know that writing ASPX app we easily can get all required information about remote machine and network right from the user's browser using this request: NameValueCollection coll =...
5
by: tracid83 | last post by:
Hello, I would like to get information from remote computer and printer connected to network. I don't want to use windows WMI service. Thank you for your help.
15
by: tmp123 | last post by:
Hello, Thanks for your time. We have very big files with python commands (more or less, 500000 commands each file). It is possible to execute them command by command, like if the commands...
1
by: Hishaam | last post by:
How to execute commands in internal zones of solaris using python running from the global zone ? i tried -- 1os.popen("zlogin <zone1>") 2os.popen("zonename") the 2nd command executes back...
6
by: samvb | last post by:
Hello Guys, Do you know any programs that can install applications to remote computers without the users knowledge/interaction? And any way to deply windows to remote computers/clients from server?...
1
by: nthato | last post by:
Hi I am trying to create a script that will execute commands on several remote servers and I am having problems to increment my positional variables. I the a simple way of doing this???
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.