Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/sh
- # Copyright (C) 2015 Uco Mesdag
- # Usage: cisco-backup [--backup|--restore] [--file file] user@hostname
- # -b,--backup back up the configuration to a file or stout.
- # -r,--restore restore the configuration from a file.
- # -f file,--file file set the path to the configuration file.
- # Instructions: Use this script to backup or restore the configuration of your
- # cisco device via SSH.
- usage() {
- echo "Usage: $(basename "$0") [--backup|--restore] [--file file] user@hostname" >/dev/stdout
- echo " -b,--backup back up the configuration to a file or stout." >/dev/stdout
- echo " -r,--restore restore the configuration from a file." >/dev/stdout
- echo " -f file,--file file set the path to the configuration file." >/dev/stdout
- }
- if [ "$( echo $@ | grep '.*\(help\|\?\)$' )" ]
- then
- usage
- exit 0
- fi
- ARGS=$(getopt -a -l backup,restore,file: -o b,r,f: -- "$@")
- #Bad arguments
- if [ $? -ne 0 ] || [ $# -lt 1 -o $# -gt 4 ]
- then
- usage
- exit 1
- fi
- eval set -- "$ARGS"
- while true;
- do
- case "$1" in
- -b|--backup)
- shift;
- ACTION=backup;
- ;;
- -r|--restore)
- shift;
- ACTION=restore;
- ;;
- -f|--file)
- shift;
- if [ -n "$1" ]; then
- echo $1;
- FILE="$( cd $(dirname "$1") ; pwd )/$1";
- shift;
- fi
- ;;
- --)
- shift;
- break;
- ;;
- esac
- done
- if [ "x$@" == "x" ]
- then
- echo -e " ...no router address specified"
- exit 1
- fi
- if [ "$ACTION" == "restore" ] && [ "x$FILE" == "x" ]
- then
- echo -e " ...no ouput file specified"
- exit 1
- fi
- if [ "x$ACTION" == "x" ]
- then
- ACTION=backup
- fi
- IFS="@"
- set -- $@
- if [ $# -eq 2 ]
- then
- USER="$1"
- HOSTNAME="$2"
- fi
- unset IFS
- if [ "$ACTION" == "backup" ]
- then
- if [ $FILE ]
- then
- echo -e " ...backing-up the router at $HOSTNAME to the file \"$FILE\""
- ssh $USER@$HOSTNAME "show running-config;exit" | sed \
- -e '/aaa/d' \
- -e '/^\r$/d' \
- -e '/^Building configuration.*/d' \
- -e '/^Current configuration.*/d' \
- -e '/^Connection to .* closed by remote host.*/d' \
- > "$FILE" \
- 2>/dev/null
- else
- ssh $USER@$HOSTNAME "show running-config" | sed \
- -e '/aaa/d' \
- -e '/^\r$/d' \
- -e '/^Building configuration.*/d' \
- -e '/^Current configuration.*/d' \
- -e '/^Connection to .* closed by remote host.*/d' \
- 2>/dev/null
- fi
- fi
- if [ "$ACTION" == "restore" ]
- then
- echo -e " ...restoring the router at $HOSTNAME from the file \"$FILE\""
- CONFIG="$( mktemp )"
- echo "configure terminal" > $CONFIG
- cat "$FILE" >> $CONFIG
- echo "exit" >> $CONFIG
- echo "copy running-config startup-config" >> $CONFIG
- ssh $USER@$HOSTNAME < $CONFIG >/dev/null 2>&1
- fi
Add Comment
Please, Sign In to add comment