473,382 Members | 1,476 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

Using python with SSH?

Hi,

I need to run a remote program from a server that I have access rights
to. I want to be able to do this from within in my python script and I
want to use ssh (or anything else that will work). However, doing
something like:

ssh -l user host "/path/to/program"

prompts for a password and I don't know how to supply one via a python
script. Is there an easy way out? Can I log onto to the other machine
via ssh somehow? Does a python script support this? Thanks!

Steve

Jul 18 '05 #1
6 39997
Steve <nospam@nopes> writes:
ssh -l user host "/path/to/program"

prompts for a password and I don't know how to supply one via a python
script. Is there an easy way out? Can I log onto to the other machine
via ssh somehow? Does a python script support this? Thanks!


The simplest way is to set up ssh at both ends with RSA authentication
and have a private key on the local machine. That gets rid of the
need for a password, while still keeping the connection secure, as
long as the local machine is secure.
Jul 18 '05 #2
Steve <nospam@nopes> writes:
ssh -l user host "/path/to/program"

prompts for a password and I don't know how to supply one via a python
script. Is there an easy way out? Can I log onto to the other machine via
ssh somehow? Does a python script support this? Thanks!


Try something like this:

import os

ssh_pipe = os.popen("ssh -l user path")
ssh_pipe.write(password)
ssh_pipe.write("\n")
ssh_pipe.flush()
ssh_pipe.close()

hope this helps

--
Lawrence "Rhymes" Oluyede
http://loluyede.blogspot.com
Jul 18 '05 #3
On Thu, Dec 18, 2003 at 12:08:43AM +0100, Lawrence Oluyede wrote:
Steve <nospam@nopes> writes:
ssh -l user host "/path/to/program"

prompts for a password and I don't know how to supply one via a python
script. Is there an easy way out? Can I log onto to the other machine via
ssh somehow? Does a python script support this? Thanks!


You could open ssh in a pty, or you could use a Python module that speaks
the SSH protocol, thus obviating the requirement to call out to the ssh
command line program.

A couple choices for such modules:

http:/www.twistedmatrix.com/

http://www.lag.net/~robey/secsh/
Try something like this:

import os

ssh_pipe = os.popen("ssh -l user path")
ssh_pipe.write(password)
ssh_pipe.write("\n")
ssh_pipe.flush()
ssh_pipe.close()

hope this helps


It does not. ssh won't accept a password from standard input, it requires
it to be entered into the terminal.

Jp

Jul 18 '05 #4
Jp Calderone <ex*****@intarweb.us> writes:
It does not. ssh won't accept a password from standard input, it requires
it to be entered into the terminal.


Ah ok, i thought it did.

--
Lawrence "Rhymes" Oluyede
http://loluyede.blogspot.com
Jul 18 '05 #5
On Wed, 2003-12-17 at 23:00, Steve wrote:
Hi,

I need to run a remote program from a server that I have access rights
to. I want to be able to do this from within in my python script and I
want to use ssh (or anything else that will work). However, doing
something like:

ssh -l user host "/path/to/program"

prompts for a password and I don't know how to supply one via a python
script. Is there an easy way out? Can I log onto to the other machine
via ssh somehow? Does a python script support this? Thanks!

Steve


Steve,
You don't say if you are running on Windows or not but this is a ssh
wrapper I use that uses the pexpect library...:
import pexpect
import sys
import re
import os
PROMPT = "\$|\%|\>"
class SSH:
def __init__(self, user, password, host):
self.child = pexpect.spawn("ssh %s@%s"%(user, host))
i = self.child.expect(['assword:', r"yes/no"], timeout=120)
if i==0:
self.child.sendline(password)
elif i==1:
self.child.sendline("yes")
self.child.expect("assword:", timeout=120)
self.child.sendline(password)
self.child.expect(PROMPT)
def command(self, command):
"""send a command and return the response"""
self.child.sendline(command)
self.child.expect(PROMPT)
response = self.child.before
return response

def close(self):
"""close the connection"""
self.child.close()
if __name__=="__main__":
import getpass
password = getpass.getpass("Password: ")
ssh = SSH("RemoteUsername", password, "RemoteHost")
print ssh.command("pwd")
ssh.close()


--
Martin Franklin <mf********@gatwick.westerngeco.slb.com>
Jul 18 '05 #6
On Thu, 18 Dec 2003 10:06:02 +0000, Martin Franklin <mf********@gatwick.westerngeco.slb.com> wrote:
On Wed, 2003-12-17 at 23:00, Steve wrote:
Hi,

I need to run a remote program from a server that I have access rights
to. I want to be able to do this from within in my python script and I
want to use ssh (or anything else that will work). However, doing
something like:

ssh -l user host "/path/to/program"

prompts for a password and I don't know how to supply one via a python
script. Is there an easy way out? Can I log onto to the other machine


If your sysadmin allows it, you can configure ssh to connect to another
machine without prompting for a password, at least at Linux systems.
Albert
--
Unlike popular belief, the .doc format is not an open publically available format.
Jul 18 '05 #7

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

Similar topics

5
by: MiLF | last post by:
Is it possible to write a Audio CD Player by using python only?
4
by: The_Incubator | last post by:
As the subject suggests, I am interested in using Python as a scripting language for a game that is primarily implemented in C++, and I am also interested in using generators in those scripts... ...
8
by: Sridhar R | last post by:
Hi, I am a little experienced python programmer (2 months). I am somewhat experienced in C/C++. I am planning (now in design stage) to write an IDE in python. The IDE will not be a simple...
8
by: Joakim Persson | last post by:
Hello all. I am involved in a project where we have a desire to improve our software testing tools, and I'm in charge of looking for solutions regarding the logging of our software (originating...
29
by: 63q2o4i02 | last post by:
Hi, I'm interested in using python to start writing a CAD program for electrical design. I just got done reading Steven Rubin's book, I've used "real" EDA tools, and I have an MSEE, so I know what...
11
by: efrat | last post by:
Hello, I'm planning to use Python in order to teach a DSA (data structures and algorithms) course in an academic institute. If you could help out with the following questions, I'd sure...
0
by: P. Adhia | last post by:
Hello, I was wondering if anyone is successfully using using Python(2.5)+DB2+pydb2. I get an error in all situations. It seems that this problem might be limited to python 2.5. A quick Google...
9
by: dominiquevalentine | last post by:
Hello, I'm a teen trying to do my part in improving the world, and me and my pal came up with some concepts to improve the transportation system. I have googled up and down for examples of using...
14
by: Nathan Pinno | last post by:
How do I factor a number? I mean how do I translate x! into proper Python code, so that it will always do the correct math? Thanks in advance, Nathan P.
53
by: Vicent Giner | last post by:
Hello. I am new to Python. It seems a very interesting language to me. Its simplicity is very attractive. However, it is usually said that Python is not a compiled but interpreted programming...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.