473,383 Members | 1,837 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,383 software developers and data experts.

Unable to write the output of pexpect sendline() function to a log file

Hi,
I am trying to ssh to a device, execute and capture the output of the commands sent across via sendline to the DUT to a file. I can see that after the "cat /proc/slabinfo'" command is executed and data is captured in the file, I do not see the output of the last 2 in the file.


def mem_cpu():
stats_file = 'AP_LOG_{}.log'.format(date.today())
with open(stats_file, 'a+') as cpu:
cpu.write(str(datetime.datetime.now())+'\n')
cpu.write('####################################### ########################################'+'\n')
child.sendline('top -n 1')
child.logfile_read = open(stats_file, 'a')
cpu.write('\n'+'\n')
child.expect('#')
child.sendline('cat /proc/meminfo')
child.logfile_read = open(stats_file, 'a')
cpu.write('\n'+'\n')
child.expect('#')
child.sendline('free')
child.logfile_read = open(stats_file, 'a')
cpu.write('\n'+'\n')
child.expect('#')
child.sendline('cat /proc/slabinfo')
child.logfile_read = open(stats_file, 'a')
cpu.write('\n'+'\n')
child.expect('#')
child.sendline('cat /proc/tasklets')
child.logfile = open(stats_file, 'a')
cpu.write('\n'+'\n')
child.expect('#')
child.sendline('ps axww')
child.logfile = open(stats_file, 'a')
cpu.write('\n'+'\n')
child.expect('#'
child.sendline('exit')

Below is the main part of the script where the above function is called.

#Opening a file to write the error logs to
mylogfile = open('logfile.txt', 'a+')
with open('Ap_Access_log.log', 'w+') as f:

# Script to log into the APs and excute the required shell commands.
for ip in list_of_IP_addresses:
ip1=str(ip)
child = pexpect.spawn('ssh '+username+'@'+ip1, logfile=mylogfile)
child.setwinsize(500, 600)
# child = pexpect.spawnu('ssh '+username+'@'+ip1)
#child.logfile_read = sys.stdout
code = child.expect(['Are you sure you want to continue connecting (yes/no)?', 'Please login: ', 'password :', 'No route to host', pexpect.EOF, pexpect.TIMEOUT])
if code==0:
child.sendline('yes')
code = child.expect(['Are you sure you want to continue connecting (yes/no)?', 'Please login: ', 'password :', 'No route to host', pexpect.EOF, pexpect.TIMEOUT])
if code==1:
print('Entering the Username')
child.sendline(username)
code = child.expect(['Are you sure you want to continue connecting (yes/no)?', 'Please login: ', 'password :', 'No route to host', pexpect.EOF, pexpect.TIMEOUT])
if code==2:
print('Entering the credentials')
child.sendline(password)
mem_cpu()
child.close()

a) Unsure of why the last two are missing from the file.
b) If ai remove the slabinfo and one other command, then that two show up.

Thank youf for the help.
Jun 1 '17 #1

✓ answered by KiranCherian

Solved by executing the commands with a comma separation;

child.sendline('cat /proc/taskliets; ps axww; exit')
time.sleep(3)
child.logfile = open(stats_file, 'a')
child.expect(pexpect.EOF)


This way i was able to capture all of the data.

1 5662
Solved by executing the commands with a comma separation;

child.sendline('cat /proc/taskliets; ps axww; exit')
time.sleep(3)
child.logfile = open(stats_file, 'a')
child.expect(pexpect.EOF)


This way i was able to capture all of the data.
Jun 1 '17 #2

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

Similar topics

16
by: Chuck Amadi | last post by:
Sorry to bovver you again (again) here's script. I still can't see why the get_payload() doesn't produce the plain text message body of an emails in the testwwws users mailbox. As you can see I...
2
by: Geoff Wilkins | last post by:
I am using <SCRIPT src=...> to import a Javascript routine from a remote source. I am then using some of the variables given values in the routine, in my own Javascript.. Unfortunately the...
2
by: Angel | last post by:
How much harder is it to write data to an XML file instead of an ascii file? When I write an output data file, I usually write it as an ascii file. But the client's been thinking of writing it in...
3
by: Billy Smith | last post by:
I'm trying to write a little utility that will write some binary data to a file via a javascript and Windows Script Host under Windows XP. The only way to do this that I can find is to convert...
0
by: super.raddish | last post by:
Greetings one and all, I have a single source XML file and I am parsing the "records" within it into separate XML files using xsl:result-document. All that works fine. I am using the Saxon...
1
by: Rahul83 | last post by:
Hi.. I have written a perl script for displaying the difference between two directory structures.(including the diffferences in the files) I have to generate the output in a log file. ...
3
by: tejas900 | last post by:
hi All, I am extracting data from a website using a automation tool. I am getting the data in the form of an array, which I want to write it in a text file. Please have a look at the below piece...
0
by: Paul Lemelle | last post by:
Jeff, Thanks for your reply. I would like to like the absolute path of a directory. I thought that os.listdir just returns the nam itself in a data list. I noticed that none was being return...
2
by: webguy262 | last post by:
I'm trying to troubleshoot a membership script. The script is attempting to write to a file called .fdaccess. The function that's throwing the error is in a file called htpasswd.php. Here's the...
1
Jory R Ferrell
by: Jory R Ferrell | last post by:
Uhm...."Why might my interpreter be unable to find the pyOpenCL function 'create_some_context'." Is what I meant to write. Anyways... I both manually installed (simply pasted the files into the...
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: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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
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...

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.