<br>Here is my script:<br><br>send_file.py<br><br><br>import os, random, re, string, sys, telnetlib, threading, time<br>from time import strftime<br>from threading import Thread<br><br><br>class ConfigIT(threading.Thread):
<br> def __init__(self,host):<br> Thread.__init__(self)<br> self.host = host<br> self.filename = filename<br><br> def run(self):<br> try:<br> tn = telnetlib.Telnet(self.host)<br>
tn.read_until("Username: ", 7)<br> except:<br> output.write(self.host + " - not responding.\n")<br> output.flush()<br> return<br> tn.write(user + "\n")
<br> tn.read_until("Password: ", 7)<br> tn.write(pswd + "\n")<br> (index, match, read) = tn.expect(["#"], 7)<br> if not match:<br> output.write(self.host
+ " - sign-on failure.\n")<br> output.flush()<br> return<br> tn.write("conf t\n")<br> rpath = (dirPath + "\\" + self.filename)<br> print rpath<br> input = file(rpath, "r")
<br> for line in file(rpath):<br> time.sleep(random.uniform(0,2))<br> text = input.readline()<br> tn.write(text)<br> (index, match, read) = tn.expect(["%"], 1)<br>
if match:<br> output.write(self.host + " - config error on line = " + text)<br> output.flush()<br> return<br> tn.write("end\n")<br>
tn.read_until(self.host.upper() + "#")<br> time.sleep(random.uniform(0,2))<br># write config to NVRAM<br> tn.write("wr\n")<br> tn.read_until(self.host.upper() + "#")<br>
time.sleep(random.uniform(0,2))<br># write config to TFTP<br> tn.write("wr net\n")<br> tn.read_until("]?")<br> time.sleep(random.uniform(0,2))<br> tn.write("192.168.19.201\n
")<br> tn.read_until("]?")<br> time.sleep(random.uniform(0,2))<br> tn.write("\n")<br> tn.read_until("[confirm]")<br> time.sleep(random.uniform(0,2))<br>
tn.write("\n")<br> (index, match, read) = tn.expect(["OK"], 15)<br> if "Error opening tftp" in read:<br> output.write(self.host + " - config write net failed.\n")
<br> output.flush()<br> return<br># if host = *swa, then write vlan.dat to SYSMAN<br> m = match_swa.match(self.host)<br> if m:<br> tn.write("copy flash tftp\n")<br>
tn.read_until("]? ")<br> time.sleep(random.uniform(0,2))<br> tn.write("vlan.dat\n")<br> tn.read_until("]? ")<br> time.sleep(random.uniform
(0,2))<br> tn.write("192.168.19.201\n")<br> tn.read_until("dat]? ")<br> time.sleep(random.uniform(0,2))<br> tn.write(self.host + ".vlan.dat\n")<br>
(index, match, read) = tn.expect(["OK"], 15)<br> if "Error opening tftp" in read:<br> output.write(self.host + " - VLAN write net failed.\n")<br>
output.flush()<br> return<br> tn.write("exit\n")<br><br>def count_active():<br> """ returns the number of Getter threads that are alive """<br> num_active = 0
<br> for thread in tlist:<br> if thread.isAlive():<br> num_active += 1<br> return num_active<br><br>threads = []<br>dirPath = (r"c:\temp\config")<br>dirList = os.listdir(dirPath)<br>match_swa =
re.compile('^s\d\d\d\d[s][w][a]')<br>Max_Threads = 10<br>pswd = "Activ8"<br>tlist = []<br>user = "cworks"<br>logFile = (r"c:\logs\send_file.log")<br>output = file(logFile, "a")<br>output.write
("\n" + "send_file script started -" + strftime(" %H:%M:%S %x") + "\n")<br>output.flush()<br><br>for file in dirList:<br> filename = file.lower()<br> host = filename.replace('.txt', '')
<br> while count_active() >= Max_Threads:<br> time.sleep(1)<br> threads = ConfigIT(host)<br> tlist.append(threads)<br> threads.start()<br><br>for thread in tlist:<br> thread.join()<br><br>output.write
("send_file script completed -" + strftime(" %H:%M:%S %x") + "\n")<br>output.flush()<br>output.close()<br><br><br><span style="color: rgb(255, 0, 0); font-weight: bold;">Here is the error:</span>
<br style="color: rgb(255, 0, 0); font-weight: bold;"><br style="color: rgb(255, 0, 0); font-weight: bold;">>pythonw -u "send_file.py"<br>c:\temp\config\rtr0544.txt<br>Exception in thread Thread-1:<br>Traceback (most recent call last):
<br> File "c:\python24\lib\threading.py", line 442, in __bootstrap<br> self.run()<br> File "send_file.py", line 45, in run<br> input = file(rpath, "r")<br>TypeError: 'str' object is not callable
<br><br>>Exit code: 0<br><br><br><br><br>The above script was created using the following script:<br><br>send_file.r1.py<br><br>#<br># This script obtains a directory listing, strips the extensions and telnets to the<br>
# device (which is the filename in the directory). Then it writes the commands in the<br># file to the device, saves config and writes it back to SYSMAN. It can be run using:<br># python send_file.py<br>#<br># Note: "os" is imported for future functionality.
<br>#<br># by: TCDH<br># on: 10/17/05<br># revised: 10/18/05 TCDH - Added logic to check for offline devices and sign-on failures.<br>#<br><br>import os, re, string, sys, telnetlib, threading, time<br>from time import strftime
<br>from threading import Thread<br><br>dirPath = (r"c:\temp\config")<br>dirList = os.listdir(dirPath)<br>pswd = "Activ8"<br>user = "cworks"<br>logFile = (r"c:\logs\send_file.log")<br>
output = file(logFile, "a")<br>output.write("\n" + "send_file script started -" + strftime(" %H:%M:%S %x") + "\n")<br>output.flush()<br><br>class ConfigIT(threading.Thread
):<br> def __init__(self,host):<br> Thread.__init__(self)<br> self.host = host<br> self.filename = filename<br><br> def run(self):<br> try:<br> tn = telnetlib.Telnet(self.host)
<br> tn.read_until("Username: ", 7)<br> except:<br> output.write(self.host + ": not responding.\n")<br> return<br> tn.write(user + "\n")<br>
tn.read_until("Password: ", 7)<br> tn.write(pswd + "\n")<br> (index, match, read) = tn.expect(["#"], 7)<br> if not match:<br> output.write(self.host + ": sign-on failure.\n")
<br> return<br> tn.write("conf t\n")<br> rpath = (dirPath + "\\" + self.filename)<br> print rpath<br> input = file(rpath, "r")<br> for line in file(rpath):
<br> tn.write(input.readline())<br> tn.write("end\n")<br> tn.read_until(self.host.upper() + "#")<br> tn.write("wr\n")<br> tn.read_until(self.host.upper() + "#")
<br> tn.write("wr net\n")<br> tn.read_until("host []?")<br> tn.write("192.168.19.201\n")<br> tn.read_until("confg]? ")<br> tn.write("\n")
<br> tn.read_until("[confirm]")<br> tn.write("\n")<br> (index, match, read) = tn.expect(["OK"], 15)<br> if "Error opening tftp" in read:<br> output.write
(self.host + ": write net failed.\n")<br> return<br> tn.write("exit\n")<br><br>tlist = []<br><br>for host in dirList:<br> filename = host.lower()<br> host = host.lower()<br> host =
host.replace('.txt', '')<br> current = ConfigIT(host)<br> tlist.append(current)<br> current.start()<br><br>for entry in tlist:<br> entry.join()<br><br>output.write("send_file script completed -" + strftime(" %H:%M:%S %x") + "\n")
<br>output.flush()<br>output.close()<br><br><br>They both use the same syntax to open the input file, however only send_file.py receives an error. What gives?<br><br><br>