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. -
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()
0 8040 Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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
|
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 :
----------------------------------------------...
|
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...
|
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")...
|
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...
|
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 =>...
|
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 =...
|
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...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
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...
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
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,...
|
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...
|
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,...
|
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...
|
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...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
| |