Commit da65f63d5799af4c2051b1e0c9694112374175f0
1 parent
418e65bb
- 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)
Showing
1 changed file
with
36 additions
and
21 deletions
sync-router
... | ... | @@ -10,7 +10,7 @@ |
10 | 10 | # repository, update startup-config file accordingly on router |
11 | 11 | # - commit changes to the git repository |
12 | 12 | # |
13 | -# Version 1.1, latest version at: https://gitlab.lindenaar.net/scripts/cisco | |
13 | +# Version 1.2, latest version at: https://gitlab.lindenaar.net/scripts/cisco | |
14 | 14 | # |
15 | 15 | # Copyright (c) 2016 Frederik Lindenaar |
16 | 16 | # |
... | ... | @@ -26,14 +26,21 @@ |
26 | 26 | # this program. If not, visit <http://www.gnu.org/licenses/> to download it. |
27 | 27 | |
28 | 28 | ### Configuration ### |
29 | -router=`basename \`dirname $0 | pwd\`` # name of router - based on directory | |
30 | -dhcpfiles=dhcp-\* # list/pattern of dhcp lease files | |
31 | -imagefiles=c??00-universalk9-mz.SPA.\* # list/pattern of IOS image files | |
32 | -filestore=flash # location of dhcp/IOS files on cisco | |
29 | +router=`basename \`dirname $0 | pwd\`` # name of router - based on directory | |
30 | +dhcpfiles=dhcp-\* # list/pattern of dhcp lease files | |
31 | +imagefiles=c??00-universalk9-mz.S[SP]A.\* # list/pattern of IOS image files | |
32 | +filestore=flash # location of dhcp/IOS files on cisco | |
33 | 33 | |
34 | 34 | ### Implementation ### |
35 | 35 | echo updating with $router |
36 | 36 | |
37 | +# Support function to print an error message and quit with an error exit code(1) | |
38 | +# parameters: $1 - message to be printed | |
39 | +fail() { | |
40 | + echo FATAL: ${1?ERROR: no error message provided for fail()}, aborted | |
41 | + exit 1 | |
42 | +} | |
43 | + | |
37 | 44 | # Support function to download a file from the router |
38 | 45 | # parameters: $1 - filename to be copied and $2 - (optional) target filename |
39 | 46 | # when no target filename is provided the source file will be downloaded to the |
... | ... | @@ -47,8 +54,11 @@ router_file_download() { |
47 | 54 | filesrc=$filestore |
48 | 55 | fi |
49 | 56 | echo downloading $tofile from router $filesrc |
50 | - scp -q $router:$filesrc:$fromfile $tofile | |
51 | - git add $tofile | |
57 | + if scp -q $router:$filesrc:$fromfile $tofile; then | |
58 | + git add $tofile | |
59 | + else | |
60 | + fail "download of $tofile failed" | |
61 | + fi | |
52 | 62 | } |
53 | 63 | |
54 | 64 | # Support function to upload a file to the router |
... | ... | @@ -64,10 +74,13 @@ router_file_upload() { |
64 | 74 | filedst=$filestore |
65 | 75 | fi |
66 | 76 | echo uploading new/updated $tofile to router $filedst |
67 | - scp -q $fromfile $router:$filedst:$tofile | |
68 | - if [ "$fromfile" != "$tofile" ]; then | |
69 | - mv $fromfile $tofile | |
70 | - git add $tofile | |
77 | + if scp -q $fromfile $router:$filedst:$tofile; then | |
78 | + if [ "$fromfile" != "$tofile" ]; then | |
79 | + mv $fromfile $tofile | |
80 | + git add $tofile | |
81 | + fi | |
82 | + else | |
83 | + fail "upload of $tofile failed" | |
71 | 84 | fi |
72 | 85 | } |
73 | 86 | |
... | ... | @@ -76,7 +89,9 @@ router_file_upload() { |
76 | 89 | router_file_remove() { |
77 | 90 | delfile=${1?ERROR: need a filename to remove from router} |
78 | 91 | echo removing $delfile as it is no longer in the repository |
79 | - ssh -q $router "delete /force $filestore:$delfile" | |
92 | + if ! ssh -q $router "delete /force $filestore:$delfile"; then | |
93 | + fail "removal of $delfile failed" | |
94 | + fi | |
80 | 95 | } |
81 | 96 | |
82 | 97 | |
... | ... | @@ -109,8 +124,7 @@ do |
109 | 124 | echo $tag $[ $value + 1 ] >> .$filename.$$.tmp |
110 | 125 | ;; |
111 | 126 | *) |
112 | - echo "found unknown entry \"$tag $value\", aborting" | |
113 | - exit 1 | |
127 | + fail "found unknown entry \"$tag $value\" in $filename" | |
114 | 128 | ;; |
115 | 129 | esac |
116 | 130 | done |
... | ... | @@ -118,22 +132,23 @@ do |
118 | 132 | router_file_upload .$filename.$$.tmp $filename |
119 | 133 | ;; |
120 | 134 | *) |
121 | - echo unsupported git status "$status", aborting | |
122 | - exit 1 | |
135 | + fail "unsupported git status \"$status\" for $filename" | |
123 | 136 | ;; |
124 | 137 | esac |
125 | 138 | done |
126 | 139 | |
127 | 140 | # Restart the DHCP service on the router if any of the dhcp files changed |
128 | 141 | if git status -s "$dhcpfiles" | egrep -q ^[MAD]; then |
129 | - echo restarting dhcp service | |
130 | - cat << EOT | ssh -q $router | |
142 | + echo restarting DHCP service | |
143 | + cat << EOT | if ! ssh -q $router; then | |
131 | 144 | configure terminal |
132 | 145 | no service dhcp |
133 | 146 | service dhcp |
134 | 147 | exit |
135 | 148 | exit |
136 | 149 | EOT |
150 | + fail "unable to restart DHCP service" | |
151 | + fi | |
137 | 152 | fi |
138 | 153 | |
139 | 154 | |
... | ... | @@ -155,8 +170,7 @@ do |
155 | 170 | router_file_upload $filename |
156 | 171 | ;; |
157 | 172 | *) |
158 | - echo unsupported git status "$status", aborting | |
159 | - exit 1 | |
173 | + fail "unsupported git status \"$status\" for $filename" | |
160 | 174 | ;; |
161 | 175 | esac |
162 | 176 | done |
... | ... | @@ -173,5 +187,6 @@ fi |
173 | 187 | |
174 | 188 | |
175 | 189 | # show what has changed in the startup config and commit to the repository |
176 | -git diff --cached startup-config | |
190 | +git diff --cached startup-config | |
177 | 191 | git commit || git reset HEAD startup-config |
192 | + | |
... | ... |