ucomesdag

Cisco backup and restore over SSH

Jul 8th, 2015 (edited)
339
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 3.11 KB | None | 0 0
  1. #!/bin/sh
  2.  
  3. # Copyright (C) 2015 Uco Mesdag
  4. # Usage:    cisco-backup [--backup|--restore] [--file file] user@hostname
  5. #   -b,--backup                     back up the configuration to a file or stout.
  6. #   -r,--restore                    restore the configuration from a file.
  7. #   -f file,--file file             set the path to the configuration file.
  8.  
  9.  
  10. # Instructions: Use this script to backup or restore the configuration of your
  11. #    cisco device via SSH.
  12.  
  13. usage() {
  14.     echo "Usage:    $(basename "$0") [--backup|--restore] [--file file] user@hostname" >/dev/stdout
  15.     echo "  -b,--backup                     back up the configuration to a file or stout." >/dev/stdout
  16.     echo "  -r,--restore                    restore the configuration from a file." >/dev/stdout
  17.     echo "  -f file,--file file             set the path to the configuration file." >/dev/stdout
  18. }
  19.  
  20. if [ "$( echo $@ | grep '.*\(help\|\?\)$' )" ]
  21. then
  22.     usage
  23.     exit 0
  24. fi
  25.  
  26. ARGS=$(getopt -a -l backup,restore,file: -o b,r,f: -- "$@")
  27.  
  28. #Bad arguments
  29. if [ $? -ne 0 ] || [ $# -lt 1 -o $# -gt 4 ]
  30. then
  31.     usage
  32.     exit 1
  33. fi
  34.  
  35. eval set -- "$ARGS"
  36.  
  37. while true;
  38. do
  39.     case "$1" in
  40.         -b|--backup)
  41.             shift;
  42.             ACTION=backup;
  43.             ;;
  44.         -r|--restore)
  45.             shift;
  46.             ACTION=restore;
  47.             ;;
  48.         -f|--file)
  49.             shift;
  50.             if [ -n "$1" ]; then
  51.                 echo $1;
  52.                 FILE="$( cd $(dirname "$1") ; pwd )/$1";
  53.                 shift;
  54.             fi
  55.             ;;
  56.         --)
  57.             shift;
  58.             break;
  59.             ;;
  60.     esac
  61. done
  62.  
  63. if [ "x$@" == "x" ]
  64. then
  65.     echo -e "    ...no router address specified"
  66.     exit 1
  67. fi
  68.  
  69. if [ "$ACTION" == "restore" ] && [ "x$FILE" == "x" ]
  70. then
  71.     echo -e "    ...no ouput file specified"
  72.     exit 1
  73. fi
  74.  
  75. if [ "x$ACTION" == "x" ]
  76. then
  77.     ACTION=backup
  78. fi
  79.  
  80. IFS="@"
  81. set -- $@
  82. if [ $# -eq 2 ]
  83. then
  84.     USER="$1"
  85.     HOSTNAME="$2"
  86. fi
  87. unset IFS
  88.  
  89. if [ "$ACTION" == "backup" ]
  90. then
  91.     if [ $FILE ]
  92.     then
  93.         echo -e "    ...backing-up the router at $HOSTNAME to the file \"$FILE\""
  94.         ssh $USER@$HOSTNAME "show running-config;exit" | sed \
  95.             -e '/aaa/d' \
  96.             -e '/^\r$/d' \
  97.             -e '/^Building configuration.*/d' \
  98.             -e '/^Current configuration.*/d' \
  99.             -e '/^Connection to .* closed by remote host.*/d' \
  100.             > "$FILE" \
  101.             2>/dev/null
  102.     else
  103.         ssh $USER@$HOSTNAME "show running-config" | sed \
  104.             -e '/aaa/d' \
  105.             -e '/^\r$/d' \
  106.             -e '/^Building configuration.*/d' \
  107.             -e '/^Current configuration.*/d' \
  108.             -e '/^Connection to .* closed by remote host.*/d' \
  109.             2>/dev/null
  110.     fi
  111. fi
  112.  
  113. if [ "$ACTION" == "restore" ]
  114. then
  115.     echo -e "    ...restoring the router at $HOSTNAME from the file \"$FILE\""
  116.     CONFIG="$( mktemp )"
  117.     echo "configure terminal" > $CONFIG
  118.     cat "$FILE" >> $CONFIG
  119.     echo "exit" >> $CONFIG
  120.     echo "copy running-config startup-config" >> $CONFIG
  121.     ssh $USER@$HOSTNAME < $CONFIG >/dev/null 2>&1
  122. fi
Add Comment
Please, Sign In to add comment