Cobbler Server:自动修改IP设定
Cobbler Server所在的主机如果IP地址改掉了,那么Cobbler服务端也是需要做出相应变更的。
下面的脚本可以让你一键完成所有关于IP变更的配置:
使用方法:
auto_cobbler_change_ip.sh [新的IP地址]
如:
auto_cobbler.sh192.168.23.3
就好了。
代码脚本如下:
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 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 |
#!/bin/bash # var purpose_ip="$1" purpose_ip_3slot=$(echo $purpose_ip | awk -F'.' '{print $1"."$2"."$3}') # display echo "Purpose IP = [$purpose_ip]" echo "3 Slot is: [$purpose_ip_3slot]" # function function modify_file_by_search() { # var target_filename="$1" string_search="$2" split_by=$3 string_purpose="$4" slot_number=$(echo "$string_purpose" | awk -F'.' -v count=0 '{for(i=1;i<=NF;i++){count=i} print count}') # display echo "==========================" echo "File: $target_filename" echo "Search: $string_search" echo "Split by: [$split_by]" echo "Purpose is = [$string_purpose]" echo "Slot number = [$slot_number]" # var by compute #string_content=$(cat $target_filename | grep "$string_search" | cut -d $split_by -f2) string_content=$(cat $target_filename | grep "$string_search" | cut -d $split_by -f2 | awk -F'.' -v slot_num=$slot_number '{for(i=1;i<=slot_num;i++){ printf $i"."} print "\n"}' | rev | cut -d' ' -f1 | rev | sed 's/.$//') # display echo "Old content is --> [$string_content]" # do echo "-------" echo "do modify" sed -i "/$string_search/s/$string_content/$string_purpose/g" $target_filename # after #string_content=$(cat $target_filename | grep "$string_search" | cut -d $split_by -f2 | rev | cut -d' ' -f1 | rev) string_content=$(cat $target_filename | grep "$string_search" | cut -d $split_by -f2 | awk -F'.' -v slot_num=$slot_number '{for(i=1;i<=slot_num;i++){ printf $i"."} print "\n"}' | rev | cut -d' ' -f1 | rev |sed 's/.$//') echo "-------" echo "after modify" echo "Content is --> [$string_content]" } function modify_file_by_search_1() { # var target_filename="$1" #string_search="$2" #split_by=$3 string_purpose="$2" slot_number=$(echo "$string_purpose" | awk -F'.' -v count=0 '{for(i=1;i<=NF;i++){count=i} print count}') # display echo "==========================" echo "File: $target_filename" #echo "Search: $string_search" #echo "Split by: [$split_by]" echo "Purpose is = [$string_purpose]" echo "Slot number = [$slot_number]" # var by compute #string_content=$(cat $target_filename | grep "$string_search" | cut -d $split_by -f2) string_content=$(cat $target_filename | grep "subnet" | grep "netmask" | grep "{" | awk '{print $2}' | awk -F'.' -v slot_num=$slot_number '{for(i=1;i<=slot_num;i++){ printf $i"."} print "\n"}' | rev | cut -d' ' -f1 | rev | sed 's/.$//') # display echo "Old content is --> [$string_content]" # do echo "-------" echo "do modify" sed -i "/subnet/s/$string_content/$string_purpose/g" $target_filename # after #string_content=$(cat $target_filename | grep "$string_search" | cut -d $split_by -f2 | rev | cut -d' ' -f1 | rev) string_content=$(cat $target_filename | grep "subnet" | grep "netmask" | grep "{" | awk '{print $2}' | awk -F'.' -v slot_num=$slot_number '{for(i=1;i<=slot_num;i++){ printf $i"."} print "\n"}' | rev | cut -d' ' -f1 | rev | sed 's/.$//') echo "-------" echo "after modify" echo "Content is --> [$string_content]" } function modify_file_by_search_2() { # var target_filename="$1" string_search="$2" #split_by=$3 string_purpose="$3" slot_number=$(echo "$string_purpose" | awk -F'.' -v count=0 '{for(i=1;i<=NF;i++){count=i} print count}') # display echo "==========================" echo "File: $target_filename" echo "Search: $string_search" #echo "Split by: [$split_by]" echo "Purpose is = [$string_purpose]" echo "Slot number = [$slot_number]" # var by compute #string_content=$(cat $target_filename | grep "$string_search" | cut -d $split_by -f2) string_content=$(cat $target_filename | grep "$string_search" | awk '{print $3}' | grep -v "iface" | awk -F'.' -v slot_num=$slot_number '{for(i=1;i<=slot_num;i++){ printf $i"."} print "\n"}' | rev | cut -d' ' -f1 | rev | sed 's/.$//') # display echo "Old content is --> [$string_content]" # do echo "-------" echo "do modify" sed -i "/$string_search/s/$string_content/$string_purpose/g" $target_filename # after #string_content=$(cat $target_filename | grep "$string_search" | cut -d $split_by -f2 | rev | cut -d' ' -f1 | rev) string_content=$(cat $target_filename | grep "$string_search" | awk '{print $3}' | grep -v "iface" | awk -F'.' -v slot_num=$slot_number '{for(i=1;i<=slot_num;i++){ printf $i"."} print "\n"}' | rev | cut -d' ' -f1 | rev | sed 's/.$//') echo "-------" echo "after modify" echo "Content is --> [$string_content]" } function modify_file_by_search_3() { # var target_filename="$1" string_search="$2" #split_by=$3 string_purpose="$3" slot_number=$(echo "$string_purpose" | awk -F'.' -v count=0 '{for(i=1;i<=NF;i++){count=i} print count}') # display echo "==========================" echo "File: $target_filename" echo "Search: $string_search" #echo "Split by: [$split_by]" echo "Purpose is = [$string_purpose]" echo "Slot number = [$slot_number]" # var by compute #string_content=$(cat $target_filename | grep "$string_search" | cut -d $split_by -f2) string_content=$(cat $target_filename | grep "$string_search" | awk '{print $2}' | awk -F'.' -v slot_num=$slot_number '{for(i=1;i<=slot_num;i++){ printf $i"."} print "\n"}' | rev | cut -d' ' -f1 | rev | sed 's/.$//') # display echo "Old content is --> [$string_content]" # do echo "-------" echo "do modify" sed -i "/$string_search/s/$string_content/$string_purpose/g" $target_filename # after #string_content=$(cat $target_filename | grep "$string_search" | cut -d $split_by -f2 | rev | cut -d' ' -f1 | rev) string_content=$(cat $target_filename | grep "$string_search" | awk '{print $2}' | awk -F'.' -v slot_num=$slot_number '{for(i=1;i<=slot_num;i++){ printf $i"."} print "\n"}' | rev | cut -d' ' -f1 | rev | sed 's/.$//') echo "-------" echo "after modify" echo "Content is --> [$string_content]" } # settings modify_file_by_search "/etc/cobbler/settings" "^server" ':' $purpose_ip modify_file_by_search "/etc/cobbler/settings" "^next_server" ':' $purpose_ip # dhcp modify_file_by_search_3 "/etc/dhcp/dhcpd.conf" "next-server" $purpose_ip modify_file_by_search_1 "/etc/cobbler/dhcp.template" $purpose_ip_3slot modify_file_by_search_1 "/etc/dhcp/dhcpd.conf" $purpose_ip_3slot modify_file_by_search_2 "/etc/cobbler/dhcp.template" "option routers" $purpose_ip_3slot modify_file_by_search_2 "/etc/dhcp/dhcpd.conf" "option routers" $purpose_ip_3slot modify_file_by_search_2 "/etc/cobbler/dhcp.template" "option domain-name-servers" $purpose_ip_3slot modify_file_by_search_2 "/etc/dhcp/dhcpd.conf" "option domain-name-servers" $purpose_ip_3slot modify_file_by_search_2 "/etc/cobbler/dhcp.template" "range dynamic-bootp" $purpose_ip_3slot modify_file_by_search_2 "/etc/dhcp/dhcpd.conf" "range dynamic-bootp" $purpose_ip_3slot |
——————————————
Done。