python connect to server using SSH protocol

A

ajikoe

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
 
L

Laszlo Zsolt Nagy

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: (e-mail address removed)

Python forever!
 
P

P

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

Simon Anders

Hi
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
 
L

Laszlo Zsolt Nagy

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: (e-mail address removed)

Python forever!
 
K

Kartic

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
 
T

Toby Dickenson

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
 

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,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top