Gribouillis
Posting Maven
2,786 posts since Jul 2008
Reputation Points: 1,044
Solved Threads: 691
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.
Gribouillis
Posting Maven
2,786 posts since Jul 2008
Reputation Points: 1,044
Solved Threads: 691
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).
Gribouillis
Posting Maven
2,786 posts since Jul 2008
Reputation Points: 1,044
Solved Threads: 691
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 .
Gribouillis
Posting Maven
2,786 posts since Jul 2008
Reputation Points: 1,044
Solved Threads: 691