473,396 Members | 1,996 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,396 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 8039

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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.