본문 바로가기
<툴, 프로그램 관련>/[Script]

SecureCRT python script to update hostname and banner

by Aggies '19 2020. 3. 4.
반응형

This script is to update hostname and banner of cisco swtiches especially IOS.

 

******

 

# $language = "python"

# $interface = "1.0"

#

 

# Author: Ethan Park

# Date: 3/2/2020

# This script reads a csv file which contains hostname, IP address and a model

# It updates hostname and banner section

 

import csv

import math



def updateBanner(hostmodel):

 

    hostLength = int(math.ceil((35 - len(host)) / 2))

    modelLength = int(math.ceil((35 - len(model)) / 2))

 

    crt.Screen.Send('no banner motd' + '\r')

    crt.Screen.WaitForString("#")

 

    crt.Screen.Send('no banner login' + '\r')

    crt.Screen.WaitForString("#")

 

    crt.Screen.Send('no banner exec' + '\r')

    crt.Screen.WaitForString("#")

 

    #

    # Login banner update

 

    crt.Screen.Send('banner login ^' + '\r')

    crt.Sleep(300)

    crt.Screen.Send('=-=-=-=-=-=-=-=-=' + '\r')

    crt.Sleep(300)

    crt.Screen.Send('SECURITY NOTICE' + '\r')

    crt.Sleep(300)

    crt.Screen.Send('=-=-=-=-=-=-=-=-=' + '\r')

    crt.Sleep(300)

    crt.Screen.Send('\r')

    crt.Screen.Send(

        'LOG OFF IMMEDIATELY if you are not an authorized user ' + '\r')

    crt.Sleep(300)

 

    crt.Screen.Send('^' + '\r')

 

    crt.Screen.WaitForString("#")

 

    #

    # exec banner update

 

    crt.Screen.Send('banner exec ^' + '\r')

    crt.Sleep(300)

 

    crt.Screen.Send('***********************************' + '\r')

    crt.Sleep(300)

 

    banner = '*' + ' ' * hostLength + host + \

        ' ' * (35 - len(host) - hostLength - 2+ '*'

    crt.Screen.Send(banner + '\r')

    crt.Sleep(300)

 

    banner='*' + ' ' * modelLength + model + ' ' * \

        (35 - len(model) - modelLength - 2+ '*'

    crt.Screen.Send(banner + '\r')

    crt.Sleep(300)

 

    crt.Screen.Send('***********************************' + '\r')

    crt.Screen.Send('^' + '\r')

 

def updateHostname(host):

    crt.Screen.Send('hostname ' + host + '\r')

 

def main():

 

    # Set Synchronous to True so that we don't miss any data

    crt.Screen.Synchronous=True

 

    host = []

    ipAddr = []

    model = []

 

    # CSV file open

    #

    with open('device.csv''r'as data:

        data_reader = csv.reader(data)

        next(data_reader)

 

        for row in data_reader:

            host.append(row[0])

            ipAddr.append(row[1])

            model.append(row[2])

 

        # Your admin account password

        password = crt.Dialog.Prompt("SSH password:""Password"""True)

 

    for i in range(len(host)):

        crt.Screen.Send('ssh ' + ipAddr[i] + '\r')

 

        result = crt.Screen.WaitForStrings(["(yes/no)?""refused""ord:"])

 

        if result == 1:

            crt.Screen.Send('yes' + '\r')

        elif result == 2:

            crt.Screen.Send('telnet ' + ipAddr[i] + '\r')

            crt.Screen.WaitForString("Username:")

            crt.Screen.Send('hpkadm' + '\r')

            crt.Screen.WaitForString("ord:")

        else:

            pass

 

        crt.Sleep(500)

        crt.Screen.Send(password + '\r')

        crt.Screen.WaitForString(">")

 

        crt.Screen.Send('en' + '\r')

        crt.Screen.WaitForString("ord:")

 

        crt.Screen.Send(password + '\r')

        crt.Screen.WaitForString("#")

 

        crt.Screen.Send('conf t' + '\r')

        crt.Screen.WaitForString("#")

 

        updateBanner(host[i], model[i])

        updateHostname(host[i])

 

        crt.Screen.Send('end' + '\r')

        crt.Screen.WaitForString("#")

 

        crt.Screen.Send('wr' + '\r')

        crt.Screen.WaitForString("#")

 

        crt.Screen.Send('exit' + '\r')

        crt.Screen.WaitForString("$")

 

main()

반응형