Cisco

  • Zero Touch Provisiong (ZTP) 22 September, 2019 by Joey

    Script to automate Cisco IOS upgrades. I used it to upgrade from IOS 16.3.6 to 16.6.6. Should work for other versions as wel. Hoever version 16.3.6 had a bug in it which caused the HTTP server to be disabled by default. The script contains a workaround for that. It might throw an error on the first run but just leave it running. The second run should go smooth. The whole process take about 20 minutes.

    from cli import configure, cli, pnp
    import time
    
    # Set Global variables to be used in later functions. Please adjust to your needs.
    tftp_server = '10.0.0.1'
    img_cat3k = 'cat3k_caa-universalk9.16.06.06.SPA.bin'
    software_version = 'Version 16.6.6'
    
    def deploy_eem_cleanup_script():
        install_command = 'install remove inactive'
        eem_commands = ['event manager applet cleanup',
                        'event none maxrun 600',
                        'action 1.0 cli command "enable"',
                        'action 2.0 cli command "%s" pattern "\[y\/n\]"' % install_command,
                        'action 2.1 cli command "y" pattern "proceed"',
                        'action 2.2 cli command "y"'
                        ]
        results = configure(eem_commands)
        print '*** Successfully configured cleanup EEM script on device! ***'
    
    # Workaround for disabled HTTP server. 
    def enable_http():
        eem_commands = ['event manager applet enablehttp',
                        'event none maxrun 600',
                        'action 1.0 cli command "enable"',
                        'action 2.0 cli command "config t"',
                        'action 2.1 cli command "ip http server"',
                        'action 2.2 cli command "end"'
                        ]
        results = configure(eem_commands)
        print '*** Successfully configured HTTP server on device! ***'
    
    def deploy_eem_upgrade_script(image):
        install_command = 'install add file flash:' + image + ' activate commit'
        eem_commands = ['event manager applet upgrade',
                        'event none maxrun 600',
                        'action 1.0 cli command "enable"',
                        'action 2.0 cli command "%s" pattern "\[y\/n\/q\]"' % install_command,
                        'action 2.1 cli command "n" pattern "proceed"',
                        'action 2.2 cli command "y"'
                        ]
        results = configure(eem_commands)
        print '*** Successfully configured upgrade EEM script on device! ***'
    
    def file_transfer(tftp_server, file, file_system='flash:/'):
        destination = file_system + file
        commands = ['file prompt quiet',
                    'ip tftp blocksize 8192'
                   ]
        results = configure(commands)
        print '*** Successfully set "file prompt quiet" on switch ***'
        transfer_file = "copy tftp://%s/%s %s vrf Mgmt-vrf" % (tftp_server, file, destination)
        print 'Transferring %s to %s' % (file, file_system)
        transfer_results = cli(transfer_file)
        if 'OK' in transfer_results:
            print '*** %s was transferred successfully!!! ***' % (file)
        elif 'XXX Error opening XXX' in transfer_results:
            raise ValueError("XXX Failed Xfer XXX")
    
    
    def upgrade_required():
        # Obtains show version output
        sh_version = cli('show version | i Switch Software')
        # Check if switch is on correct version.
        print(str(sh_version))
        a_bool = software_version in str(sh_version)
        return a_bool
    
    def main():
        print '###### STARTING ZTP SCRIPT ######'
        enable_http()
        time.sleep(20)
        if upgrade_required():
            print '*** Upgrade is not required ***\n'
        else:
            print '*** Upgrade is required. Starting upgrade process.. ***\n'
            print '*** Attempting to transfer image to switch.. ***'
            file_transfer(tftp_server, img_cat3k)
            print '*** Deploying EEM upgrade script ***'
            deploy_eem_upgrade_script(img_cat3k)
            print '*** Performing the upgrade - switch will reboot ***\n'
            cli('event manager run upgrade')
            time.sleep(600)
    
        # Cleanup any leftover install files
        print '*** Deploying Cleanup EEM Script ***'
        deploy_eem_cleanup_script()
        print '*** Running Cleanup EEM Script ***'
        cli('event manager run cleanup')
        time.sleep(30) 
     
        print '###### FINISHED ZTP SCRIPT ######'
    
    
    if __name__ in "__main__":
        main()
    

Leave a Reply

Your email address will not be published. Required fields are marked *