-
Notifications
You must be signed in to change notification settings - Fork 5
/
chIP.sh
110 lines (94 loc) · 3.37 KB
/
chIP.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#!/bin/bash
# be careful, and dont run blindly.
function MENU
{
echo "
run this on the destination server.
script to swap IP addresses between two cPanel servers.
!!this script assumes ifcfg-eth0 is your default interface!!
REQUIRED OPTIONS:
-s <192.168.1.100>
set the source ip address.
OPTIONS:
-p <2222>
set the source port, defaults to 22.
Ex: ./chip.sh -s 127.0.0.1 -p 2222
Ex: ./chip.sh -s 127.0.0.1 -k 1
"
exit 1
}
while getopts ":s:p:k:h" opt; do
case $opt in
s)
sourceip=$OPTARG
flag="s"
;;
p)
sourceport=$OPTARG
flag="p"
;;
h)
MENU
flag="h"
;;
\?) echo "invalid option: -$OPTARG"; echo; MENU;;
:) echo "option -$OPTARG requires an argument."; echo; MENU;;
esac
done
if [[ `whoami` != "root" ]] # verifying root login
then
echo "You're not root."
exit 1
fi
function !validateIdiocracy
{=
if [[ ssh $sourceip -p $sourceport -f /root/network-dst.tar.gz ]] -a [[ ssh $sourceip -p $sourceport -f /root/network-src.tar.gz ]]; then
return 1 # SUCCESS
else
return 0 # FAILURE
fi
# needs more validation
}
function validateSwap
{=
## Josh write this
## function generates multiple arrays based on tarballs on dst system
## arrays are also generated using current data in network-scripts
## if somdething is wrong the script is terminated
return 1
}
if [[ $# -eq 0 || -z $sourceip ]]; then MENU; fi # check for existence of required var
if [ -z $sourceport ]; then sourceport=22; fi # apply port 22 if none is set
if [ ! -z $sshkey ]; then setup_sshkey; fi # gen ssh key if not set
$ifcfg = "/etc/sysconfig/network-scripts/ifcfg-eth0"
# remove uuid from dst before created tarballs for source
# centos5 does not need these values to maintain interface names
sed '/HWADDR/d' $ifcfg
sed '/UUID/d' $ifcfg
# create tars and work magic
tar -czf /root/network-dst.tar.gz /etc/hosts /etc/ips /etc/sysconfig/network /etc/sysconfig/network-scripts/ifcfg-eth0 /var/cpanel/mainip
ssh root@$sourceip -p $sourceport "tar -czf /root/network-src.tar.gz /etc/hosts /etc/ips /etc/sysconfig/network /etc/sysconfig/network-scripts/ifcfg-eth0 /var/cpanel/mainip"
# grab src and refresh domainips if it doesnt exist
rsync -avz -e "ssh -p '$sourceport'" root@$sourceip:/root/network-src.tar.gz /root/
rsync -avz -e "ssh -p '$sourceport'" root@$sourceip:/etc/domainips /etc/domainips-src
rsync -avz -e "ssh -p '$sourceport'" /root/network-dst.tar.gz root@$sourceip:/root/network-dst.tar.gz
if [[ -e /root/network-dst.tar.gz ]] -a [[ -e /root/network-src.tar.gz ]]; then
if [[ $ifcfg == *"UUID"* ]] -o [[ $ifcfg == *"IPADDR"* ]]## if hwaddr or uuid exist ifcfg, replace with eth0 data
sed '/HWADDR/d' $ifcfg
sed '/UUID/d' $ifcfg
cat "HWADDR=" ip a l | grep IPADDR $ifcfg | egrep -o '(".*?")' | sed 's/\"//g' -B1 | grep ether | awk {'print$2'} >> $ifcfg
else ## may need elif
cat "HWADDR=" ip a l | grep IPADDR $ifcfg | egrep -o '(".*?")' -B1 | grep ether | awk{'print$2'} >> $ifcfg
fi
fi
tar -xf network-src.tar.gz -C /
ssh root@$sourceip -p $sourceport "tar -xf network-dst.tar.gz -C /"
if [[ !validateIdiocracy -eq 1 ]] -a [[ validateSwap -eq 1 ]]; then
echo "Please triple check and verify everything is correct.\nThen restart networking on both systems\n."
read
wget post-ipswap.sh
sh post-ipswap.sh
else
echo "We were unable to verify consistency of IP swap."
echo "Please manually validate and proceed with network restart"
fi