Ok so I'm trying to write a script to save the config on my network devices. I'm going wrong somewhere and I'm not sure where. It won't send the final confirmation "y" character, but I think it's due to what I'm telling it to expect.
Current code:
import pexpect
import sys
import os
import time
import getpass
hostname = "switch1"
username = raw_input('Enter Your username: ')
password = getpass.getpass('Password:')
fout = file('mylog.txt','w')
child = pexpect.spawn('ssh %s@%s' % (username, hostname))
child.logfile_read = fout
child.expect('myusername@%s\'s password:' % hostname)
child.sendline(password)
child.expect('>')
child.sendline('enable')
child.expect('Password:')
child.sendline(password)
child.expect('#')
child.sendline('wr mem')
child.expect("Are you sure you want to save? (y/n) ")
child.sendline('y')
child.expect('#')
child.sendline('logout')
I've also tried this line which has produced the same result:
child.expect("\r\n\r\nThis operation may take a few minutes.\r\nManagement interfaces will not be available during this time.\r\n\r\nAre you sure you want to save? (y/n) ")
Here is what it looks like on my switch:
(switch1) #write mem
This operation may take a few minutes.
Management interfaces will not be available during this time.
Are you sure you want to save? (y/n) y
Config file 'startup-config' created successfully .
Configuration Saved!
(switch1) #
Here is the error I get when I run the script:
python wrmem.py
Enter Your username: myusername
Password:
Traceback (most recent call last):
File "wrmem.py", line 35, in <module>
child.expect("Are you sure you want to save? (y/n) ")
File "/usr/local/lib/python2.7/dist-packages/pexpect/spawnbase.py", line 327, in expect
timeout, searchwindowsize, async_)
File "/usr/local/lib/python2.7/dist-packages/pexpect/spawnbase.py", line 355, in expect_list
return exp.expect_loop(timeout)
File "/usr/local/lib/python2.7/dist-packages/pexpect/expect.py", line 104, in expect_loop
return self.timeout(e)
File "/usr/local/lib/python2.7/dist-packages/pexpect/expect.py", line 68, in timeout
raise TIMEOUT(msg)
pexpect.exceptions.TIMEOUT: Timeout exceeded.
<pexpect.pty_spawn.spawn object at 0x7f5d9c82af90>
command: /usr/bin/ssh
args: ['/usr/bin/ssh', 'myusername@switch1']
buffer (last 100 chars): 'nagement interfaces will not be available during this time.\r\n\r\nAre you sure you want to save? (y/n) '
before (last 100 chars): 'nagement interfaces will not be available during this time.\r\n\r\nAre you sure you want to save? (y/n) '
after: <class 'pexpect.exceptions.TIMEOUT'>
match: None
match_index: None
exitstatus: None
flag_eof: False
pid: 22641
child_fd: 6
closed: False
timeout: 30
delimiter: <class 'pexpect.exceptions.EOF'>
logfile: None
logfile_read: <open file 'mylog.txt', mode 'w' at 0x7f5d9ae0c150>
logfile_send: None
maxread: 2000
ignorecase: False
searchwindowsize: None
delaybeforesend: 0.05
delayafterclose: 0.1
delayafterterminate: 0.1
searcher: searcher_re:
0: re.compile("Are you sure you want to save? (y/n) ")
Again I think it has to do with the expect "are you sure you want to save" line but I'm not sure. I can confirm logging into the switch and other commands work as I have other scripts, it's just this one I can't figure out. Any help is appreciated.
Check the log:
searcher: searcher_re:
0: re.compile("Are you sure you want to save? (y/n) ")
Compiling the "Are you sure you want to save? (y/n) "
as regex will get ?, (, / and )
as special characters so try to scape them like this:
"Are you sure you want to save\? \(y\/n\) "
which will match the text "Are you sure you want to save? (y/n) "
Check this
So change this line to:
child.expect("Are you sure you want to save\? \(y\/n\) ")