Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- #Script logs into HP Switches via SCP and grabs the Running Config to upload to FTP server
- #Script needs expect and ftp
- ftpServer="192.168.1.50"
- ftpUser="netdevices"
- ftpPass="MyPassword"
- switchList=("swcicr01" "swci08" "swcipr01" "swciga01" "swcicr02" "swcitr07" "swcitr01" "swcihr01" "swccdr01" "swci01" "swcitr02" "swcitr06" "swcpga01" "swcpgh01" "swf1ba01" "swf3dr01" "swf4dr01" "swfpdr01" "swgoga01" "swhmdr01" "swhsdr01" "swhudr01")
- echo -en '\n'
- echo -n "Enter HP Switch User Account [ENTER]: "
- read hpUserName
- echo -en '\n'
- echo -en '\n'
- echo -n "Enter HP Admin Password [ENTER]: "
- read -s hpPassword
- echo -en '\n'
- for switch in ${switchList[@]}; do
- echo -en '\n'
- echo "Downloading config for $switch"
- echo -en '\n'
- #Need to enable ip ssh filetransfer on hp switch
- echo "Running Command: scp $hpUserName@$switch:/cfg/running-config ./$switch-$(date +%Y-%m-%d).cfg"
- configFile=$switch-$(date +%Y-%m-%d_%H_%M).cfg
- /usr/bin/expect <<EOF
- proc ssh_failed { } {
- send_user "ERROR: SSH LOGIN FAILED\n"
- exit
- }
- proc scp_ok { } {
- send_user "SCP SUCCEEDED\n"
- }
- spawn scp -oStrictHostKeyChecking=no $hpUserName@$switch:/cfg/running-config ./$configFile
- #Check if login is successful and import ssh key if not yet in known_hosts
- expect {
- "Connection refused" { ssh_failed }
- "No route to host" { ssh_failed }
- "Permission denied" { ssh_failed }
- "bad password." { ssh_failed }
- "(yes/no)?" { send "yes\r";
- exp_continue }
- "password:" { send "$hpPassword\r";
- exp_continue }
- "Password:" { send "$hpPassword\r";
- exp_continue }
- "100% " { scp_ok }
- default { ssh_failed }
- }
- EOF
- #Make Sure the folder exist on FTP Server
- ftp -inv $ftpServer <<EOF
- user $ftpUser $ftpPass
- mkdir HP_Switch
- cd HP_Switch
- mkdir $switch
- cd $switch
- mkdir $(date +%Y-%m-%d)
- bye
- EOF
- #Upload File to FTP server
- echo -en '\n'
- echo "Uploading Config File To FTP Server"
- echo -en '\n'
- ftp -inv $ftpServer <<EOF
- user $ftpUser $ftpPass
- cd HP_Switch/$switch/$(date +%Y-%m-%d)
- put ./$configFile
- bye
- EOF
- #--- Display the content for to ensure its good
- echo -en '\n'
- echo -en '\n'
- echo "The following switch has attempted a backup"
- echo "Important Note: Time outs suck, you may need to close the Duo app and reopen each time a logon attempt is made."
- echo -en '\n'
- echo -e "\e[36m $switch\e[39m"
- echo -en '\n'
- echo "Please verify the backup was successful before continuing"
- read -p "Press enter to continue or CTRL-C to quit."
- done
- #=============End Functions
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement