Using Python Exscript need to create regular expression for set_prompt() for cisco switch

355 Views Asked by At

Having a hard time figuring out how to write a regular expression in python/exscript so that the prompt matches the output when i run "copy run tftp"...

For example the prompt changes to...

"Address or name of remote host []?"

then to...

"Destination filename [lab-3560.confg]?"

I know I need to set the "set_prompt()" prior to executing the command conn.execute('copy run tftp') just no clue on the proper syntax(s)

1

There are 1 best solutions below

1
On

There are so many ways to do this, here is an example:

So all you need to do is parse the returned prompt/text, here is a small example from this link:

import pexpect

switch_ip = "10.0.0.1"
switch_un = "user"
switch_pw = "s3cr3t"
switch_port = "Gi2/0/2"
switch_vlan = 300
config = "lab-3560.confg"

child = pexpect.spawn('ssh %s@%s' % (switch_un, switch_ip))
child.logfile = sys.stdout
child.timeout = 4
child.expect('Address or name of remote host []?')
child.sendline(switch_ip)
child.expect('Destination filename [lab-3560.confg]?')
child.sendline(config)