shell foundation of the IP changes

Claim:

  Show all network cards and their corresponding IP addresses, and IP-related content change to take effect.

. 1 ! # / Bin / the bash
 2  # NIC access to the machine and the IP
 . 3  the while :
 . 4  do 
. 5 Network = $ ( the ifconfig | grep BROAD | awk -F: ' {}. 1 Print $ ' )
 . 6  echo -e " native NIC \ TIP " 
. 7  for single_network in $ ( echo $ Network); do 
. 8      all_ip = $ ( the ifconfig $ single_network | grep - W  " inet " | awk  'Print 2} $ { ' )
 9      echo -e " $ single_network \ t \ t $ all_ip " 
10  DONE 
11  # get the card you want to change the name of
 12  the while :
 13  do 
14 the Read -p " Please enter the name of the card you want to change (the default exit ): " new_network
 15      IF [the -z $ new_network]; the then 
16      echo  " exit script " 
17      exit
 18      the else 
19      judge_new_network =` echo  " $ Network " | grep - w " $ New_network " `
 20          IF [the -z " $ judge_new_network " ]; the then 
21          echo  " ! Please enter the correct name card " 
22          the else 
23          BREAK
 24-          fi 
25      fi     
26  DONE     
27  # to change IP, mask, gateway
 28 = IPZZ " ^ ([0-9] \ |. [1-9] [0-9] \ |.. 1 [0-9] [0-9] \ |. 2 [0-4] [0-9 ] \ |.. 25 [0-5 ] \) {3} ([1-9] | [1-9] [0-9] | 1 [0-9] [0-9] | 2 [0- 4] [0-9] | 25 [0-4]) $ " 
29 netmask_ZZ = "^ ([0-9] \ |. [1-9] [0-9] \ |. 1 [0-9] [0-9] \ |. 2 [0-4] [0-9] \. | 25 [0-5] \) { 3} ([0-9] |. [0-9] [0-9] | 1 [0-9] [0-9] | 2 [0-4] [ 0-9] | 25 [0-5]) $ " 
30  # enter the new IP address
 31 is  the while :
 32  do 
33 is Read -p " Please enter a new IP address: " new_ip
 34 is      IF [$ new_ip the -Z]; the then 
35      echo  " IP address can not be empty, please re-enter! " 
36      elif [[~ $ $ = new_ip IPZZ]]; the then 
37 [      BREAK
 38 is      the else 
39      echo  " Please enter the correct IP address! " 
40      Fi 
41 is  DONE 
42 is # Input mask
 43 is  the while :
 44 is  do 
45 Read -p " Please enter a new mask (default 255.255.255.0): " new_netmask
 46 is      IF [$ new_netmask the -Z]; the then 
47      new_netmask = 255.255 . 255.0 
48      BREAK
 49      elif [[~ $ $ = new_netmask netmask_ZZ]]; the then 
50      BREAK
 51 is      the else 
52 is      echo  " ! Please input the correct mask " 
53 is      Fi 
54 is  DONE 
55  # input gateway
 56 is  the while :
 57 is do 
58 Read -p " Please enter a new gateway: " new_gateway
 59      IF [$ new_gateway the -Z]; the then 
60      echo  " ! Gateway can not be empty, please re-enter " 
61 is      elif [[~ $ $ = new_gateway IPZZ]]; the then 
62      BREAK
 63      the else 
64-      echo  " Please enter the correct gateway! " 
65      fi 
66  DONE 
67  # into the configuration file
 68  echo  " NAME = $ new_network 
69 the DEVICE = $ new_network
 70 ONBOOT = yes
 71 BOOTPROTO=none
72 IPADDR=$new_ip
73 NETMASK=$new_netmask
74 GATEWAY=$new_gateway
75 DNS1=114.114.114.114
76 DNS2=8.8.8.8" > /etc/sysconfig/network-scripts/ifcfg-$new_network
77 #重启服务
78 systemctl restart network
79 done

Guess you like

Origin www.cnblogs.com/renyz/p/11305329.html