Skip to content

Instantly share code, notes, and snippets.

@upa
Last active April 28, 2016 10:21
Show Gist options
  • Select an option

  • Save upa/019e36deb4dc4861dcf3bab0d554222d to your computer and use it in GitHub Desktop.

Select an option

Save upa/019e36deb4dc4861dcf3bab0d554222d to your computer and use it in GitHub Desktop.
a simple single command config fetcher
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# kumade version 2.
# a simple single command config fetcher
import sys
import pexpect
from optparse import OptionParser
def print_pexpect_stdout (p, fo) :
fo.write (p.before)
fo.write (p.buffer)
fo.write (p.after)
return
def juniper (target, options, fo) :
username = options.username
password = options.password
p = pexpect.spawn ("telnet %s" % target)
fo.write ("!!!! Start Kumade2 !!!!\n")
fo.write ("!!!! login to %s !!!!\n" % target)
p.expect ("login: ")
print_pexpect_stdout (p, fo)
p.sendline ("%s" % username)
p.expect ("Password:")
print_pexpect_stdout (p, fo)
p.sendline ("%s" % password)
p.expect ("> ")
print_pexpect_stdout (p, fo)
fo.write ("\n")
fo.write ("!!!! set pager disable !!!!\n")
p.sendline ("set cli screen-length 0")
p.expect ("> ")
print_pexpect_stdout (p, fo)
fo.write ("\n")
fo.write ("!!!! show configuration !!!!\n")
p.sendline ("show configuration")
p.expect ("> ")
print_pexpect_stdout (p, fo)
p.sendline ("exit")
p.close ()
fo.write ("\n")
fo.write ("!!!! End of Kumade2 !!!!\n")
return
def alaxala (target, options, fo) :
username = options.username
password = options.password
p = pexpect.spawn ("telnet %s" % target)
fo.write ("!!!! Start Kumade2 !!!!\n")
fo.write ("!!!! login to %s !!!!\n" % target)
p.expect ("login: ")
print_pexpect_stdout (p, fo)
p.sendline ("%s" % username)
p.expect ("Password:")
print_pexpect_stdout (p, fo)
p.sendline ("%s" % password)
if not options.auto_enable :
# no auto enable. prompt is '>' and send password.
p.expect ("> ")
print_pexpect_stdout (p, fo)
p.sendline ("enable")
p.expect ("Password:")
print_pexpect_stdout (p, fo)
p.sendline ("%s" % password)
p.expect ("# ")
print_pexpect_stdout (p, fo)
fo.write ("\n")
fo.write ("!!!! set pager disable !!!!\n")
p.sendline ("set terminal pager disable")
p.expect ("# ")
print_pexpect_stdout (p, fo)
fo.write ("\n")
fo.write ("!!!! show configuration !!!!\n")
p.sendline ("show running-config")
p.expect ("# ")
print_pexpect_stdout (p, fo)
p.sendline ("exit")
p.close ()
fo.write ("\n")
fo.write ("!!!! End of Kumade2 !!!!\n")
return
def wlc (target, options, fo) :
username = options.username
password = options.password
p = pexpect.spawn ("telnet %s" % target)
fo.write ("!!!! Start Kumade2 !!!!\n")
fo.write ("!!!! login to %s !!!!\n" % target)
p.expect ("User: ")
print_pexpect_stdout (p, fo)
p.sendline ("%s" % username)
p.expect ("Password:")
print_pexpect_stdout (p, fo)
p.sendline ("%s" % password)
p.expect (" >")
print_pexpect_stdout (p, fo)
fo.write ("\n")
fo.write ("!!!! set pager disable !!!!\n")
p.sendline ("config paging disable")
p.expect (" >")
print_pexpect_stdout (p, fo)
fo.write ("\n")
fo.write ("!!!! show configuration !!!!\n")
p.sendline ("show run-config commands")
p.expect (" >")
print_pexpect_stdout (p, fo)
p.sendline ("logout")
p.close ()
fo.write ("\n")
fo.write ("!!!! End of Kumade2 !!!!\n")
return
def yamaha (target, options, fo) :
username = options.username
password = options.password
p = pexpect.spawn ("telnet %s" % target)
fo.write ("!!!! Start Kumade2 !!!!\n")
fo.write ("!!!! login to %s !!!!\n" % target)
p.expect ("Password: ")
print_pexpect_stdout (p, fo)
p.sendline ("%s" % password)
p.expect ("> ")
print_pexpect_stdout (p, fo)
fo.write ("\n")
fo.write ("!!!! set pager disable !!!!\n")
p.sendline ("console lines infinity")
p.expect ("> ")
print_pexpect_stdout (p, fo)
fo.write ("\n")
fo.write ("!!!! show configuration !!!!\n")
p.sendline ("show config")
p.expect ("> ")
print_pexpect_stdout (p, fo)
p.sendline ("exit")
p.close ()
fo.write ("\n")
fo.write ("!!!! End of Kumade2 !!!!\n")
return
def nec (target, options, fo) :
username = options.username
password = options.password
p = pexpect.spawn ("telnet %s" % target)
fo.write ("!!!! Start Kumade2 !!!!\n")
fo.write ("!!!! login to %s !!!!\n" % target)
p.expect ("login: ")
print_pexpect_stdout (p, fo)
p.send ("%s\r" % username)
p.expect ("Password: ")
print_pexpect_stdout (p, fo)
p.send ("%s\r" % password)
p.expect ("# ")
print_pexpect_stdout (p, fo)
fo.write ("\n")
fo.write ("!!!! enter config mode !!!!\n")
p.sendline ("configure\r")
p.expect ("# ")
print_pexpect_stdout (p, fo)
fo.write ("\n")
fo.write ("!!!! set pager disable !!!!\n")
p.sendline ("terminal length 0\r")
p.expect ("# ")
print_pexpect_stdout (p, fo)
fo.write ("\n")
fo.write ("!!!! show configuration !!!!\n")
p.sendline ("show running-config\r")
p.expect ("# ")
print_pexpect_stdout (p, fo)
p.sendline ("exit\r")
p.expect ("# ")
print_pexpect_stdout (p, fo)
p.sendline ("exit\r")
p.close ()
fo.write ("\n")
fo.write ("!!!! End of Kumade2 !!!!\n")
return
def kumade2 (target, options) :
# execute kumade2.
if options.output :
try :
fo = open (options.output, 'w')
except :
print "failed to open output file '%s'" % options.output
sys.exit ()
else :
fo = sys.stdout
if options.device == 'juniper' :
juniper (target, options, fo)
elif options.device == 'alaxala' :
alaxala (target, options, fo)
elif options.device == 'wlc' :
wlc (target, options, fo)
elif options.device == 'yamaha' :
yamaha (target, options, fo)
elif options.device == 'nec' :
nec (target, options, fo)
else :
print "invalid device type '%s'" % options.device
sys.exit ()
if fo != sys.stdout :
fo.close ()
return
if __name__ == '__main__' :
desc = "usage : %prog [options] address"
parser = OptionParser (desc)
parser.add_option (
'-o', '--output', type = "string", default = None, dest = 'output',
help = 'output file name. default is stdout.'
)
parser.add_option (
'-u', '--username', type = "string", default = "", dest = 'username',
help = 'login username for taget device.'
)
parser.add_option (
'-p', '--password', type = "string", default = "", dest = 'password',
help = 'login password for taget device.'
)
parser.add_option (
'-d', '--device', type = "string", default = "none", dest = 'device',
help = 'device type: juniper, alaxala, wlc, yamaha, nec'
)
parser.add_option (
'-e', '--auto-enable', action = "store_true", default = False,
dest = 'auto_enable',
help = 'auto enable mode for the target device. default false.'
)
(options, args) = parser.parse_args ()
try :
target = args.pop ()
except :
print "target device is not specified"
sys.exit ()
kumade2 (target, options)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment