- added fail() function to exit with an error code in case a problem occurs

- scp and ssh actions that fail (i.e. host not available) will now generate an error message (fixes #6)
- default pattern for universal IOS images now also supports provisional images (fixes #8)
This commit is contained in:
2016-11-22 13:15:26 +01:00
parent 418e65bb68
commit da65f63d57

View File

@@ -10,7 +10,7 @@
# repository, update startup-config file accordingly on router # repository, update startup-config file accordingly on router
# - commit changes to the git repository # - commit changes to the git repository
# #
# Version 1.1, latest version at: https://gitlab.lindenaar.net/scripts/cisco # Version 1.2, latest version at: https://gitlab.lindenaar.net/scripts/cisco
# #
# Copyright (c) 2016 Frederik Lindenaar # Copyright (c) 2016 Frederik Lindenaar
# #
@@ -26,14 +26,21 @@
# this program. If not, visit <http://www.gnu.org/licenses/> to download it. # this program. If not, visit <http://www.gnu.org/licenses/> to download it.
### Configuration ### ### Configuration ###
router=`basename \`dirname $0 | pwd\`` # name of router - based on directory router=`basename \`dirname $0 | pwd\`` # name of router - based on directory
dhcpfiles=dhcp-\* # list/pattern of dhcp lease files dhcpfiles=dhcp-\* # list/pattern of dhcp lease files
imagefiles=c??00-universalk9-mz.SPA.\* # list/pattern of IOS image files imagefiles=c??00-universalk9-mz.S[SP]A.\* # list/pattern of IOS image files
filestore=flash # location of dhcp/IOS files on cisco filestore=flash # location of dhcp/IOS files on cisco
### Implementation ### ### Implementation ###
echo updating with $router echo updating with $router
# Support function to print an error message and quit with an error exit code(1)
# parameters: $1 - message to be printed
fail() {
echo FATAL: ${1?ERROR: no error message provided for fail()}, aborted
exit 1
}
# Support function to download a file from the router # Support function to download a file from the router
# parameters: $1 - filename to be copied and $2 - (optional) target filename # parameters: $1 - filename to be copied and $2 - (optional) target filename
# when no target filename is provided the source file will be downloaded to the # when no target filename is provided the source file will be downloaded to the
@@ -47,8 +54,11 @@ router_file_download() {
filesrc=$filestore filesrc=$filestore
fi fi
echo downloading $tofile from router $filesrc echo downloading $tofile from router $filesrc
scp -q $router:$filesrc:$fromfile $tofile if scp -q $router:$filesrc:$fromfile $tofile; then
git add $tofile git add $tofile
else
fail "download of $tofile failed"
fi
} }
# Support function to upload a file to the router # Support function to upload a file to the router
@@ -64,10 +74,13 @@ router_file_upload() {
filedst=$filestore filedst=$filestore
fi fi
echo uploading new/updated $tofile to router $filedst echo uploading new/updated $tofile to router $filedst
scp -q $fromfile $router:$filedst:$tofile if scp -q $fromfile $router:$filedst:$tofile; then
if [ "$fromfile" != "$tofile" ]; then if [ "$fromfile" != "$tofile" ]; then
mv $fromfile $tofile mv $fromfile $tofile
git add $tofile git add $tofile
fi
else
fail "upload of $tofile failed"
fi fi
} }
@@ -76,7 +89,9 @@ router_file_upload() {
router_file_remove() { router_file_remove() {
delfile=${1?ERROR: need a filename to remove from router} delfile=${1?ERROR: need a filename to remove from router}
echo removing $delfile as it is no longer in the repository echo removing $delfile as it is no longer in the repository
ssh -q $router "delete /force $filestore:$delfile" if ! ssh -q $router "delete /force $filestore:$delfile"; then
fail "removal of $delfile failed"
fi
} }
@@ -109,8 +124,7 @@ do
echo $tag $[ $value + 1 ] >> .$filename.$$.tmp echo $tag $[ $value + 1 ] >> .$filename.$$.tmp
;; ;;
*) *)
echo "found unknown entry \"$tag $value\", aborting" fail "found unknown entry \"$tag $value\" in $filename"
exit 1
;; ;;
esac esac
done done
@@ -118,22 +132,23 @@ do
router_file_upload .$filename.$$.tmp $filename router_file_upload .$filename.$$.tmp $filename
;; ;;
*) *)
echo unsupported git status "$status", aborting fail "unsupported git status \"$status\" for $filename"
exit 1
;; ;;
esac esac
done done
# Restart the DHCP service on the router if any of the dhcp files changed # Restart the DHCP service on the router if any of the dhcp files changed
if git status -s "$dhcpfiles" | egrep -q ^[MAD]; then if git status -s "$dhcpfiles" | egrep -q ^[MAD]; then
echo restarting dhcp service echo restarting DHCP service
cat << EOT | ssh -q $router cat << EOT | if ! ssh -q $router; then
configure terminal configure terminal
no service dhcp no service dhcp
service dhcp service dhcp
exit exit
exit exit
EOT EOT
fail "unable to restart DHCP service"
fi
fi fi
@@ -155,8 +170,7 @@ do
router_file_upload $filename router_file_upload $filename
;; ;;
*) *)
echo unsupported git status "$status", aborting fail "unsupported git status \"$status\" for $filename"
exit 1
;; ;;
esac esac
done done
@@ -173,5 +187,6 @@ fi
# show what has changed in the startup config and commit to the repository # show what has changed in the startup config and commit to the repository
git diff --cached startup-config git diff --cached startup-config
git commit || git reset HEAD startup-config git commit || git reset HEAD startup-config