3
votes

Background: I am trying to automate the rebooting of a server when PXE obtains an IP address. This is to reproduce an issue, and the only way to reproduce is to cold boot it at the exact same time every reboot. I'm tired of doing this manually and have spent probably 10 hours on this script and troubleshooting so far..

I am trying to read lines from a serial console on a server, while looking for a certain string, then issuing a reboot command.

Right now, the only way I can get this script to echo what is coming on the serial console is to power off the server, start minicom, power on the server, when text starts then I can exit minicom without a reset, then start my script.

The first time through, the script works fine, and even the iLO commands at the end work, then it restarts the while loop, but then I don't get any output from the console anymore.

It seems that I am either not opening the serial port correctly, but I print get_settings and the baudrate, stopbits, etc, are all correct.

I have searched and used snippets of code from many different places to hack together this script, and am really getting frustrated that I can't get this to work on its own.

[root@localhost ~]# python2 bootorder.py 
{'parity': 'N', 'baudrate': 115200, 'bytesize': 8, 'xonxoff': False, 'rtscts': False, 'timeout': None, 'inter_byte_timeout': None, 'stopbits': 1, 'dsrdtr': False, 'write_timeout': None}

As you can see above, when I have it run, I have the serial port settings printed out, and they match minicom, and the serial console on the server side.

So what is minicom doing to open the port that I'm not doing in the script? I have followed examples from many sites, and it does work at times, I just can't figure out how to make this work on its own.

Here is my script:

#!/usr/bin/python

import io
import hpilo
import os
import sys
import time
import serial
from datetime import datetime

outfile='/tmp/bootordercount.txt'
# configure the serial connections (the parameters differs on the device you    are connecting to)
port = '/dev/ttyS0'
ser = serial.Serial(port,115200,timeout=None)
cycles = 1

if ser.isOpen(): ser.close()
ser.open()

if ser.isOpen():

    try:
#        ser.flushInput() #flush input buffer, discarding all its contents
#        ser.flushOutput()#flush output buffer, aborting current output 
                 #and discard all that is in buffer
    print(ser.get_settings())
    with open(outfile, 'a') as f:

       while ser.isOpen():
           line = ser.readline()
           print line
           if "CLIENT IP:" in line: 
                print "Client string seen!"
                ilo = hpilo.Ilo('10.0.8.203', 'administrator', 'password') #configure ilo function
                ilo.cold_boot_server() #cold boot the server
                print cycles
   #            f.write(datetime.utcnow().isoformat() + '\t' + cycles + '\n')
   #            f.flush()
                cycles += 1

    except Exception, e1:
        print "error communicating...: " + str(e1)
        ser.close()

Thanks for your input and help!

1
Also I think the readline part is right, since it works one time through correctly, but after it cycles, I quit getting data from the serial port, or readline stops working.. Not sure which..John Julian

1 Answers

1
votes

It might save something to do with the other lines in the serial port: The DTR, DSR, etc. Their use is often inconsistent, and they are often used for other purposes than they are intended for.

Maybe minicom uses the DTR to initialize the connection. Try adding this after the serial open.

s.setDTR(False)
sleep(0.025)
s.setDTR(True)