An instance of the IP setup script under Linux and the solution to the problem encountered

  • 2020-06-12 11:37:17
  • OfStack

Examples of IP setup scripts under Linux and solutions to problems encountered

background

1 Java web project has one function of IP modification. The IP modification script of Linux is as follows:


#!/bin/bash 
 
#useing parameter ip:netmask:gateway:dns1  
#system version:centos6,7/redhat6,7 
 
#read parameter 
ipaddr=`echo $1|cut -d ":" -f 1` 
netmask=`echo $1|cut -d ":" -f 2` 
gateway=`echo $1|cut -d ":" -f 3` 
dns1=`echo $1|cut -d ":" -f 4` 
dns2=`echo $1|cut -d ":" -f 5` 
targetDevice=$2 
 
#get device(active) name -- centos6 and centos7 
DEVICE=`dmesg|grep Link|grep Up|cut -d ":" -f 3,4|cut -d " " -f 2|cut -d ":" -f 1|uniq|head -n 1` 
#if parameter has device name,use it ,else use first alive device 
if [ "$targetDevice" != "" ]; then 
  DEVICE=$targetDevice 
  echo 'device use target device :' $DEVICE 
fi 
 
#backup net_file 
mv  /etc/sysconfig/network-scripts/'ifcfg-'$DEVICE  /etc/sysconfig/network-scripts/'ifcfg-'$DEVICE'.bak' -f 
 
#config(net_file) 
cat <<end>> /etc/sysconfig/network-scripts/'ifcfg-'$DEVICE 
DEVICE=$DEVICE 
ONBOOT=yes 
TYPE=Ethernet 
BOOTPROTO=none 
IPV6INIT=no 
DEFROUTE=yes 
IPADDR=$ipaddr 
GATEWAY=$gateway 
NETMASK=$netmask 
DNS1=$dns1 
DNS2=$dns2 
end 
 
#update /etc/hosts 
cat /sensor/bin/standardhosts>/etc/hosts  
echo '127.0.0.1 '$(hostname)>>/etc/hosts 
 
#grant the net_file 
chmod 755 /etc/sysconfig/network-scripts/'ifcfg-'$DEVICE 
 
#restart network 
/etc/init.d/network restart 


In fact, during the execution of this shell script, we encountered several problems related to the configuration of the target server's network services. There are 4 different types of errors, sorted out below for later use. The essence of the script is to write the destination IP information to the network card file ifcfg-eth2. (eth2 is the name of the communication card on the test machine), which needs to be passed to the script as a parameter.

Question 1

Problems encountered during the execution of this script while testing on a virtual machine. The local installation is VirtualBox, which copies the nodes of five virtual machines. Based on this background, after executing the script, restart the network card service service network restart command, always encountering failure, failure message prompt:

Error:No suitable device found: no device found for connection "System eth0"

This is because the virtual machine copied the network card during replication, but es37EN-ES38en actually shows only one network card. Solutions:

Step 1: Execute ifconfig. The card name of the current system. My current vm node is eth2,lo.

Step 2: Keep ifcfg-eth2 and ES55en-ES56en in /etc/sysconfig/ ES51en-ES52en, and delete other network card files that do not exist. The copied eth0, rm? ifcfg-eth0 files.

Step 3, ifcfg-eth2 file content, ensure that DEVICE=eth2 parameter name and file name 1.

Step 4, modify the network card policy file vi etc/udev/rules d / 70 - persistent - net rules find eth2 network adapter configuration, fixed MAC address and eth2 MAC 1.

Step 5: Verify: Perform service network restart to verify that the network card has been restarted successfully.

Question 2


nm_object_array_demarshal: couldn't create object for /org/freedesktop/NetworkManager/ActiveConnection/39
Error: Obtaining active connection for'/org/freedesktop/NetworkManager/ActiveConnection/39' failed.
                             [FAILED]

Question 3


 See  ' systemctlstatus network.service '  and  ' journalctl-xn '  for details

For this problem, execute journalctl? xe > /home/journalctl.log

Look in the log file and find the real error is it:


Bringing up interface enp4s0f0: RTNETLINK answers: File exists

Question 4


Bringing up interface eth0: Device eth0 does not seem to be present,delaying initialization. 

centos 2. The failure of the 3. 43 1 sample at the root of the problem, two is to start the network services have conflict: / etc/init d/network and/etc init d/NetworkManager conflict with these two services.

Solution: Turn off the NetworkManager service by ordering service NetworkManager stop. Perform service network restart verification to restart the network card service normally.

conclusion

Verify the IP setup script and provide two parameters: 1 is IP information and network card name. Execute the following command:

sh reset_ip. sh 192.168.10.170:255.255.255.0:192.168.10.1:10.0.1.90 eth2 IP address can modify the virtual machine. A simple IP setup script, the execution of the process of almost the network with the network card related problems, have encountered. Various attempts have finally been made to find the cause, and the main source is the problem with NetworkManage.

Thank you for reading, I hope to help you, thank you for your support to this site!


Related articles: