- 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
# - 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
#
@@ -26,14 +26,21 @@
# this program. If not, visit <http://www.gnu.org/licenses/> to download it.
### Configuration ###
router=`basename \`dirname $0 | pwd\`` # name of router - based on directory
dhcpfiles=dhcp-\* # list/pattern of dhcp lease files
imagefiles=c??00-universalk9-mz.SPA.\* # list/pattern of IOS image files
filestore=flash # location of dhcp/IOS files on cisco
router=`basename \`dirname $0 | pwd\`` # name of router - based on directory
dhcpfiles=dhcp-\* # list/pattern of dhcp lease files
imagefiles=c??00-universalk9-mz.S[SP]A.\* # list/pattern of IOS image files
filestore=flash # location of dhcp/IOS files on cisco
### Implementation ###
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
# 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
@@ -47,8 +54,11 @@ router_file_download() {
filesrc=$filestore
fi
echo downloading $tofile from router $filesrc
scp -q $router:$filesrc:$fromfile $tofile
git add $tofile
if scp -q $router:$filesrc:$fromfile $tofile; then
git add $tofile
else
fail "download of $tofile failed"
fi
}
# Support function to upload a file to the router
@@ -64,10 +74,13 @@ router_file_upload() {
filedst=$filestore
fi
echo uploading new/updated $tofile to router $filedst
scp -q $fromfile $router:$filedst:$tofile
if [ "$fromfile" != "$tofile" ]; then
mv $fromfile $tofile
git add $tofile
if scp -q $fromfile $router:$filedst:$tofile; then
if [ "$fromfile" != "$tofile" ]; then
mv $fromfile $tofile
git add $tofile
fi
else
fail "upload of $tofile failed"
fi
}
@@ -76,7 +89,9 @@ router_file_upload() {
router_file_remove() {
delfile=${1?ERROR: need a filename to remove from router}
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 "found unknown entry \"$tag $value\", aborting"
exit 1
fail "found unknown entry \"$tag $value\" in $filename"
;;
esac
done
@@ -118,22 +132,23 @@ do
router_file_upload .$filename.$$.tmp $filename
;;
*)
echo unsupported git status "$status", aborting
exit 1
fail "unsupported git status \"$status\" for $filename"
;;
esac
done
# Restart the DHCP service on the router if any of the dhcp files changed
if git status -s "$dhcpfiles" | egrep -q ^[MAD]; then
echo restarting dhcp service
cat << EOT | ssh -q $router
echo restarting DHCP service
cat << EOT | if ! ssh -q $router; then
configure terminal
no service dhcp
service dhcp
exit
exit
EOT
fail "unable to restart DHCP service"
fi
fi
@@ -155,8 +170,7 @@ do
router_file_upload $filename
;;
*)
echo unsupported git status "$status", aborting
exit 1
fail "unsupported git status \"$status\" for $filename"
;;
esac
done
@@ -175,3 +189,4 @@ fi
# show what has changed in the startup config and commit to the repository
git diff --cached startup-config
git commit || git reset HEAD startup-config