Linux Tutorials

CentOS 7 Run Script When Community Interface is Up (Community Supervisor)

SEOClerks

I need to run a shell script or different program instantly after my community interface goes up on a CentOS 7 / RHEL 7 server. I created /sbin/ifup-local, however it’s not engaged on a CentOS 7 field however works completely on CentOS 6. I’m utilizing NetworkManager with CentOS 7. How do I run ifup-post or ifup-pre scripts?

Introduction: The ifup and pals script don’t run if NetworkManager is enabled. The default is to permit NetworkManager on a CentOS or RHEL 7 server. To resolve this downside, you’ll be able to disable NetworkManager and use all current scripts from CentOS 6.x or earlier. Nevertheless, if you should use NetworkManager, you should place scripts in /and so forth/NetworkManager/dispatcher.d/ listing. Allow us to see use each strategies to run a script or program instantly after your community interface goes up.

CentOS 7 Run Script When Community Interface is Up When Utilizing Community Supervisor (nm)

On this technique # 1, you’ll use a Community Supervisor (nm) to run a script referred to as ifup-local when interface named wlp4s0 goes up and operating.

An instance of an ifup-local script utilizing the bash/sh case statment

Create a script referred to as /and so forth/NetworkManager/dispatcher.d/ifup-local on a CentOS / RHEL 7 field utilizing the nano command or vim command:
$ sudo vim /and so forth/NetworkManager/dispatcher.d/ifup-local
Append the next code:

#!/bin/sh
# Function: Setup extra ip or routing when wlp4s0 goes up beneath GPL v.three.x+
# Creator: Vivek Gite {https://www.cyberciti.biz/
# —————————————————————————–
 
INF=“$1” # your present interface identify corresponding to eth0, wlp4s0 and so forth
STA=“$2” # standing corresponding to UP or DOWN
 
# Ship message to /var/log/messages
logger “$zero referred to as for interface named $INF with $STA …”
 
# We simply need to work on wlp4s0 interface
# When matched referred to as one other script referred to as /root/bin/setup.wlp4s0.ips
case $INF in
wlp4s0) logger “Organising $INF ip deal with…”; /root/bin/setup.wlp4s0.ips ;;
esac
exit zero

Save and shut the file in vim textual content editor. Ensure you setup appropriate permission in your script:
$ chmod +x /and so forth/NetworkManager/dispatcher.d/ifup-local
Here’s a pattern /root/bin/setup.wlp4s0.ips displayed utilizing the cat command:
$ sudo cat /root/bin/setup.wlp4s0.ips
Pattern outputs:

#!/bin/bash
# Function: Setup extra ip or routing beneath GPL v.three.x+
# Creator: Vivek Gite https://www.cyberciti.biz/
# —————————————————————————–
#/sbin/iptables -I FORWARD 1 -m state -s 192.168.2.zero/24 -d 192.168.122.zero/24 –state NEW,RELATED,ESTABLISHED -j ACCEPT
#
INF=“$1” # your present interface identify corresponding to eth0, wlp4s0 and so forth
STA=“$2” # standing corresponding to UP or DOWN
 
# Ship message to /var/log/messages
logger “$zero referred to as for interface named $INF with $STA …”
 
if [ $INF == “wlp4s0” ]
then
begin=201
for i in zero..10
do
/usr/sbin/ifconfig wlp4s0:$ 192.168.2.$begin
(( begin++ ))
performed
fi

Each scripts are just about self-documenting and shortly developed or modified anybody. Each scripts are just about self-documenting and shortly developed or modified anybody. The script /and so forth/NetworkManager/dispatcher.d/ifup-local run every time an interface named wlp4s0 up. The script-based config stays persistent throughout reboots and when community supervisor/service restarts. Here’s a pattern log from /var/log/messages displayed utilizing tail command or grep command/egrep command generated by logger command:
# tail -f /var/log/messages
# egrep -i ‘Organising|ifup-local’ /var/log/messages
CentOS 7 Run Script When Network Interface is Up with nmClick on to enlarge picture

A be aware about creating /sbin/ifdown-local when community interface goes down

When a community interface goes down /sbin/ifdown-local runs. Here’s a pattern script:

#!/bin/bash
# Function: Take away extra ip or routing beneath GPL v.three.x+
# Creator: Vivek Gite https://www.cyberciti.biz/
# —————————————————————————–
# take down all ips and delete routing if any
begin=201
for i in zero..10
do
/usr/sbin/ifconfig wlp4s0:$ down
(( begin++ ))
performed

CentOS 7 disable Community Supervisor (nm) and run ifup-local script

To disable nm set NM_CONTROLLED to no in /and so forth/sysconfig/network-scripts/ifcfg-wlp4s0:
# vi /and so forth/sysconfig/network-scripts/ifcfg-wlp4s0
Append the next
NM_CONTROLLED=no
Save and shut the file. If set to ‘no’, NetworkManager will ignore this connection/gadget and thus it would run /sbin/ifup-local. By defaults NM_CONTROLLED set to ‘sure’. Here’s a pattern /sbin/ifup-local:

#!/bin/sh
# Examined on a CentOS and RHEL model 5/6/7
# Creator: Creator: Vivek Gite https://www.cyberciti.biz/
# ——————————————————-
#!/bin/sh
IN=“$1”
STATUS=“$2”
case “$1” in
eth0)
logger “$zero: Engaged on eth0 with $STATUS …”
/sbin/ethtool -Okay eth0 gso off
;;
 
eth1)
logger “$zero: Engaged on eth1 with $STATUS …”
/sbin/ethtool -G eth1 rx 4096 tx 4096
/sbin/ethtool -Okay eth1 gso off
;;
 
eth2)
logger “$zero: Engaged on eth2 with $STATUS …”
begin=201
for i in zero..10
do
/usr/sbin/ifconfig eth2:$ 192.168.2.$begin
(( begin++ ))
performed
 
esac
exit zero

Create /sbin/ifdown-local if you wish to run a script when a community interface goes down as described above.

Conclusion

You realized run instructions persistently on boot or at any time when community interface goes up or down. Check out syconfig.txt for more information.

Posted by: Vivek Gite

The writer is the creator of nixCraft and a seasoned sysadmin, DevOps engineer, and a coach for the Linux working system/Unix shell scripting. Get the newest tutorials on SysAdmin, Linux/Unix and open supply subjects through RSS/XML feed or weekly e mail publication.

Source link

Related Articles

Back to top button