<br>
I created a script that parses a directory, strips the &quot;.txt&quot;
extension off the file, telnet &amp; log on to the device (the filename
is the device name), opens the associated &quot;.txt&quot; file, sends all the
commands containted in the file to the device and then executes some
commands to save the changes. I'm not very experienced with
programming, writing Python, using functions, OOP, etc., therefore I'd like to
know if there is a better way to do this:<br>
<br>
#<br>
# This scripts 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>
#&nbsp;&nbsp;&nbsp; python send_file4.py<br>
#<br>
# Note: &quot;os&quot; is imported for future functionality.<br>
#<br>
# by: TCDH<br>
# on: 10/17/05<br>
# revised: 10/18/05&nbsp;&nbsp;&nbsp; TCDH - Added logic to check for offline devices and sign-on failures.<br>
#<br>
<br>
import telnetlib, re, os, string<br>
<br>
dirpath = (r&quot;c:\temp\sun&quot;)<br>
dirlist = os.listdir(dirpath)<br>
wpath = (r&quot;c:\temp\py\send_file4.out&quot;)<br>
output = file(wpath, &quot;w&quot;)<br>
for i in dirlist:<br>
&nbsp;&nbsp;&nbsp; try:<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; tn = telnetlib.Telnet(i.rstrip(&quot;.txt&quot;))<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; tn.read_until(&quot;Username: &quot;)<br>
&nbsp;&nbsp;&nbsp; except:<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; output.write(i.rstrip(&quot;.txt&quot;) + &quot;: not responding.\n&quot;)<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; continue<br>
&nbsp;&nbsp;&nbsp; tn.write(&quot;cworks\n&quot;)<br>
&nbsp;&nbsp;&nbsp; (index, match, read) = tn.expect([&quot;Password: &quot;], 5)<br>
&nbsp;&nbsp;&nbsp; if not match:<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; output.write(i.rstrip(&quot;.txt&quot;) + &quot;: sign-on failure.\n&quot;)<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; continue<br>
&nbsp;&nbsp;&nbsp; tn.write(&quot;&lt;password_here&gt;\n&quot;)<br>
&nbsp;&nbsp;&nbsp; tn.write(&quot;conf t\n&quot;)<br>
&nbsp;&nbsp;&nbsp; rpath = (dirpath + &quot;\\&quot; + i)<br>
&nbsp;&nbsp;&nbsp; input = file(rpath, &quot;r&quot;)<br>
&nbsp;&nbsp;&nbsp; for lines in file(rpath):<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; tn.write(input.readline())<br>
&nbsp;&nbsp;&nbsp; tn.write(&quot;end\n&quot;)&nbsp;&nbsp;&nbsp; # exit config mode<br>
&nbsp;&nbsp;&nbsp; tn.write(&quot;wr\n&quot;)&nbsp;&nbsp;&nbsp; # save config<br>
&nbsp;&nbsp;&nbsp; tn.read_until(&quot;#&quot;)<br>
&nbsp;&nbsp;&nbsp; tn.write(&quot;wr net\n&quot;)&nbsp;&nbsp;&nbsp; #write config to TFTP<br>
&nbsp;&nbsp;&nbsp; tn.read_until(&quot;]? &quot;)<br>
&nbsp;&nbsp;&nbsp; tn.write(&quot;172.16.250.22\n&quot;)&nbsp;&nbsp;&nbsp; # TFTP server address<br>
&nbsp;&nbsp;&nbsp; tn.read_until(&quot;]? &quot;)<br>
&nbsp;&nbsp;&nbsp; tn.write(&quot;\n&quot;)<br>
&nbsp;&nbsp;&nbsp; tn.read_until(&quot;[confirm]&quot;)<br>
&nbsp;&nbsp;&nbsp; tn.write(&quot;\n&quot;)<br>
&nbsp;&nbsp;&nbsp; tn.read_until(&quot;#&quot;)<br>
&nbsp;&nbsp;&nbsp; tn.write(&quot;exit\n&quot;)&nbsp;&nbsp;&nbsp; # end session<br>
<br>
This script needs to be able to log which device is offline and if
there was a sign-on problem. For the first exception (device offline),
I had to use try-except because tn.expect wouldn't work. However, I had
to use tn.expect on the second exception (sign-on problem) because
try-except wouldn't work there.<br>
<br>