Hi,
I am having some issues writing a basic python script to configure access switches in mass.
The process I am trying to follow is –
Connect the switches to the lab network, issue IP address from a DHCP server and use auto config to load a base config (set the username, enable local login).
The python script will do the following-
Read the IP list file
Log in to the devices one by one
Update firmware (must still be completed)
Optionally load a config update file
Load a custom config file (based on device sn)
Gen new rsa key
Save config
Backs up new running config.
If I write this, one command following on the other it seems to work.
However. In order to provide more flexibility, I would prefer to write separate definitions and call these as I need them. This works to a limited extent.
The process to call the individual definitions works. The expect statements do not seem to function beyond the first definition that I call. My suspicion is that the expect telnet instance gets interrupted and do not continue to function as too as the first definition is completed.
How do I get the pexpect child statement to work on the subsequent definitions?
regards
It looks like you're new here. If you want to get involved, click one of these buttons!
Comments
def login_to_node ():
use Telnet
try:
child = pexpect.spawn('telnet %s' % (host))
child.expect('Username:')
child.sendline(switch_username)
child.expect('Password:')
child.sendline(switch_telnet_password)
child.expect('#')
print(("Successfully Telnetted to host "), (host))
except:
try:
#child.expect('>')
child.sendline('enable')
child.expect('Password:')
child.sendline(switch_enable_password)
child.expect('#')
print(("Successfully Telneted to host "), (host), (',and enabled console'))
except:
print(("Failed to Telnet to host "), (host))
return
load custom config
def load_custom_config():
try:
# get unit info
# child = pexpect.spawn()
child.sendline('show version | include Processor')
child.expect('(?:Processor\sboard\sID\s)(\d|\w{11})')
device_id = child.after[-11:]
serial_number = device_id
print(("This is Device-ID "), (device_id))
print(("This is Device-SN "), (serial_number))
config_file ='%s.cfg' % serial_number
except:
print(("Failed to copy files to flash "), (host))
return
generate new crypto key
def generate_rsa_key ():
# check current key
# ?
# generate new key
try:
# child = pexpect.spawn()
child.sendline('crypto key generate rsa')
child.expect('(512)')
child.sendline(rsa_key_length)
child.expect('#')
print(("Successfully generated new RSA key on host "), (host))
except:
print(("RSA key gen failed on host "), (host))
return
save running config
def save_running_config():
try:
#child = pexpect.spawn()
child.expect('#')
child.sendline('write memory')
child.expect('[OK]')
child.expect('#')
print(("Successfully saved the running-config on "), (host))
except:
print(("Failed to save the running-config on "), (host))
#return
for line in host_list:
host = line.split()[0]
print(("Start configuration on host "), (host))
platform =None
hardware_model = None
firmware = None
flash_available = None
device_id = None
serial_number = None
config_file = None
login_to_node ()
load_custom_config ()
generate_rsa_key ()
save_running_config ()