By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
439,930 Members | 2,018 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 439,930 IT Pros & Developers. It's quick & easy.

Using pexpect to configure cisco device..

P: 3
Hello All,

I havn't been programming with python too long, but decided the best way to learn it would be to dive right in. Right now I have been taking snippets here and there along with modifying some code in between to create some programs. The one I am currently doing requires me to ssh to a raritan, choose one of the cisco devices ( a router in this case) and shut down or bring up the interface based on the check boxes selected. My issue is that pexpect keeps timing out with an error when i go into the cisco device. I have a feeling it has something to do with the prompt expected, but I am not sure. Here is the code below of what I currently have. Any help or comments would be appreciated.

Make note, ignore the ip address field. Right now I hard coded everything just to ensure it works.
Expand|Select|Wrap|Line Numbers
  1. from Tkinter import *
  2. import os
  3. import getpass
  4. import sys
  5. import telnetlib
  6. import pexpect
  7.  
  8.  
  9.  
  10. class Application(Frame):
  11.  
  12.     """ GUI application """
  13.  
  14.     def __init__(self, master):
  15.  
  16.         """ Initialize Frame. """
  17.  
  18.         Frame.__init__(self, master)
  19.  
  20.         self.grid()
  21.  
  22.         self.create_widgets()
  23.  
  24.  
  25.  
  26.     def create_widgets(self):
  27.  
  28.         """ Create widgets """
  29.  
  30.         # create instruction label
  31.  
  32.         Label(self,
  33.  
  34.             text = "Connect to Device:\n"
  35.  
  36.             ).grid(row = 0, column = 0, columnspan = 2, sticky = W)
  37.  
  38.  
  39.         # create a label and text entry for IP Address
  40.  
  41.         Label(self,
  42.  
  43.             text = "IP Address: "
  44.  
  45.             ).grid(row = 1, column = 0, sticky = W)
  46.  
  47.         self.ipaddress = Entry(self)
  48.  
  49.         self.ipaddress.grid(row = 1, column = 1, sticky = W)
  50.  
  51.  
  52.         # create a label for student check buttons
  53.  
  54.         Label(self,
  55.  
  56.             text = "Student(s):"
  57.  
  58.             ).grid(row = 2, column = 0, sticky = W)
  59.  
  60.  
  61.  
  62.         # create check button
  63.  
  64.         self.student1 = BooleanVar()
  65.  
  66.         Checkbutton(self,
  67.  
  68.             text = "Student 1",
  69.  
  70.             variable = self.student1
  71.  
  72.             ).grid(row = 2, column = 1, sticky = W)
  73.  
  74.  
  75.         # create check button
  76.  
  77.         self.student2 = BooleanVar()
  78.  
  79.         Checkbutton(self,
  80.  
  81.             text = "Student 2",
  82.  
  83.             variable = self.student2
  84.  
  85.             ).grid(row = 2, column = 2, sticky = W)
  86.  
  87.  
  88.  
  89.         # create check button
  90.  
  91.         self.student3 = BooleanVar()
  92.  
  93.         Checkbutton(self,
  94.  
  95.             text = "Student 3",
  96.  
  97.             variable = self.student3
  98.  
  99.             ).grid(row = 3, column = 1, sticky = W)
  100.  
  101.  
  102.         # create check button
  103.  
  104.         self.student4 = BooleanVar()
  105.  
  106.         Checkbutton(self,
  107.  
  108.             text = "Student 4",
  109.  
  110.             variable = self.student4
  111.  
  112.             ).grid(row = 3, column = 2, sticky = W)
  113.  
  114.  
  115.  
  116.         # create a submit button = ON
  117.  
  118.         Button(self,
  119.  
  120.             text = "ON",
  121.  
  122.             command = self.on
  123.  
  124.             ).grid(row = 6, column = 1, sticky = W)
  125.  
  126.  
  127.  
  128.         # create a submit button = OFF
  129.  
  130.         Button(self,
  131.  
  132.             text = "OFF",
  133.  
  134.             command = self.off
  135.  
  136.             ).grid(row = 6, column = 2, sticky = W)
  137.  
  138.     def on(self):
  139.         print ssh.command("telnet 1.1.1.1")
  140.         print ssh.command("k") #goes to command line
  141.         print ssh.command("config t") #enters config mode
  142.  
  143.         if self.student1.get():
  144.             print ssh.command("int eth 0/18")
  145.             print ssh.command("shutdown")
  146.  
  147.         if self.student2.get():
  148.             comstu2 = "mkdir student2" 
  149.             print ssh.command(comstu2)
  150.  
  151.         if self.student3.get():
  152.             comstu3 = "mkdir student3" 
  153.             print ssh.command(comstu3)
  154.  
  155.         if self.student4.get():
  156.             comstu4 = "mkdir student4" 
  157.             print ssh.command(comstu4)
  158.  
  159.  
  160.  
  161.     def off(self):
  162.         ssh.close()
  163.         raw_input("Press Enter to Exit")
  164.  
  165. class SSH:
  166.     def __init__(self):
  167.         PROMPT = "\$|\%|\>"
  168.         password = "FILL IN"
  169.         self.child = pexpect.spawn("ssh -p 443 ------@1.1.1.1")
  170.         i = self.child.expect(['assword:', r"yes/no"], timeout=120)
  171.         if i==0:
  172.             self.child.sendline(password)
  173.         elif i==1:
  174.             self.child.sendline("yes")
  175.             self.child.expect("assword:", timeout=120)
  176.             self.child.sendline(password)
  177.             self.child.expect(PROMPT)
  178.  
  179.  
  180.     def command(self, command):
  181.         """send a command and return the response"""
  182.         PROMPT = "\$|\%|\>"
  183.         self.child.sendline(command)
  184.         self.child.expect(PROMPT)
  185.         response = self.child.before
  186.         return response
  187.  
  188.     def close(self):
  189.         """close the connection"""
  190.         self.child.close()
  191.  
  192.     def interact(self):
  193.         """interaction with the remote box"""
  194.         self.child.interact()
  195.  
  196.  
  197.  
  198.  
  199.  
  200. # main
  201. import os
  202. import getpass
  203. import sys
  204. import telnetlib
  205.  
  206. from Tkinter import *
  207.  
  208.  
  209. root = Tk()
  210.  
  211. root.title("Turn Firewall Mode ON/OFF")
  212. ssh = SSH()
  213.  
  214. app = Application(root)
  215.  
  216. root.mainloop()
Nov 6 '07 #1
Share this question for a faster answer!
Share on Google+

Post your reply

Sign in to post your reply or Sign up for a free account.