Detect IP-Address change on an interface - linux

I would like to trigger a service when a change of an ip address on a specific interface occurs. Is there a target for this or some other method I am not aware of to achieve this using systemd on Linux (Kernel 3.19)?
The service would be used to send a SIGNAL to a defined process. The Linux is running on an embedded system.
Thanks!

Because you use Systemd you might already use systemd-networkd for managing your devices instead of relying on 3rd party code.
You could use the structured journal output to get the last 2 ADDRESS field of the current BOOD_ID.(sadly, there is no notification mechanism for address changes in systemd-networkd):
→ sudo journalctl -F ADDRESS -u systemd-networkd -n 2
192.168.178.29
So, if there is only one line output, there was no address change.

There is an solution in other question of StackOverflow. Just here:
Detecting a change of IP address in Linux
I like this code, it's easy, you onli need a cron job with frecuency as you need (I made a little change):
#!/bin/bash
OLD_IP=`cat ip.txt`
NEW_IP=`/sbin/ifconfig | awk -F "[: ]+'{ print $4}'`
if [ $NEW_IP != OLD_IP ]; then
YOU_COMMAND <commands>
echo $NEW_IP > ip.txt
fi
exit 0

Related

identify correct interface in Linux among others

I have some physical servers with multiple interface, each interface is assigned to a separate vlan. The correct mapping of interface number to vlan from network admins usually differs from what I see inside my console and the common procedure is to check MAC of interface he has configured. I tried to find a solution to resolve this. The first solution I tried was to get tcpdump from the interface to see what type of ip address is broadcasting which it is cumbersome. The second solution was to arping the gw to see if this is true interface with a command like:
arping -c 1 -D -q -I $i $SGW1
if [ $? == 1 ]; then echo "$i $SGW1" ;SIF+=("$i"); fi
like
arping -c 1 -D -q -I ens160 172.20.29.158
but it take time and sometimes with wrong answers. Could you please suggest a better way?
(we are not running DHCP by the way)

Why echo isn't able to access a variable in Ubuntu 20.04

I can't find anything that helps with this issue even if the answer may be very simple.
I'm trying to get squid to redirect traffic through a 3g dongle, therefore I need to table the IP and change it in the squid.conf every time it changes.
I'm using this code here on "/etc/ppp/ip-up.local" so it's launched every time a new IP connects (or reconnects) to the machine.
#!/bin/bash
if [[ "$PPP_IFACE" == "ppp0" ]] ; then
TABLE=uplink2
/bin/sed -i "s&\(tcp_outgoing_address\).*\(very1\)&\1 $PPP_LOCAL \2&" /etc/squid/squid.conf
fi
##generate ip subnet for route
baseip=`echo "$PPP_LOCAL" | cut -d"." -f1-3`
/usr/sbin/ip route add "$baseip".0/24 dev "$PPP_IFACE" src "$PPP_LOCAL" table "$TABLE"
/usr/sbin/ip route add default via "$PPP_REMOTE" table "$TABLE"
/usr/sbin/ip rule add from "$PPP_LOCAL" table "$TABLE"
/usr/sbin/squid/ -k reconfigure
/usr/bin/systemctl squid restart
exit 0
The problem is that baseip=echo "$PPP_LOCAL" | cut -d"." -f1-3 cannot use $PPP_LOCAL
I tried to add echo $PPP_LOCAL >> file.txt but it just adds an empty line.
It's awkward to me that sed instead accesses the variable and modify correctly the squid.conf file with the new address
How can I fix this??
I also have a "sub-question", I'm a complete newbie just starting to learn and I'm not sure whether or not I should add an ip-down code to remove the table rules
Thanks everyone for the help

How to set up a udev rule for ETH "link down"/"link up"?

I like to switch on a green LED(connected through GPIOs), when eth0 is connected. When disconnected I like to switch the green LED of and a red one on.
Thought that udev is maybe the right place for it. I created the simple demo rule:
KERNEL=="eth0", SUBSYSTEM=="net", ACTION=="add", RUN+="/sbin/set_BSPleds eth0 on"
This rule should call a script when the eth0 is added. It was never executed.
After I was looking to the udev monitor by entering "udevadm monitor -k -u" at the shell. There were no events coming when I unplug/plug the lan cable.
root#sama5d3xek:/etc/udev/rules.d# udevadm monitor -k -uh0
monitor will print the received events for:
UDEV - the event which udev sends out after rule processing
KERNEL - the kernel uevent
root#sama5d3xek:/etc/udev/rules.d#
Seems that there are no uevents for eth0. The ethernet driver is provided my ATMEL. I am building a custom Linux by the help of Yocto.
My question is, how to get the "link down"/"link up" events to udev?
If it does not works with udev, what alternative way is the best?
As others have already mentioned, it seems one can't use udev for this.
Ubuntu: wait for network link up and execute a bash command suggests
inotifywait -e modify /sys/class/net/eth0/carrier; echo 'Change detected'
(Currently, / is on nfs for my box, so I can't really say if it will work.)
In other posts, there are some concerns about using the inotify API on /sys: inotify_add_watch fails on /sys/class/net/eth0/operstate .
I think the Right Way(TM) do do this would be to use the netlink(7) API, preferably through a daemon such as netplugd.
Hope that helps :)
Ethernet devices are devices, but connections are not.
You could trace connection through /dev/xconsole, dmesg or /var/log/kern.log.
Sample, using rsyslog:
You could (as root):
echo ':msg, contains, "Link is" |/run/netlink' >/etc/rsyslog.d/netlinkstate.conf
mkfifo /run/netlink
chgrp user /run/netlink
chmod 640 /run/netlink
service rsyslog force-reload
Then, logged as user, simply:
read line < /run/netlink
will wait for input from fifo, then hold until something happen.
state=${line#*Link is } eventtime=${line%% $HOSTNAME*}
echo $eventtime $state
2016-11-21T17:40:50.956181+01:00 Down
or
echo $eventtime $state
2016-11-21T17:40:50.956181+01:00 Up 100 Mbps Full Duplex, Flow Control: Rx/Tx
echo $eventtime ${state%% *}
2016-11-21T17:40:50.956181+01:00 Up
Under bash, you could use timeout for emptying fifo and read only last entry:
while read -t .01 entry </run/netlink;do line="$entry";done
state=${line#*Link is }
eventtime=${line%% $HOSTNAME*}
shortstate=${state%% *}
Nota: I've used /run to store fifo. This could be not the better place as this won't exist on next reboot.

Why this function in bashrc Dosnt work?

I am trying to get this function to file .bashrc
This is my function :
function my_ip() {
echo "internal or external?"
read -r choise
if [ "$choise" == "internal" ] ;then
echo "please enter the name of the card that youare using at";
read -r card ;
ifconfig "$card" | grep 'inet addr' |cut -d':' -f2|cut -d ' ' -f1;
else
wget -qO- http://noc.co.il |grep "var VisitorCountry" | awk '{print$4}'|cut -d '"' -f4;
fi
}
My goal is that the function will quickly give me my IP by choosing internal or external
if I want to automatically displays the network card ("without the need to ask Which network card do you use(The computer automatically detects which network card the user uses and will put the name as a variable at the right commend for internal IP , how can I get him to identify what card the user use(WLAN0 WLAN1 etc.) ?
A system can have more than one IP address. Which one of them is the one you are looking for? You did not specify that.
There are multiple questions related to finding IP addresses with bash:
Find internal IP address
The accepted answer uses a deprecated option for hostname; the second answer is the more correct one IMHO.
In bash, is there a way to find the IP address for all interfaces?
Find IP address for my system
Bash script to find all IP addresses
If none of those float your boat, consider closing this question and posting a new one with more precise phrasing what you are looking for.

Capture nethogs output in log file

I want to check the network bandwidth used by my process.
For this i found that nethogs tool is useful. Using this tool i can see which process is eating up a network bandwidth and process behaviour.
But how do I capture data from nethogs for a my process and store it into log file ?
You can run nethogs in background in tracemode and write output to a file like this:
sudo nethogs -t eth1 &> /var/tmp/nethogs.log &
Download and build the nethogs-parser as described here.
Then after you have accumulated enough data you can run the parser to see the results:
./hogs -type=pretty /var/tmp/nethogs.log
Make sure to kill the running nethogs process when you are done collecting data.
More info here on automating the task.
I dont know when these options got implemented but you can use nethogs -t or nethogs -b, the pid and user are strangely placed at the end of the pid command string, but easy enough to parse.
I think you need to use the latest cvs version 0.8.1-SNAPSHOT
You can use this command to capture output:
nethogs -d 5 | sed 's/[^[:print:][:cntrl:]]//g' > output.txt
The right command of nethogs is
nethogs -d 1 eth0 > output.txt
You need to specify the network interface otherwise, the default interface eth0 will be used. Sometime, nethogs might not show the proper output because of the network interface. It is always better to provide the network interface and generate some traffic during the experimentation. You can print the output to a file by adding > output.txt
-d argument specifies how frequently the output will be shown. Here, I gave 1, this indicates that the output will be shown per second.
Hope this might be useful.

Resources