Date created: Sunday, January 27, 2019 11:44:50 AM. Last modified: Sunday, February 10, 2019 10:03:51 AM

Eve Config Bacup

#!/bin/bash


# Send "exit\r" over telnet twice to ensure the target device is at the login  
# prompt, i.e. put any device into the same "state" before we start the  
# backup process. For certain device we might need some extra commands (e.g. 
# vMX or CSR1000v) in case the device console was left in config mode.
function exit_cli {

    if [ "$2" == "vMX" ]; then

        (
        echo -e "exit configuration-mode\r"
        sleep 2
        ) | telnet 127.0.0.1 $1 | tee "$2" 


    elif [ "$2" == "CSR1000v" ]; then

        (
        echo -e "end\r"
        sleep 2
        ) | telnet 127.0.0.1 $1 | tee "$2" 

    elif [ "$2" == "XRv9k" ]; then

        (
        echo -e "clear\r"
        sleep 2
        echo -e "end\r"
        sleep 2
        ) | telnet 127.0.0.1 $1 | tee "$2" 


    elif [ "$2" == "ios" ]; then

        (
        echo -e "end\r"
        sleep 2
        ) | telnet 127.0.0.1 $1 | tee "$2" 

    fi


    (
    echo -e "exit\r"
    sleep 2
    echo -e "exit\r"
    sleep 2
    ) | telnet 127.0.0.1 $1 | tee "$2"

}


# There is no ios backup script included in EVE, this function will produce  
# the same result as the other backup scripts provided by EVE.
function backup_ios {

    (
    echo -e "\r"
    sleep 2
    echo -e "\r"
    sleep 2
    echo -e "enable\r"
    sleep 2
    echo -e "term len 0\r"
    sleep 2
    echo -e "show run\r"
    sleep 10
    echo -e "exit\r"
    sleep 2
    echo -e "exit\r"
    ) | telnet 127.0.0.1 $1 | tee "$2" 

}


year_month=`date +"%Y-%m"` # 2019-01
timestamp=`date +"%Y-%m-%d--%H-%M-%S"` # 2019-01-27--09-17-31
backup_dir="/home/bensley/config_backups/$year_month/$timestamp"
compress_dir="/home/bensley/config_backups/$year_month"
script_dir="/opt/unetlab/scripts"

mkdir -p "$backup_dir"

# vMX, CSR1000v, XRv9K scripts expect to login to the device.

# PE1 - vMX VPC - Expects to login, will logout when finished, script edited to contain login details
# The vMX script very reliably times-out, run it twice and it works fine:
exit_cli 32769 "vMX"
$script_dir/config_vmxvcp.py -a get -f "$backup_dir/PE1.txt" -p 32769
sleep 2
$script_dir/config_vmxvcp.py -a get -f "$backup_dir/PE1.txt" -p 32769

# PE3 - CSR1000v - This device has no console authentication
exit_cli 32771 "CSR1000v"
$script_dir/config_csr1000v.py -a get -f "$backup_dir/PE3.txt" -p 32771

# RR1 - CSR1000v - This device has no console authentication
exit_cli 32772 "CSR1000v"
$script_dir/config_csr1000v.py -a get -f "$backup_dir/RR1.txt" -p 32772

# PE2 - XRv9k - Edited script to include login details, expects to login and will logout when finished
exit_cli 32773 "XRv9k"
$script_dir/config_xrv9k.py -a get -f "$backup_dir/PE2.txt" -p 32773

# RR2 - XRv9k - Edited script to include login details, expects to login and will logout when finished
exit_cli 32776 "XRv9k"
$script_dir/config_xrv9k.py -a get -f "$backup_dir/RR2.txt" -p 32776

# P1 - IOS
exit_cli 32774 "ios"
backup_ios 32774 "$backup_dir/P1.txt"

# P2 - IOS
exit_cli 32775 "ios"
backup_ios 32775 "$backup_dir/P2.txt"

# CPE1 - IOS
exit_cli 32777 "ios"
backup_ios 32777 "$backup_dir/CPE1.txt"

# CPE2 - IOS
exit_cli 32775 "ios"
backup_ios 32775 "$backup_dir/CPE2.txt"

cp /opt/unetlab/labs/MultiVendor-1.unl "$backup_dir/"
tar -cvzf $compress_dir/$timestamp.tar.gz $backup_dir
if [ "$?" -eq 0 ]; then
    rm -rf "$backup_dir"
fi