Hotplug iface scripts

Hello,

I have two scripts in /etc/hotplug.d/iface which I wan to use to perform some actions when the matched interface change state from up to down and vise versa. Below are my scripts.

When interface comes up

#!/bin/sh
if [ "${ACTION}" == "ifup" ] && [ "${DEVICE}" = "eth1" ]
then
    echo "local 1.1.1.1" >> /etc/openvpn/tester.ovpn
	service openvpn start Wan1
fi

When interface comes down

#!/bin/sh
if [ "${ACTION}" == "ifdown" ] && [ "${DEVICE}" = "eth1" ]
then
    sed -i '/local/d' /etc/openvpn/tester.ovpn
	service openvpn stop Wan1
fi

As you can see my goal when the interface is coming up is to write the local ip address of the interface which obtains from the DHCP in the .ovpn file and start the respective vpn instance while the opposite action is to remove the line from the file and stop the instance.

However, while testing the scripts, only the echo command seems to be passing as I can see the .ovpn file changes. Sed, start and stop doesn't seems to work.

Any help will be appreciated.

PS. What is the interface variable for the ip address which can be used in echo so I can write the obtained DHCP address?

Thank you,

D

Add the line below before the first if. This will log all the output generated by all commands into that file. This will help you debug your script.

exec >>/root/hotplug.log 2>&1
3 Likes

Well that one is strange too..

After adding that line before the if on both scripts I have the following

/sbin/hotplug-call: /etc/hotplug.d/iface/98-Wan1-up: line 6: service: not found

It is not recognizing the "service openvpn start Wan1". However if I run this command in cli manually it works as expected. The same apply for the commands in the scripts for down state.

Regarding the log for the down script it seems that it is not calling it at all.

Use full path for service.

Hi fantom-x

I modified my scripts as per below with a small improvement on ifup state.

#!/bin/sh
exec >>/root/hotplug.log 2>&1
if [ "${ACTION}" == "ifup" ] && [ "${DEVICE}" = "eth1" ]
then
uci set openvpn.Wan1.local=172.16.100.2
uci commit openvpn
/etc/init.d/openvpn start Wan1
fi

#!/bin/sh
exec >>/root/hotplug.log 2>&1
if [ "${ACTION}" == "ifdown" ] && [ "${DEVICE}" = "eth1" ]
then
/etc/init.d/openvpn stop Wan1
uci set openvpn.Wan1.local=1.1.1.1
uci commit openvpn
fi

However, the script for the down state does nothing and is not logging at all while running the commands in cli it works as expected with no errors.

Add set -x before exec to get more debug information. Then you can tell if the script even gets invoked.

1 Like

well it seems that the script on down state is not invoking

Here is the log when the interface goes down.

  • '[' ifdown '==' ifdown ]
  • '[' '=' eth1 ]
  • '[' ifdown '==' ifup ]

and when the interface goes up

  • '[' ifup '==' ifdown ]
  • '[' ifup '==' ifup ]
  • '[' eth1 '=' eth1 ]
  • uci set 'openvpn.Wan1.local=172.16.100.2'
  • uci commit openvpn
  • /etc/init.d/openvpn start Wan1

any idea? what could be wrong? Do I miss any package or something?

The DEVICE variable is empty. Remove the check for eth1

sorry mate I might sound weird but my brain is fried up due to the COVID-19 isolation. 9 days home isolation and still counting.

can you please let me know how my down script should be modified? Also is it possible to have both scripts in one?

#!/bin/sh

set -x
exec >>/root/hotplug.log 2>&1

if [ "${ACTION}" == "ifup" ] && [ "${DEVICE}" = "eth1" ]; then
  uci set openvpn.Wan1.local=172.16.100.2
  uci commit openvpn
  /etc/init.d/openvpn start Wan1
elif [ "${ACTION}" == "ifdown" ]; then
  /etc/init.d/openvpn stop Wan1
  uci set openvpn.Wan1.local=1.1.1.1
  uci commit openvpn
fi

well combining both in one doesn't work at all. not even the log file is created.

also what I cannot understand is if I don't use the check for the interface in down script how I am going to match it. At a given time more than one interface might became down.

can you please let me know what is the variable for the ip address of an interface so I cannot use it for the obtained DHCP address instead of puting static?

sorry to be more specific I need the script to pull the "inet addr" from the interface which is obtained from the DHCP

Hi

Using the ubus call network.interface.Cellular3 status | jsonfilter -e '@["ipv4-address"][0].address' as value I obtained the IP.

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.