Commit f8bd93e92f16127cc7274cb7f33f064a4fe3e3f4

Authored by Frederik Lindenaar
2 parents 5f30d259 12517d18

Merge branch 'master' of https://gitlab.lindenaar.net/scripts/cisco into HEAD

Showing 2 changed files with 105 additions and 20 deletions
README.md
... ... @@ -14,15 +14,17 @@ License, see [below](#license)
14 14  
15 15 sync-router
16 16 -----------
17   -Shell scripts used to synchronize the current Cisco startup configuration and
18   -DHCP static lease files with with a GIT repository. This scripts assumes one
19   -(git-managed) subdirectory per router. It performs the following operations when
20   -invoked:
  17 +Shell scripts used to synchronize the current Cisco startup configuration, IOS
  18 +images and DHCP static lease files with with a GIT repository. This scripts
  19 +assumes one (git-managed) subdirectory per router. It performs the following
  20 +operations when invoked:
21 21  
  22 + - copy Cisco startup-config using scp and add to next commit
  23 + - synchronize the IOS images in the git repository and update the startup-
  24 + config accordingly
22 25 - update the header of modified DHCP static lease files, upload them using
23 26 using scp and add them to the next commit
24 27 - restart Cisco DHCP service after updating DHCP static lease files
25   - - copy Cisco running-config using scp and add to next commit
26 28 - commit changes to the git repository
27 29  
28 30 I use this script to keep the configuration of my cisco router(s) in a GIT
... ...
sync-router
... ... @@ -6,9 +6,11 @@
6 6 # them using using scp and add them to the next commit
7 7 # - restart Cisco DHCP service after updating static lease files
8 8 # - copy Cisco startup-config using scp and add to next commit
  9 +# - update IOS image files on the router when checked in to the
  10 +# repository, update startup-config file accordingly on router
9 11 # - commit changes to the git repository
10 12 #
11   -# Version 1.0, latest version at: https://gitlab.lindenaar.net/scripts/cisco
  13 +# Version 1.1, latest version at: https://gitlab.lindenaar.net/scripts/cisco
12 14 #
13 15 # Copyright (c) 2016 Frederik Lindenaar
14 16 #
... ... @@ -25,22 +27,73 @@
25 27  
26 28 ### Configuration ###
27 29 router=`basename \`dirname $0 | pwd\`` # name of router - based on directory
28   -syncfiles=dhcp-\* # list/pattern of dhcp lease files
29   -fileto=flash: # location of dhcp lease files on cisco
  30 +dhcpfiles=dhcp-\* # list/pattern of dhcp lease files
  31 +imagefiles=c??00-universalk9-mz.SPA.\* # list/pattern of IOS image files
  32 +fileto=flash # location of dhcp/IOS files on cisco
30 33  
31 34 ### Implementation ###
32 35 echo updating with $router
33 36  
  37 +# Support function to download a file from the router
  38 +# parameters: $1 - filename to be copied and $2 - (optional) target filename
  39 +# when no target filename is provided the source file will be downloaded to the
  40 +# current directory with the same name. Adds downloaded file to git changeset
  41 +router_file_download() {
  42 + fromfile=${1?ERROR: at least one filename is required to copy from router}
  43 + tofile=${2:-$fromfile}
  44 + filesrc=$fileto
  45 + if [ "$fromfile" == startup-config ]; then
  46 + filesrc=nvram
  47 + fi
  48 + echo downloading $tofile from router $filesrc
  49 + scp -q $router:$filesrc:$fromfile $tofile
  50 + git add $tofile
  51 +}
  52 +
  53 +# Support function to upload a file to the router
  54 +# parameters: $1 - filename to be copied and $2 - (optional) target filename
  55 +# when a target filename is provided the source file will be moved to the target
  56 +# file after uploading and the target file is added to the git changeset
  57 +router_file_upload() {
  58 + fromfile=${1?ERROR: at least one filename is required to copy to router}
  59 + tofile=${2:-$fromfile}
  60 + filedst=$fileto
  61 + if [ "$fromfile" == startup-config ]; then
  62 + filedst=nvram
  63 + fi
  64 + echo uploading new/updated $tofile to router $filedst
  65 + scp -q $fromfile $router:$filedst:$tofile
  66 + if [ "$fromfile" != "$tofile" ]; then
  67 + mv $fromfile $tofile
  68 + git add $tofile
  69 + fi
  70 +}
  71 +
  72 +# Support function to remove a file from the router
  73 +# parameters: $1 - filename to be removed
  74 +router_file_remove() {
  75 + delfile=${1?ERROR: need a filename to remove from router}
  76 + echo removing $delfile as it is no longer in the repository
  77 + ssh -q $router "delete /force $fileto:$delfile"
  78 +}
  79 +
  80 +
  81 +# Fetch the start-up configuration from the router
  82 +router_file_download startup-config
  83 +
34 84 # Process the DHCP static lease files that have changed
35   -git status -s "$syncfiles" | while read status filename
  85 +git status -s "$dhcpfiles" | while read status filename token newfilename
36 86 do
37 87 case $status in
38 88 \?\?)
39 89 echo skipping $filename as it is not yet added to the repository
40 90 ;;
41 91 D)
42   - echo removing $filename as it is no longer in the repository
43   - ssh -q $router "delete /force $fileto$filename"
  92 + router_file_remove $filename
  93 + ;;
  94 + R)
  95 + router_file_remove $filename
  96 + router_file_upload $newfilename
44 97 ;;
45 98 A |M|MM)
46 99 echo updating and uploading modified file $filename
... ... @@ -60,9 +113,7 @@ do
60 113 esac
61 114 done
62 115 tail +3 $filename >> .$filename.$$.tmp
63   - scp -q .$filename.$$.tmp $router:$fileto$filename
64   - mv .$filename.$$.tmp $filename
65   - git add $filename
  116 + router_file_upload .$filename.$$.tmp $filename
66 117 ;;
67 118 *)
68 119 echo unsupported git status "$status", aborting
... ... @@ -72,7 +123,7 @@ do
72 123 done
73 124  
74 125 # Restart the DHCP service on the router if any of the dhcp files changed
75   -if git status -s "$syncfiles" | egrep -q ^[MAD]; then
  126 +if git status -s "$dhcpfiles" | egrep -q ^[MAD]; then
76 127 echo restarting dhcp service
77 128 cat << EOT | ssh -q $router
78 129 configure terminal
... ... @@ -83,10 +134,42 @@ exit
83 134 EOT
84 135 fi
85 136  
86   -# Fetch the current start-up configuration and add it to the repository
87   -scp -q $router:startup-config .
88   -git diff startup-config
89   -git add startup-config
90 137  
91   -# and commit the changes (or unadd new configuration when commit is aborted)
  138 +# Process the IOS image files that have changed
  139 +git status -s "$imagefiles" | while read status filename token newfilename
  140 +do
  141 + case $status in
  142 + \?\?)
  143 + echo skipping $filename as it is not yet added to the repository
  144 + ;;
  145 + R)
  146 + router_file_remove $filename
  147 + router_file_upload $newfilename
  148 + ;;
  149 + D)
  150 + router_file_remove $filename
  151 + ;;
  152 + A |M)
  153 + router_file_upload $filename
  154 + ;;
  155 + *)
  156 + echo unsupported git status "$status", aborting
  157 + exit 1
  158 + ;;
  159 + esac
  160 +done
  161 +
  162 +# Update the boot images in the startup-config file if we're updating any images
  163 +if git status -s "$imagefiles" | egrep -q ^[MADR]; then
  164 + fgrep -n "boot system $fileto" startup-config | cut -d: -f1 > .startup-config.$$.lines
  165 + head -$[ `head -1 .startup-config.$$.lines` -1 ] startup-config > .startup-config.$$
  166 + git ls-files $imagefiles | sort -r | sed "s/^/boot system $fileto /g" >> .startup-config.$$
  167 + tail +$[ `tail -1 .startup-config.$$.lines` +1 ] startup-config >> .startup-config.$$
  168 + rm .startup-config.$$.lines
  169 + router_file_upload .startup-config.$$ startup-config
  170 +fi
  171 +
  172 +
  173 +# show what has changed in the startup config and commit to the repository
  174 +git diff --cached startup-config
92 175 git commit || git reset HEAD startup-config
... ...