Objective: I want to write a Python test script to execute a reboot command through SSH on a remote Linux machine, capture the boot logs and check if the boot is successful.
Problem: I am able to send a reboot command using Paramiko and the machine reboots as expected, but I couldn't capture the boot logs and print them out. My code also seems to run without waiting for the boot process to finish.
Here is part of my code:
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
try:
ssh.connect(host, username=username, password=password)
except paramiko.SSHException as e:
ssh.get_transport().auth_none(username) # without password
# execute reboot and capture the boot logs
stdin, stdout, stderr = ssh.exec_command("/sbin/reboot")
print(stdout.readlines())
print(stderr.readlines())
# check if reboot is done
exit_status = ''
msg = ''
while True:
if stdout_channel.exit_status_ready():
exit_status = ssh.stdout.channel.recv_exit_status()
print("Exit status: %s" % exit_status)
break
time.sleep(10)
ssh.close()
if exit_status == 0:
print("Reboot successful")
else:
print(Reboot not successful")
Logs are not captured and the following output is printed out before the machine finishing rebooting:
[]
[]
Exit status: 0
Reboot successful
Questions:
a) How can I capture the boot logs?
b) How to properly check for status after boot process is completed? Alternatively, I think I can ssh again and simply run a command after waiting some time for it to reboot.