473,666 Members | 2,386 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 3792
aj****@gmail.co m 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,chi ld_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*****@geochem source.com

Python forever!
Jul 18 '05 #2
P
aj****@gmail.co m 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.co m 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,chi ld_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*****@geochem source.com

Python forever!
Jul 18 '05 #5

aj****@gmail.co m 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 programmaticall y
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.co m 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.writ e(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
3332
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
3517
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. The problem is that MSN file sending/receiving capabilities are terribly slow and has no resume option. We're also on
10
3681
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. Andrew dalke@dalkescientific.com
3
7217
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. These auto-register themselves in the admin client. However, when I try to connect to the db's to look at the Table details I get the following errors: ------- SQL30081N A communication error has
14
25255
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 tried a test, and I can't bind two sockets to the same port/ip but I can connect with one socket, and bind with another to the same ip/port. Thanks
16
3089
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. for example. in my program I would want to call the ssh feature like one does on the command line ssh Admin@192.168.2.10 .....etc How can I do this easily ?
14
7027
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 sql server resides on is not reachable. The error is different depending on the connection string that I use. If I use the following connection string: "server=192.1.1.1; Initial Catalog=master; uid=The_User; password=The_Password; Connect...
1
1145
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', '/index.html' print servername, filename
28
7041
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 certificate. But how can I tell my Python program to trust my SSL certificate? When I tried before I received the error 503 Thank you for help L,
0
8440
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8355
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
8550
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8638
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7381
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6191
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
1
2769
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 we have to send another system
2
2006
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1769
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.