473,471 Members | 1,967 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

pexpect output

11 New Member
I have a script that uses pexpect to ssh to a list of servers and will run a user specified command on each server. I have tried to use pexpect logging to output the results of each command to a file so the user can see the output (of commands such as uptime and ntpdate). The remote servers are both Solaris and Red Hat. For some reason, not all output is captured in the file.
Here is my code:

Some code unnecessary and snipped for this question...

Expand|Select|Wrap|Line Numbers
  1. def DoStuff():
  2.    fout = file(LOG_FILENAME,'w')
  3.  <snip>
  4.    child = pexpect.spawn('ssh -l root %s'%(i))
  5.    k = child.expect (['connecting (yes/no)', '[Pp]assword: '])
  6.    if k==0:
  7.       print "Adding key for %s"%(i)
  8.       child.sendline ("yes")
  9.       child.expect ('[Pp]assword: ')
  10.       child.sendline (Passwd1)
  11.    if k==1:
  12.       child.sendline (Passwd1)
  13.    l = child.expect (['please try again.', '[#/$]'])
  14.    if l==0:
  15.       child.sendline (Passwd2)
  16.    if l==1:
  17.       child.sendline ('echo')
  18.    child.expect ('[#/$]')
  19.    child.sendline (cmnd)
  20.    child.logfile_read = fout
  21.    child.expect ('#')
  22.    child.logfile = None
  23.    child.sendline ('exit')
  24.  
Ideally, I would like a way to print the server name on a line then the output from the command on the next line(s).
I can not figure out why the above, using uptime as the command, gives the following:


Expand|Select|Wrap|Line Numbers
  1. $ more /tmp/menu2.log
  2. root@server1 :#uptime
  3. root@server2 :#uptimeuptime
  4.   2:53pm  up 174 day(s),  8:07,  1 user,  load average: 0.00, 0.00, 0.01
  5. # uptime
  6.   2:46pm  up 174 day(s),  7:53,  1 user,  load average: 0.04, 0.04, 0.04
  7. #
  8. root@server3 :#uptime
  9. root@server4 :#uptime
  10. root@server5 :#uptime.example.com now...
Jun 4 '10 #1
0 1251

Sign in to post your reply or Sign up for a free account.

Similar topics

8
by: Hugh Macdonald | last post by:
I'm calling a command from within a python script and I need to be able to both catch the output (stdout and stderr) from it and also have the PID (so that I can kill it) I can do one or other...
8
by: Gianluca Trombetta | last post by:
Someone know pexpect module? I've a problem working with it... I need to run some commands on remote hosts, like ls, df -k etc..All right. Although, when i launch a command that have a "more"...
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: Adrian Casey | last post by:
I have a python script which uses pexpect and I want to timeout (i.e. raise pexpect.TIMEOUT) if a long running command does not produce the output I am expecting. To simulate the 'long running...
5
by: funkyj | last post by:
I love pexpect because it means I may never have to use expect again (I don't do any heavy expect lifting -- I just need simple tty control)! As a python advocate I find it embarassing how...
4
by: jonathan.sabo | last post by:
I have a pexpect script to walk through a cisco terminal server and I was hoping to get some help with this regex because I really suck at it. This is the code: index = s.expect() if index...
1
by: Kevin Erickson | last post by:
Hello, I am attempting to use pexpect in python to copy files from a server using scp; the copy works however exceptions are thrown and it exits unsuccessfully. Below is the a sample code and...
1
by: Sriram Rajan | last post by:
For some reason, Using pexpect causes my output to echo twice when I connect from my MAC Darwin (10.4) to Linux (CentOS release 5 ): The program: --------------------- #!/usr/bin/python #...
1
by: Anh Khuong | last post by:
Hi all, I am using pexpect and I want to send output of pexpet to both stdout and log file concurrently. Anybody know a solution for it please let me know. Thanks
0
by: Lie Ryan | last post by:
On Tue, 30 Sep 2008 20:48:12 -0700, Anh Khuong wrote: One way is to create a file-like object that forked the output to stdout and the logfile. class forkwriter(object): def...
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
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,...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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,...
1
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...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...

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.