Jan-27-2018, 10:09 PM
Hello there.
I am brand new to python and this is my first project.
Essentially I the script will allow a user to select a file from their computer that contains a list of IP's. The script will then take that list connect to each device on the list and monitor for a specific error. If the error is present then it will send a command to power cycle the device.
The issue is that when I select a file and it monitors for errors I loose all control on my GUI.
I was hoping to add more buttons, like a start and stop etc but I am stuck. Really not sure why the GUI locks up. I assume that it is waiting for my while loop to end. Is there a way to maintain control from the GUI while running a while loop.
Here is a copy of my code:
import telnetlib
import socket
from Tkinter import *
import Tkinter, Tkconstants, tkFileDialog
import time
root = Tk()
def fileopen():
Tkinter.Tk().withdraw() # Close the root window
inpath = tkFileDialog.askopenfilename()
return inpath
def NoFault():
#tn.write("exit\n")
#print "session closed"
print "Time out"
def main():
#t = Timer(3.0, NoFault)
#t.start()
#read file
#file = open("hostnamelist.txt", "r")
#lines = file.readline(2)
#for line in lines:
#line = line.strip()
fp = open(fileopen()) # open file on read mode
lines = fp.read().split("\n") # create a list containing all lines
print lines
#IpAddr = lines
#for i in range(len(IpAddr)):
#print(IpAddr[i])
IpAddr = lines
i = 0
while i <= len(IpAddr):
print "Connecting to...",IpAddr[i]
#i = i + 1
sck = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
if sck.connect_ex((IpAddr[i], 23)) == 0:
print "Connection established with ", IpAddr[i] ## Telnet is open do telnet related stuff here
#sck.close()
HOST = IpAddr[i]
tn = telnetlib.Telnet(HOST, "23")
tn.read_until("Welcome to the Tesira Text Protocol Server...")
tn.write("DEVICE get activeFaultList\r\n")
tn.write("exit\n")
TelnetOut = tn.read_all()
#print TelnetOut
print "Device checked at "
if TelnetOut.find("Unable to communicate with the IO card") == -1:
print "Specified Error not found"
print"_____________________________________________________________________"
else:
print "***Found error on device!***"
tn.open(HOST)
tn.read_until("Welcome to the Tesira Text Protocol Server...")
time.sleep(1)
tn.write("DEVICE recallPresetByName \"***Card slot error found device rebooted***\"\r\n")
time.sleep(3)
tn.write("DEVICE reboot\r\n")
tn.write("exit\n")
print "Device rebooted"
print"_____________________________________________________________________"
ActiveFaults = tn.write("DEVICE get activeFaultList\r\n")
DeviceSerial = tn.write("DEVICE get serialNumber\r\n")
else:
print"Could not connect to device at IP ", IpAddr[i]
print"_____________________________________________________________________"
i = i + 1
if i >= len(IpAddr):
print"*********************************************************************"
print"The following IP's have been checked"
print IpAddr
print"Completed "
print"*********************************************************************"
sck.close()
time.sleep(160)
i = 0
print len(IpAddr)
fp.close() # close file
print lines
file.close()
telnet = lines
menubar = Menu(root)
Start = Tkinter.Button(text ="Run Tesira Error Monitor", command = main)
Start.pack()
root.config(menu=menubar)
root.mainloop()
main()
I am brand new to python and this is my first project.
Essentially I the script will allow a user to select a file from their computer that contains a list of IP's. The script will then take that list connect to each device on the list and monitor for a specific error. If the error is present then it will send a command to power cycle the device.
The issue is that when I select a file and it monitors for errors I loose all control on my GUI.
I was hoping to add more buttons, like a start and stop etc but I am stuck. Really not sure why the GUI locks up. I assume that it is waiting for my while loop to end. Is there a way to maintain control from the GUI while running a while loop.
Here is a copy of my code:
import telnetlib
import socket
from Tkinter import *
import Tkinter, Tkconstants, tkFileDialog
import time
root = Tk()
def fileopen():
Tkinter.Tk().withdraw() # Close the root window
inpath = tkFileDialog.askopenfilename()
return inpath
def NoFault():
#tn.write("exit\n")
#print "session closed"
print "Time out"
def main():
#t = Timer(3.0, NoFault)
#t.start()
#read file
#file = open("hostnamelist.txt", "r")
#lines = file.readline(2)
#for line in lines:
#line = line.strip()
fp = open(fileopen()) # open file on read mode
lines = fp.read().split("\n") # create a list containing all lines
print lines
#IpAddr = lines
#for i in range(len(IpAddr)):
#print(IpAddr[i])
IpAddr = lines
i = 0
while i <= len(IpAddr):
print "Connecting to...",IpAddr[i]
#i = i + 1
sck = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
if sck.connect_ex((IpAddr[i], 23)) == 0:
print "Connection established with ", IpAddr[i] ## Telnet is open do telnet related stuff here
#sck.close()
HOST = IpAddr[i]
tn = telnetlib.Telnet(HOST, "23")
tn.read_until("Welcome to the Tesira Text Protocol Server...")
tn.write("DEVICE get activeFaultList\r\n")
tn.write("exit\n")
TelnetOut = tn.read_all()
#print TelnetOut
print "Device checked at "
if TelnetOut.find("Unable to communicate with the IO card") == -1:
print "Specified Error not found"
print"_____________________________________________________________________"
else:
print "***Found error on device!***"
tn.open(HOST)
tn.read_until("Welcome to the Tesira Text Protocol Server...")
time.sleep(1)
tn.write("DEVICE recallPresetByName \"***Card slot error found device rebooted***\"\r\n")
time.sleep(3)
tn.write("DEVICE reboot\r\n")
tn.write("exit\n")
print "Device rebooted"
print"_____________________________________________________________________"
ActiveFaults = tn.write("DEVICE get activeFaultList\r\n")
DeviceSerial = tn.write("DEVICE get serialNumber\r\n")
else:
print"Could not connect to device at IP ", IpAddr[i]
print"_____________________________________________________________________"
i = i + 1
if i >= len(IpAddr):
print"*********************************************************************"
print"The following IP's have been checked"
print IpAddr
print"Completed "
print"*********************************************************************"
sck.close()
time.sleep(160)
i = 0
print len(IpAddr)
fp.close() # close file
print lines
file.close()
telnet = lines
menubar = Menu(root)
Start = Tkinter.Button(text ="Run Tesira Error Monitor", command = main)
Start.pack()
root.config(menu=menubar)
root.mainloop()
main()