What i want to build is this:
Sending a command to another computer and get the results back on the computer where i started on.

Additional info:
I have a web server running with python on 192.168.10.100:5000.
When i go to this address i get a web page and on this web page you'll this:

PC1
192.168.10.110
textbox[type your command]
button[send command to this computer]

PC2
192.168.10.120
textbox[type your command]
button[send command to this computer]

So when i type in a command in the textbox of the first pc (i.e. ipconfig) and i click on the button, i want that the command is send to the first computer, and that the first computer executes the (ipconfig) command and sends the result back to the webserver (192.168.10.100) and presents the results on the screen.

I already have the webserver running in python and the website is also build and i can also send a command to the webserver itself and get that result back. The only problem is that i don't know how to send the command to another computer.

This is the code to do commands locally on this webserver:

POST=cgi.FieldStorage()
cmd=POST['commando'].value

def voerUIT (commando):
    uitvoer = subprocess.Popen(commando, shell=True, stdin = subprocess.PIPE, stdout = subprocess.PIPE, stderr = subprocess.STDOUT)
    return uitvoer

if cmd:
    
    doe = voerUIT(cmd)
    for x in doe.stdout.readlines():
        print ( x.decode()+ '<br />' )

Recommended Answers

All 7 Replies

So i have to build an ssh server on the remote pc where i want to send the command to right? But after that how do i connect them? And how do i send the command from my webserver to the remote ssh client?

Could you build a working example for my situation please so i fully understand what the script is doing?

And i'm sorry but i do not completely understand what your script is doing.

1.	import paramiko
2.	ssh = paramiko.SSHClient()
3.	ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
4.	ssh.connect("IP_ADDRESS", username="USER", password="PASSWORD", allow_agent = False)
5.	i, o, e = ssh.exec_command("cp foo bar") # example command
6.	s = e.read()
7.	if s: # an error occurred
8.	raise RuntimeError, s
9.	result = o.read()

Your web server would run similar code to send a command to the computer with a ssh server. The first 4 lines connect to the ssh server, line 5 sends a command to the ssh server, i, o, e are input, output and error streams for this command. Lines 6+ read the command's output in the streams o and e.

Edit: click the 'Toggle plain text' item before you copy code from this forum.

So i also run this same script on my server in line 4 i would type this?
ssh.connect("IP_ADDRESS", username="USER", password="PASSWORD", allow_agent = False)

IP?ADDRESS = ip address of remote server?
USER = username on remote server?
PASSWORD = password of the login name of the remote server?

So if i want to send ipconfig to this other computer on line 5 i do this:
i, o, e = ssh.exec_command("ipconfig") # example command

So i also run this same script on my server in line 4 i would type this?
ssh.connect("IP_ADDRESS", username="USER", password="PASSWORD", allow_agent = False)

IP?ADDRESS = ip address of remote server?
USER = username on remote server?
PASSWORD = password of the login name of the remote server?

So if i want to send ipconfig to this other computer on line 5 i do this:
i, o, e = ssh.exec_command("ipconfig") # example command

Exactly. I just tried the following script at home

#!/usr/bin/env python
# -*-coding: iso8859-1-*-
import warnings
warnings.filterwarnings("ignore")
import paramiko

COMPNAME = 'hirondelle' # or '192.168.0.17'
USER = 'ClintEastwood'
PASSWORD = '7==4K23_-"_"-"_uu'

ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(COMPNAME, username=USER, password=PASSWORD, allow_agent = False)

def check_stderr(stream):
    s = stream.read()
    if s:
        raise RuntimeError, s

def ipconfig(ssh):
    com = "ipconfig"
    i, o, e = ssh.exec_command(com)
    check_stderr(e)
    return ''.join(o .readlines())
    
def home_dir(user):
    return "/cygdrive/c/Documents*Settings/" + user

if __name__ == "__main__":
    print ipconfig(ssh).decode('latin1')
    
""" my output --->
Configuration IP de Windows


Carte Ethernet Connexion au réseau local:

        Suffixe DNS propre à la connexion : 
        Adresse IP. . . . . . . . . . . . : 192.168.0.17
        Masque de sous-réseau . . . . . . : 255.255.255.0
        Passerelle par défaut . . . . . . : 192.168.0.1
"""

All I had to do was to install copssh on the computer named 'hirondelle'. User name and password have been changed for the forum (I also have another trick to avoid hard coding my user name and password).

I tried the same script and get the following error at line 19:
raise RuntimeError, s

It fails at the comma. So i changed the line into:
raise (RuntimeError, s)

Then i got the following error on line 31:
print ipconfig(ssh).decode('latin1')

I changed it into:
print (ipconfig(ssh).decode('latin1'))

But now when i run the script i get this error:
Traceback (most recent call last):
File "C:\Scripts\test.py", line 3, in <module>
import paramiko
ImportError: No module named paramiko

Well i tried installing paramiko but i just couldn't get it to work. All the installations finished without any error message's or warnings but still python tells me i don't have this pakage. Could you pleaes tell me where you got it and how you installed it? Cause without it i cannot continue.

My version of Python is 3.2.2.
My 2 servers are:
Win 7 64bit

One server is physical and the other server is virtual.

Also if you know a way how to do this with telnet instead of ssh i also would appreciate it very much!

Cause i just cannot continue now that i wasn't able to install paramiko.

Unfortunately I never ran paramiko in python 3. I'm afraid it doesn't yet work in python 3. In this case you have different options

1) use python 2.7 instead of 3.2
2) install python 2.7 together with 3.2 on your machine (see the altinstall option in the python install procedure) and start a python 2.7 process using paramiko which communicates with your web server (for example through an xmlrpc server)
3) Another possible solution is to use a windows executable called plink.exe which communicates with remote ssh servers (it is probably installed when you install 'putty'). See here http://the.earth.li/~sgtatham/putty/0.58/htmldoc/Chapter7.html. With such a program, you could probably write something like

com = Command("plink hirondelle -l clinteastwood -pwd foobar ipconfig").run()
print(com.output)

where Command is a snippet similar to this one http://www.daniweb.com/software-development/python/code/257449 .

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.