472,354 Members | 1,913 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,354 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 39931
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...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge required to effectively administer and manage Oracle...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made but the http to https rule only works for...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it so the python app could use a http request to get...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and credentials and received a successful connection...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific technical details, Gmail likely implements measures...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the synthesis of my design into a bitstream, not the C++...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand. Background colors can be used to highlight important...
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS starter kit that's not only easy to use but also...
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python has gained popularity among beginners and experts...

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.