473,506 Members | 11,491 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

python connect to server using SSH protocol

How can python connect to server which use SSH protocol?
Is it easy since my python has to run third party vendor, write data,
read data inside the server (supercomputer).

Any suggestion?

Sincerely Yours,
Pujo Aji

Jul 18 '05 #1
7 3782
aj****@gmail.com wrote:
How can python connect to server which use SSH protocol?
Is it easy since my python has to run third party vendor, write data,
read data inside the server (supercomputer).

In advance, I'm not sure if I understood your problem. SSH is clearly a
remote
shell. You will be able to execute other programs on the remote computer.
Is is suitable to use this:

child_stdin,child_stdout,child_stderr = os.popen2('ssh -l my_username
my_host')
On the other side, you can write an application that also uses
stdin/stdout for communication.
Best,

Laci 2.0

--
__________________________________________________ _______________
Laszlo Nagy web: http://designasign.biz
IT Consultant mail: ga*****@geochemsource.com

Python forever!
Jul 18 '05 #2
P
aj****@gmail.com wrote:
How can python connect to server which use SSH protocol?
Is it easy since my python has to run third party vendor, write data,
read data inside the server (supercomputer).

Any suggestion?


you can use popen around the ssh binary.
You man need the pty module if you want to deal with password prompts
If you want lower level control, use the twisted module.

--
Pádraig Brady - http://www.pixelbeat.org
--
Jul 18 '05 #3
Hi

Laszlo Zsolt Nagy wrote:
aj****@gmail.com wrote:
How can python connect to server which use SSH protocol?
Is it easy since my python has to run third party vendor, write data,
read data inside the server (supercomputer).

In advance, I'm not sure if I understood your problem. SSH is clearly a
remote
shell. You will be able to execute other programs on the remote computer.
Is is suitable to use this:

child_stdin,child_stdout,child_stderr = os.popen2('ssh -l my_username
my_host')


This is what I was about to reply as well. But I did a short test
program and encountered a problem:

import os
fi, foe = os.popen4 ('ssh dopey')
print >>fi, 'ls'
fi.close () # <-- this is annoying
for line in foe:
print line,
foe.close ()

The above connects to a server, passes the command 'ls', which is
executed there, and prints the returned result.

However, reading from foe succeeds only if fin has been closed before.
An fi.flush() seems to be not sufficient. But if one wants Python to
interactivly communicate with some shell on a remote machine, it is
inconvenient to have to close and reopen the connection all the time.

There should be a better way.

Simon
Jul 18 '05 #4

import os
fi, foe = os.popen4 ('ssh dopey')
print >>fi, 'ls'
fi.close () # <-- this is annoying
for line in foe:
print line,
foe.close ()

The above connects to a server, passes the command 'ls', which is
executed there, and prints the returned result.

However, reading from foe succeeds only if fin has been closed before.
An fi.flush() seems to be not sufficient.


Hmm. Is it possible to use ssh for simple port forwarding (-L option)
and write a server program on the remote machine?

--
__________________________________________________ _______________
Laszlo Nagy web: http://designasign.biz
IT Consultant mail: ga*****@geochemsource.com

Python forever!
Jul 18 '05 #5

aj****@gmail.com wrote:
How can python connect to server which use SSH protocol?
Is it easy since my python has to run third party vendor, write data,
read data inside the server (supercomputer).

Any suggestion?

Sincerely Yours,
Pujo Aji


Pujo,

There are two Python oriented SSH solutions.
1. pyssh (pyssh.sf.net) which is a library for programmatically
controlling ssh.
2. Paramiko (http://www.lag.net/paramiko/) which are python bindings
for the SSH2 protocol.

I have used neither of these packages, so I am in no position to
recommend anything.

Of course, you have the usual popen() functions that you can use to
invoke SSH.

Thanks,
-Kartic

Jul 18 '05 #6
Simon Anders wrote:
Hi

Laszlo Zsolt Nagy wrote:
aj****@gmail.com wrote:
How can python connect to server which use SSH protocol?

<BIG SNIP>

There should be a better way.

Simon


There is : pexpect! http://pexpect.sourceforge.net/

Jul 18 '05 #7
On Tuesday 08 February 2005 13:26, Simon Anders wrote:
This is what I was about to reply as well. But I did a short test
program and encountered a problem:

import os
fi, foe = os.popen4 ('ssh dopey')
print >>fi, 'ls'
fi.close () # <-- this is annoying
for line in foe:
print line,
foe.close ()

The above connects to a server, passes the command 'ls', which is
executed there, and prints the returned result.

However, reading from foe succeeds only if fin has been closed before.
An fi.flush() seems to be not sufficient.
But this version below does work. Im not sure whats happening when using the
file as an iterator to make a difference.

import os, sys
fi, foe = os.popen4 ('ssh xxxxx')
print >>fi, 'ls'
fi.flush () # <-- this is annoying
while 1:
b = foe.readline()
sys.stdout.write(b)

But if one wants Python to
interactivly communicate with some shell on a remote machine, it is
inconvenient to have to close and reopen the connection all the time.


But this route carries a big deadlock risk. the rsync program can tunnel over
ssh this way, but have first hand experience of
http://www.google.com/search?q=rsync+ssh+deadlock

In the script above, if 'ls' is replaced with a longer input then there is
every chance that the fi stream will block before all of it is written,
because this script hasnt started draining foe.

For a real python program using ssh (but not 'interactive'... data written to
fi does not depend on data read from foe) see
http://dirstorage.sourceforge.net/replica.html
--
Toby Dickenson
Jul 18 '05 #8

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

Similar topics

13
3310
by: Ajay | last post by:
hi! can you call a Python application from a Java program? does this require any additional package to be installed? thanks cheers
8
3509
by: gs | last post by:
Hi! This is my first time posting to a newsgroup so please be gentle to me :) Introduction to my problem: I'm studying at the university and a lot of friends and I use MSN to communicate....
10
3660
by: Andrew Dalke | last post by:
Is there an author index for the new version of the Python cookbook? As a contributor I got my comp version delivered today and my ego wanted some gratification. I couldn't find my entries. ...
3
7193
by: Harlin | last post by:
Hi, I installed the admin client on my WebSphere box. I am now attempting to connect to my DB2 server (on another box). I have been able to find the DB2 server, instance and needed databases....
14
25234
by: DaTurk | last post by:
I am makeing a Multicast server client setup and was wondering what the difference is between Socket.Connect, and Socket.Bind. It may be a stupid question, but I was just curious. Because I...
16
3074
by: diffuser78 | last post by:
I want to write a python program and call OS specific commands in it. So basically, instead of typing in on the command line argument I want to have it in a python program and let it do the action....
14
6995
by: Marcus | last post by:
I have a function that simply returns TRUE if it can connect to a particular Sql Server 2005 express, or FALSE if it cannot. I am getting some strange error codes returned when the computer that...
1
1140
by: allavarapu | last post by:
Hi This is the program . import sys, httplib showlines = 6 try: servername, filename = sys.argv # cmdline args? except: servername, filename = 'starship.python.net',...
28
7021
by: Johny | last post by:
I need to use Python with SSL comunication betweeen servers. (I use hhtplib but I think urllib2 can also be used ) I think I need to use SSL root certificate and tell a program to trust this...
0
7220
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7479
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5617
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5037
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...
0
4702
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3178
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1534
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
757
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
410
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.