Advertisement
jordack

Bash Script - Backup HP Switch Config To FTP

May 2nd, 2019
8,567
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 2.55 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. #Script logs into HP Switches via SCP and grabs the Running Config to upload to FTP server
  4.  
  5. #Script needs expect and ftp
  6. ftpServer="192.168.1.50"
  7. ftpUser="netdevices"
  8. ftpPass="MyPassword"
  9.  
  10. switchList=("swcicr01" "swci08" "swcipr01" "swciga01" "swcicr02" "swcitr07" "swcitr01" "swcihr01" "swccdr01" "swci01" "swcitr02" "swcitr06" "swcpga01" "swcpgh01" "swf1ba01" "swf3dr01" "swf4dr01" "swfpdr01" "swgoga01" "swhmdr01" "swhsdr01" "swhudr01")
  11.  
  12. echo -en '\n'
  13. echo -n "Enter HP Switch User Account [ENTER]: "
  14. read hpUserName
  15. echo -en '\n'
  16. echo -en '\n'
  17. echo -n "Enter HP Admin Password [ENTER]: "
  18. read -s hpPassword
  19. echo -en '\n'
  20. for switch in ${switchList[@]}; do
  21.   echo -en '\n'
  22.   echo "Downloading config for $switch"
  23.   echo -en '\n'
  24.  
  25.   #Need to enable ip ssh filetransfer on hp switch
  26.  
  27.   echo "Running Command: scp $hpUserName@$switch:/cfg/running-config ./$switch-$(date +%Y-%m-%d).cfg"
  28.  
  29.   configFile=$switch-$(date +%Y-%m-%d_%H_%M).cfg
  30.  
  31.   /usr/bin/expect <<EOF
  32.  
  33.   proc ssh_failed { } {
  34.     send_user "ERROR: SSH LOGIN FAILED\n"
  35.     exit
  36.   }
  37.  
  38.   proc scp_ok { } {
  39.     send_user "SCP SUCCEEDED\n"
  40.   }
  41.  
  42.   spawn scp -oStrictHostKeyChecking=no $hpUserName@$switch:/cfg/running-config ./$configFile
  43.   #Check if login is successful and import ssh key if not yet in known_hosts
  44.   expect {
  45.    "Connection refused" { ssh_failed }
  46.    "No route to host" { ssh_failed }
  47.    "Permission denied" { ssh_failed }
  48.    "bad password." { ssh_failed }
  49.    "(yes/no)?" { send "yes\r";
  50.    exp_continue }
  51.    "password:" { send "$hpPassword\r";
  52.    exp_continue }
  53.    "Password:" { send "$hpPassword\r";
  54.    exp_continue }
  55.    "100% " { scp_ok }
  56.    default { ssh_failed }
  57.   }
  58.  
  59. EOF
  60.  
  61. #Make Sure the folder exist on FTP Server
  62. ftp -inv $ftpServer <<EOF
  63. user $ftpUser $ftpPass
  64. mkdir HP_Switch
  65. cd HP_Switch
  66. mkdir $switch
  67. cd $switch
  68. mkdir $(date +%Y-%m-%d)
  69. bye
  70. EOF
  71.  
  72. #Upload File to FTP server
  73. echo -en '\n'
  74. echo "Uploading Config File To FTP Server"
  75. echo -en '\n'
  76.  
  77. ftp -inv $ftpServer <<EOF
  78. user $ftpUser $ftpPass
  79. cd HP_Switch/$switch/$(date +%Y-%m-%d)
  80. put ./$configFile
  81. bye
  82. EOF
  83.  
  84.  
  85. #--- Display the content for to ensure its good
  86. echo -en '\n'
  87. echo -en '\n'
  88. echo "The following switch has attempted a backup"
  89. echo "Important Note:  Time outs suck, you may need to close the Duo app and reopen each time a logon attempt is made."
  90. echo -en '\n'
  91. echo -e "\e[36m $switch\e[39m"
  92. echo -en '\n'
  93. echo "Please verify the backup was successful before continuing"
  94. read -p "Press enter to continue or CTRL-C to quit."
  95.  
  96. done
  97. #=============End Functions
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement