473,289 Members | 2,155 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,289 software developers and data experts.

Using pexpect to configure cisco device..

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
0 8030

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

Similar topics

0
by: Steve Horsley | last post by:
I am trying to automate changing of a password for a particular user account (for use by scripts, not people). I am trying to use the command "passwd testuser" as root. Of course I can do this by...
3
by: Maurice LING | last post by:
I know this might sounds wierd but I'm wondering if I can use pexpect or os.popen3 function to invoke and control python interpreter to make it act like a python interpreter in python? maurice
0
by: Krutibas Biswal | last post by:
Hi, I am using a script 'unbuffer' for unbuffering my outputs when using pipes. This script is based on expect and looks like this : ----------------------------------------------...
0
by: Tim Thulin | last post by:
I am using Visual Basic 2005 and trying to discover a Cisco router/catalyst using SNMP. Does anyone have a good sample to start from and also what references should I be using with 2005? Thank...
0
by: dwelch91 | last post by:
I'm having a problem using pexpect with 'sudo' on Ubuntu 6.06 (Dapper). Here's the program: #!/usr/bin/env python import pexpect import sys child = pexpect.spawn("sudo apt-get update")...
0
by: indiarocks | last post by:
I need to send control+C after sending a particular command on my ssh session. How do I achieve this using Python 2.3 with pexpect? I tried using conn_handle.sendline(chr(3)), but that doesn't...
1
by: nehaz | last post by:
can anyone tell me how can i configure a device from HTTP page using perl ? I am using following cmd to enter the http page use HTTP::Request; $req = HTTP::Request->new($httpRequestMethod =>...
2
by: BlackjadeLin | last post by:
I'm new to python I want to write a simple script to switch user,for example,from user_A to user_B. This my codes: #!/usr/bin/python import pexpect import os passwd="user_B" child =...
2
by: yellowblueyellow | last post by:
Hey , I need to SSH into a server .. (10.8.42.38) using pexpect the username is 'admin' and password is 'abc123' so far i have the following code import pexpect import sys import time...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.