LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Red Hat (https://www.linuxquestions.org/questions/red-hat-31/)
-   -   HowTo Run a script on ifup ethX and a different one for ifdown ethX (https://www.linuxquestions.org/questions/red-hat-31/howto-run-a-script-on-ifup-ethx-and-a-different-one-for-ifdown-ethx-736995/)

dscholl 07-01-2009 12:31 PM

HowTo Run a script on ifup ethX and a different one for ifdown ethX
 
I'm running RHEL 5.2 and would like to run an iptables command when an ethernet device goes up, and a different iptables command when it goes down. I want this to happen automatically after the device goes up or down.

For instance... eth1:2

I tried just adding a line to to /etc/sysconfig/network-scripts/ifcfg-eth1:2 like so:
touch /tmp/cant-touch-this

This ends up getting run on both ifdown eth1:2, and ifup eth1:2 - which helps but isn't quite what I want.

Thanks for any help in advance!

unSpawn 07-02-2009 05:51 AM

Network device status is managed with /etc/rc.d/init.d/network. This SysV initscript changes dirs to /etc/sysconfig/network-scripts and runs 'ifup' on found devices. Looking in 'ifup-eth' you will find the last thing it execs is 'ifup-post'. About the last thing 'ifup-post' exececutes is the do_netreport shell function. This function is sourced from network-functions and runs 'netreport'. See if reading 'man netreport' gives you ideas or else check if modifying ifup/ifdown scripts is more your thing.

dscholl 07-07-2009 12:47 PM

Thanks for the info!

So if if I understand netreport... I'd have to have a background process for each eth interface in question which calls 'netreport -r' and then waits for a SIGIO signal meaning that the interface's state has changed. I guess that seems a bit complicated, but doable.

This is really easy on Debian/Ubuntu. You can just add one-liners to /etc/network/interfaces that get run when an specific interface goes up or down. Here's an example. Is there anything simple like that I can do on RHEL?

Code:

auto eth0:2
allow-hotplug eth0:2
iface eth0:2 inet static
        address 172.18.202.2
        netmask 255.255.0.0
        up  /etc/network/scripts/static-nat up  172.18.202.2 129.193.148.44
        down /etc/network/scripts/static-nat down 172.18.202.2 129.193.148.44

Thanks again for the help.

unSpawn 07-07-2009 01:09 PM

Quote:

Originally Posted by dscholl (Post 3599905)
This is really easy on Debian/Ubuntu. You can just add one-liners to /etc/network/interfaces that get run when an specific interface goes up or down. Here's an example. Is there anything simple like that I can do on RHEL?

Sure. That's why I ended with "... or else check if modifying ifup/ifdown scripts is more your thing". That isn't advisable as 0) it isn't standard practice plus 1) scripts may jam if you don't script well and 2) scripts may be replaced on update but sure, other than that it's not impossible to add a line or so. I hope the "ifup-post" example made clear there's also an "ifdown-post" and that both are scripts you can read and tweak as you like.


All times are GMT -5. The time now is 07:32 PM.