Skip to content

Commit 546519b

Browse files
committed
add project
0 parents  commit 546519b

File tree

4 files changed

+229
-0
lines changed

4 files changed

+229
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
*.lst

README.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# cisco-backup-git
2+
# Description
3+
This script allows network backups of Cisco switches and routers.
4+
It stores the configs in git repositories.
5+
6+
# How to use
7+
```sh
8+
./cisco-backup-git.sh -f dev.lst -d ../backup/ -n 8 -c "some changes description"
9+
```
10+
11+
```sh
12+
./cisco-backup-git.sh -f dev.lst -d ../backup/ -n "29 30" -c "back up devices with number 29 and 30 from the configuration file"
13+
```
14+
15+
```sh
16+
./cisco-backup-git.sh -f dev.lst -d ../backup/ -c "backup all devices from the configuration file"
17+
```
18+
19+
# cisco backup user example
20+
```
21+
username backup privilege 3 secret 0 PASSWORD
22+
privilege exec all level 3 show running-config
23+
```

cisco-backup-git.sh

Lines changed: 198 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,198 @@
1+
#!/bin/bash
2+
3+
RS=0;
4+
5+
R1="^([0-9]{1,2})@([a-z]+)@([a-z]+)@([a-z]+)@([0-9a-zA-Z\.|]+)@([0-9a-zA-Z\.|#]+)@([0-9A-Za-z\.\-]+)@([0-9]+)$";
6+
7+
SSHC="/usr/bin/ssh -4 -p 22 -oStrictHostKeyChecking=no -oPreferredAuthentications=password ";
8+
SSHC+="-oNumberOfPasswordPrompts=1 -oPubkeyAuthentication=no -oConnectTimeout=5 ";
9+
SSHC+="-oKexAlgorithms=+diffie-hellman-group1-sha1";
10+
11+
# копирует файлы по симлинкам и это, похоже, никак не исправить (rsync?)
12+
SCPC="/usr/bin/scp -4 -r -oStrictHostKeyChecking=no -oPreferredAuthentications=password ";
13+
SCPC+="-oNumberOfPasswordPrompts=1 -oPubkeyAuthentication=no -oConnectTimeout=5";
14+
15+
DEVFILE="";
16+
GITDIR="";
17+
DLST="";
18+
CMNT="";
19+
20+
while getopts ":d:f:n:c:" opt; do
21+
case $opt in
22+
f) DEVFILE="${OPTARG}";;
23+
d) GITDIR="${OPTARG}";;
24+
n) DLST="${OPTARG}";;
25+
c) CMNT="${OPTARG}";;
26+
:) echo "Option -$OPTARG requires an argument." >&2;;
27+
\?) echo "Invalid option: -$OPTARG" >&2;;
28+
esac;
29+
done;
30+
31+
GITDIR=`sed 's/\/$//' <<<"${GITDIR}"`;
32+
GITDIR=`realpath "${GITDIR}"`;
33+
34+
if [ ! -d "${GITDIR}" ]; then
35+
echo "GIT folder does not exit";
36+
else if [ ! -f "${DEVFILE}" ]; then
37+
echo "File not found: "${DEVFILE}"";
38+
else
39+
CHANGES=0;
40+
git -C "${GITDIR}" pull;
41+
if [ ${?} -eq 0 ]; then while read L; do if [[ ${L} =~ ${R1} ]]; then
42+
HNUM="${BASH_REMATCH[1]}";
43+
HTYP="${BASH_REMATCH[2]}";
44+
STYP="${BASH_REMATCH[3]}";
45+
USER="${BASH_REMATCH[4]}";
46+
PASS="${BASH_REMATCH[5]}";
47+
ENBL="${BASH_REMATCH[6]}";
48+
HOST="${BASH_REMATCH[7]}";
49+
PORT="${BASH_REMATCH[8]}";
50+
51+
if [ -n "${DLST}" ]; then
52+
if ! [[ " ${DLST} " =~ " ${HNUM} " ]]; then
53+
continue;
54+
fi;
55+
fi;
56+
57+
FILE=""${GITDIR}"/"${HOST}".cfg";
58+
59+
case "${HTYP}" in
60+
"linux")
61+
case "${STYP}" in
62+
"scp")
63+
mkdir -p "${GITDIR}"/"${HOST}"/ || exit 1;
64+
65+
expc="set timeout 120\n";
66+
expc+="log_user 0\n";
67+
expc+="spawn "${SCPC}" -P "${PORT}" "${USER}"@"${HOST}":/etc/ "${GITDIR}"/"${HOST}"/\n";
68+
expc+="while 1 {\n";
69+
expc+="expect {\n";
70+
expc+="\"*Could not resolve*\" { send_user 'Temporary\ failure\ in\ nameresolution'; exit 1 }\n";
71+
expc+="\"*assword:\" { send -- ""${PASS}""\\\r\\\n }\n";
72+
expc+="\"*refused*\" { send_user 'refused'; exit 1 }\n";
73+
expc+="\"*not known*\" { send_user 'notknown'; exit 1 }\n";
74+
expc+="timeout { send_user 'timeout'; exit 1 }\n";
75+
expc+="eof { exit 0 }\n";
76+
expc+="}\n";
77+
expc+="}\n";
78+
expc+="exit 1\n";
79+
;;
80+
*)
81+
expc="send_user 'unknown'\nexit 1\n";
82+
;;
83+
esac;
84+
;;
85+
"cisco")
86+
case "${STYP}" in
87+
"ssh")
88+
expc="set timeout 6\n";
89+
expc+="log_user 0\n";
90+
expc+="spawn "${SSHC}" "${USER}"@"${HOST}"\n";
91+
expc+="while 1 {\n";
92+
expc+="expect {\n";
93+
expc+="\"*assword:\" { send -- \"""${PASS}""\\\r\\\n\" }\n";
94+
expc+="\"*>\" { send -- \"enable\n\";\sleep 1;\n";
95+
expc+="while 1 {\n";
96+
expc+="expect \"*assword:\" { send -- \"""${ENBL}""\n\"; sleep 1;break }\n";
97+
expc+="expect \"*denied*\" { send_user 'denied'; exit 1 }\n";
98+
expc+="}\n";
99+
expc+="}\n";
100+
expc+="\"*#\" { send -- \"terminal length 0\\\r\\\n\"; sleep 1; break }\n";
101+
expc+="\"*denied*\" { send_user 'denied'; exit 1 }\n";
102+
expc+="\"*refused*\" { send_user 'refused'; exit 1 }\n";
103+
expc+="\"*not known*\" { send_user 'notknown'; exit 1 }\n";
104+
expc+="timeout { send_user 'timeout'; exit 1 }\n";
105+
expc+="eof { send_user 'eof'; exit 1 }\n";
106+
expc+="}\n";
107+
expc+="}\n";
108+
expc+="log_user 1\n";
109+
expc+="expect \"*#\" { send -- \"show running-config view full\\\r\\\n\"; sleep 1;\n";
110+
expc+="expect # { send -- \"exit\\\r\\\n\"; exit 0 }\n";
111+
expc+="}\n";
112+
expc+="log_user 0\n";
113+
expc+="exit 1\n";
114+
;;
115+
"tel")
116+
expc="set timeout 3\n";
117+
expc+="log_user 0\n";
118+
expc+="spawn telnet "${HOST}"\n";
119+
expc+="while 1 {\n";
120+
expc+="expect {\n";
121+
expc+="\"*sername:\" { send -- ""${USER}""\\\r }\n";
122+
expc+="\"*assword:\" { send -- ""${PASS}""\\\r }\n";
123+
expc+="\"*denied*\" { send_user 'denied'; exit 1 }\n";
124+
expc+="\"*failed*\" { send_user 'denied'; exit 1 }\n";
125+
expc+="\"*refused*\" { send_user 'refused'; exit 1 }\n";
126+
expc+="\"*not known*\" { send_user 'notknown'; exit 1 }\n";
127+
expc+="\"*>\" { send_user 'permission'; exit 1 }\n";
128+
expc+="\"*#\" { send -- \"terminal length 0\\\r\"; break }\n";
129+
expc+="timeout { send_user 'timeout'; exit 1 }\n";
130+
expc+="}\n";
131+
expc+="}\n";
132+
expc+="log_user 1\n";
133+
expc+="expect \"*#\" { send -- \"show running-config view full\\\r\" }\n";
134+
expc+="expect \"*nvalid input*\" { send -- \"show running-config\\\r\" }\n";
135+
expc+="expect # { send -- \"exit\\\r\"; exit 0 }\n";
136+
expc+="log_user 0\n";
137+
expc+="exit 1\n";
138+
;;
139+
*)
140+
expc="send_user 'unknown'\nexit 1\n";
141+
;;
142+
esac;
143+
;;
144+
*)
145+
expc="send_user 'unknown'\nexit 1\n";
146+
;;
147+
esac;
148+
149+
size=0;
150+
outex=$(echo -e "${expc}" | /usr/bin/expect -nN -f -);
151+
152+
if [ ${?} -eq 0 ]; then
153+
case "${HTYP}" in
154+
"linux")
155+
case "${STYP}" in
156+
"scp")
157+
size=$(du -bs "${GITDIR}"/"${HOST}" | cut -f 1);
158+
if [ ${size} -ge 5000 ]; then
159+
echo "OK: "${HOST}"";
160+
git -C "${GITDIR}" add "${HOST}";
161+
CHANGES=1;
162+
else
163+
echo "ERROR: "${HOST}" status: size: "${size}"";
164+
RS=1;
165+
fi;
166+
;;
167+
esac;
168+
;;
169+
*)
170+
echo "${outex}" | sed -n '/!/,/^end/p' | egrep -v "ntp clock-period" > "${FILE}";
171+
size=$(wc -c <"${FILE}");
172+
if [ ${size} -ge 3100 ]; then
173+
echo "OK: "${HOST}"";
174+
git -C "${GITDIR}" add "${FILE}";
175+
CHANGES=1;
176+
else
177+
echo "ERROR: "${HOST}" status: size: "${size}"";
178+
RS=1;
179+
fi;
180+
;;
181+
esac;
182+
else
183+
echo "ERROR: "${HOST}" status: "${outex}"";
184+
RS=1;
185+
fi;
186+
else
187+
echo "ERROR: Wrong device's string: "${L}""
188+
RS=1;
189+
fi;
190+
done < <(egrep -v "^( +)?#.*$|^$" "${DEVFILE}" | sort -u);
191+
if [ ${CHANGES} -eq 1 ]; then
192+
git -C "${GITDIR}" commit -m "$(hostname).$(dnsdomainname) $(date +%Y-%m-%d_%H.%M.%S) ${CMNT}" && git -C "${GITDIR}" push;
193+
fi;
194+
fi;
195+
fi;
196+
fi;
197+
198+
exit ${RS};

lst.example

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# NUMBER@DEVICE_TYPE@CONNECTION_PROTOCOL@DEVICE_USER@USER_PASSWORD@ENABLE_PASSWORD@DEVICE_ADDRESS@DEVICE_PORT
2+
1@cisco@ssh@USER@PASSWORD@[email protected]@22
3+
2@cisco@ssh@USER@PASSWORD@ENABLEPASS@router1@22
4+
3@cisco@ssh@USER@PASSWORD@ENABLEPASS@router2@22
5+
4@cisco@ssh@USER@PASSWORD@[email protected]@22
6+
5@cisco@tel@USER@PASSWORD@none@switch2@23
7+
6@cisco@tel@USER@@PASSWORD@[email protected]@23

0 commit comments

Comments
 (0)