Chris Hallman wrote:
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:

It looks pretty good to me. A few suggestions below.

> import telnetlib, re, os, string
> 
> dirpath = (r"c:\temp\sun")

The parens here and for wpath are not needed.

> dirlist = os.listdir(dirpath)
> wpath = (r"c:\temp\py\send_file4.out")
> output = file(wpath, "w")
> for i in dirlist:

i is not a very descriptive name.
You might want to check that i ends with '.txt'
device = i.rstrip('.txt')
might be handy too.

>     try:
>         tn = telnetlib.Telnet(i.rstrip(".txt"))
>         tn.read_until("Username: ")
>     except:
>         output.write(i.rstrip(".txt") + ": not responding.\n")
>         continue
>     tn.write("cworks\n")
>     (index, match, read) = tn.expect(["Password: "], 5)
>     if not match:
>         output.write(i.rstrip(".txt") + ": sign-on failure.\n")
>         continue
>     tn.write("<password_here>\n")
>     tn.write("conf t\n")
>     rpath = (dirpath + "\\" + i)

Use os.path.join(dirpath, i)

>     input = file(rpath, "r")

You don't use input

>     for lines in file(rpath):
>         tn.write(input.readline())
>     tn.write("end\n")    # exit config mode
>     tn.write("wr\n")    # save config
>     tn.read_until("#")
>     tn.write("wr net\n")    #write config to TFTP
>     tn.read_until("]? ")
>     tn.write("172.16.250.22\n")    # TFTP server address
>     tn.read_until("]? ")
>     tn.write("\n")
>     tn.read_until("[confirm]")
>     tn.write("\n")
>     tn.read_until("#")
>     tn.write("exit\n")    # end session

The above is pretty repetitive. How about something like
  finishCmds = [
    ("#",   "wr net\n"),    #write config to TFTP
    ("]? ", "172.16.250.22\n"),    # TFTP server address
    ("]? ", "\n"),
    ("[confirm]", "\n"),
    ("#",   "exit\n"),    # end session
  ]
  for prompt, response in finishCmds:
    tn.read_until(prompt)
    tn.write(response)

Kent

_______________________________________________
Tutor maillist  -  [email protected]
http://mail.python.org/mailman/listinfo/tutor

Reply via email to