I have a set of Cisco firewalls that I have to wipe all the data from. I want to automate this by using Python (on a Windows machine) and PuTTY Link (Plink) on the command line.
The problem I keep running into is when I try to log into the firewall's console with a username and password. When I directly access the console from my computer and put in the username and password, there's no problem and I get access. However, when I try to use my Python script, it doesn't work.
I am using the pexpect
module for Python, specifically the PopenSpawn
class that works on Windows. Here's the portion of my code that catches the username and password prompts and is supposed to send the corresponding input:
def login(shell, username, password):
...
shell.expect("Username: ")
sleep(1) # Inserted to keep input from going faster than the console
shell.sendline(username)
shell.expect("Password: ")
sleep(1) # ^^
shell.sendline(password)
The two shell.expect
function calls read the output from the firewall's console until they encounter the specific pattern they're looking for, which is "Username: "
and "Password: "
, respectively. Those work just fine.
The two shell.sendline
function calls are supposed to send a string as input to the firewall's console and then send a newline afterwards.
There's a call to sleep
in between to help buffer the input.
I have the shell
process set up so it prints the firewall's output to the screen as it executes. What I see when I run it is this:
[ a bunch of booting stuff... ]
Username: <username>
<password>
<username>
Password: <password>
where <username>
and <password>
are my actual credentials. I don't know why they come out that way, but it isn't working how it is right now.