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.