By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,191 Members | 1,256 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,191 IT Pros & Developers. It's quick & easy.

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

P: 2
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.

Share this Question
Share on Google+
1 Reply


P: 2
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

Post your reply

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