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
- from Tkinter import *
- import os
- import getpass
- import sys
- import telnetlib
- import pexpect
- class Application(Frame):
- """ GUI application """
- def __init__(self, master):
- """ Initialize Frame. """
- Frame.__init__(self, master)
- self.grid()
- self.create_widgets()
- def create_widgets(self):
- """ Create widgets """
- # create instruction label
- Label(self,
- text = "Connect to Device:\n"
- ).grid(row = 0, column = 0, columnspan = 2, sticky = W)
- # create a label and text entry for IP Address
- Label(self,
- text = "IP Address: "
- ).grid(row = 1, column = 0, sticky = W)
- self.ipaddress = Entry(self)
- self.ipaddress.grid(row = 1, column = 1, sticky = W)
- # create a label for student check buttons
- Label(self,
- text = "Student(s):"
- ).grid(row = 2, column = 0, sticky = W)
- # create check button
- self.student1 = BooleanVar()
- Checkbutton(self,
- text = "Student 1",
- variable = self.student1
- ).grid(row = 2, column = 1, sticky = W)
- # create check button
- self.student2 = BooleanVar()
- Checkbutton(self,
- text = "Student 2",
- variable = self.student2
- ).grid(row = 2, column = 2, sticky = W)
- # create check button
- self.student3 = BooleanVar()
- Checkbutton(self,
- text = "Student 3",
- variable = self.student3
- ).grid(row = 3, column = 1, sticky = W)
- # create check button
- self.student4 = BooleanVar()
- Checkbutton(self,
- text = "Student 4",
- variable = self.student4
- ).grid(row = 3, column = 2, sticky = W)
- # create a submit button = ON
- Button(self,
- text = "ON",
- command = self.on
- ).grid(row = 6, column = 1, sticky = W)
- # create a submit button = OFF
- Button(self,
- text = "OFF",
- command = self.off
- ).grid(row = 6, column = 2, sticky = W)
- def on(self):
- print ssh.command("telnet 1.1.1.1")
- print ssh.command("k") #goes to command line
- print ssh.command("config t") #enters config mode
- if self.student1.get():
- print ssh.command("int eth 0/18")
- print ssh.command("shutdown")
- if self.student2.get():
- comstu2 = "mkdir student2"
- print ssh.command(comstu2)
- if self.student3.get():
- comstu3 = "mkdir student3"
- print ssh.command(comstu3)
- if self.student4.get():
- comstu4 = "mkdir student4"
- print ssh.command(comstu4)
- def off(self):
- ssh.close()
- raw_input("Press Enter to Exit")
- class SSH:
- def __init__(self):
- PROMPT = "\$|\%|\>"
- password = "FILL IN"
- self.child = pexpect.spawn("ssh -p 443 ------@1.1.1.1")
- i = self.child.expect(['assword:', r"yes/no"], timeout=120)
- if i==0:
- self.child.sendline(password)
- elif i==1:
- self.child.sendline("yes")
- self.child.expect("assword:", timeout=120)
- self.child.sendline(password)
- self.child.expect(PROMPT)
- def command(self, command):
- """send a command and return the response"""
- PROMPT = "\$|\%|\>"
- self.child.sendline(command)
- self.child.expect(PROMPT)
- response = self.child.before
- return response
- def close(self):
- """close the connection"""
- self.child.close()
- def interact(self):
- """interaction with the remote box"""
- self.child.interact()
- # main
- import os
- import getpass
- import sys
- import telnetlib
- from Tkinter import *
- root = Tk()
- root.title("Turn Firewall Mode ON/OFF")
- ssh = SSH()
- app = Application(root)
- root.mainloop()