473,394 Members | 2,160 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,394 software developers and data experts.

Pexpect: SSH, Command and Password

Hi,

I'm trying to run a script over unix on a remote machine.
In order to automate it, the procedure requests the following:
1. Using SSH connection.
2. Operating a command on the remote machine.
3. Expecting password or (yes/no) request and authorize it.

I get an error (I thing that it occures at the last part (3) of the
password request).
I don't have any errors when trying to operate the script without the
last password.
Here is the script:

import pexpect
import sys
import re
import os
import getopt

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, password):
"""send a command and return the response"""
self.child.expect(PROMPT)
self.child.sendline(command)
j = self.child.expect(['assword:', r'yes/no'],
timeout=120)
if j==0:
self.child.sendline(password)
elif j==1:
self.child.sendline("yes")
self.child.expect("assword:", timeout=120)
self.child.sendline(password)
self.child.expect(PROMPT)

# response = self.child.before
# return response

def close(self):
"""close the connection"""
self.child.close()
if __name__=="__main__":

ssh = SSH(sys.argv[1], sys.argv[2], sys.argv[3])
command=sys.argv[4]
print command
print "Password: " + sys.argv[2]
responce=ssh.command(command, sys.argv[2])
ssh.close()
print responce
I'm waiting for your ideas.
Thanks,
Gil H.

Apr 4 '07 #1
2 9979
>>>>"Gil" == Gil H <gi*******@gmail.comwrites:
class SSH:
def __init__(self, user, password, host):
self.child = pexpect.spawn("ssh %s@%s"%(user, host))
Try adding the following line here

self.child.logfile = sys.stdout

That should give you some clue.

Ganesan
--
Ganesan Rajagopal

Apr 4 '07 #2
On 4 Apr, 15:14, Ganesan Rajagopal <rgane...@myrealbox.comwrote:
>>>"Gil" == Gil H <gil.ho...@gmail.comwrites:
classSSH:
def __init__(self, user, password, host):
self.child =pexpect.spawn("ssh%s@%s"%(user, host))

Try adding the following line here

self.child.logfile = sys.stdout

That should give you some clue.

Ganesan

--
Ganesan Rajagopal
Thanks,

That's all i needed!

Apr 4 '07 #3

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

Similar topics

2
by: Michael Surette | last post by:
I have been trying to automate the changing of passwords using python and pexpect. I wrote a script as a test and it works, except that it gives me an exception when it stops running: Exception...
0
by: Steve Horsley | last post by:
I am trying to automate changing of a password for a particular user account (for use by scripts, not people). I am trying to use the command "passwd testuser" as root. Of course I can do this by...
0
by: Eric Myers | last post by:
Hello folks: (This message is also posted on the help forum at the pexpect sourceforge page, but all indentation in the code got stripped away when I submitted the post.) For some time I've...
0
by: dwelch91 | last post by:
I'm having a problem using pexpect with 'sudo' on Ubuntu 6.06 (Dapper). Here's the program: #!/usr/bin/env python import pexpect import sys child = pexpect.spawn("sudo apt-get update")...
5
by: half.italian | last post by:
Hi all. I try not to post until I am stuck in hole with no way out. I fought with this for several hours, and am currently in the hole. I'm doing a proof of concept for creating afp shares...
0
by: flynnguy | last post by:
I am trying to essentially fork a rsync process from my python script and I am having some issues with the forking part. My program basically checks to see if I need to transfer a file and if it...
0
by: bsdixon21222 | last post by:
Hello All, I havn't been programming with python too long, but decided the best way to learn it would be to dive right in. Right now I have been taking snippets here and there along with...
8
by: asgarde | last post by:
hello, I'm new in Python and i would like to use Pexpect to execute a root command (i want to mount via a Pyhton script a drive) so that's my script for the moment : from os import *...
2
by: BlackjadeLin | last post by:
I'm new to python I want to write a simple script to switch user,for example,from user_A to user_B. This my codes: #!/usr/bin/python import pexpect import os passwd="user_B" child =...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: 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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
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...

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.