Linux: execute a command when network connection is restored - linux

I have a linux box connected to a router via an ethernet cable.
The cable is never unplugged and the linux box is always on.
My goal is to have a command executed on the linux box every time the router is rebooted.
This question is quite similar to this question but the suggested solution doesn't seem to work.
More specifically:
the command
inotifywait -e modify /sys/class/net/eth0/carrier; echo 'Change detected' does never detect a change (even in the case the cable is unplugged from the pc), because according to this answer the command inotifywait does not work in /sys
the command (suggested in a comment) inotifywait -e modify /etc/network/if-up.d/avahi-daemon does not detect any change when the router is rebooted

You can start a script after the linux box gets connected using up
(requires ifplugd to be installed )
#/etc/network/interfaces
auto eth0
iface eth0 inet dhcp
up /etc/network/yourscript.sh
However, there keep in mind that if you disconnect the cable (and plug in in after a while), the script also starts even though the router might not have been restarted.
--edit--
alternatively , place your script in
/etc/network/if-up.d/
(make sure it is executable and restart networking after changes.)

Depending on the Linux distribution on that router the correct way of running a command at startup/reboot is to create a startup script, or add the command to /etc/rc.local

Related

wpa_supplicant keeping print message to screen

I am start learning Linux(CentOs 5.5 kernel 2.6.35.13).
When I try to install a usb wifi stick(TP-Link TL-WN823N,and "lsusb" will show ID 0bda:8178 Realtek Semiconductor Corp..)
It work fine in the window interface( ctrl+shift+F7).
But when I shift to the command window(ctrl+shift+F1),and try to start wifi connection by
wpa_supplicant -Bw -Dwext -iwlan0 -c/etc/wpa_supplicant.conf
I found it kept print logs to screen before I type the command above.
I use
ps axjf |grep wpa_supplicant
to list all related process and find
there is a process start by user "dbus" with the command
/usr/sbin/wpa_supplicant -c /etc/wpa_supplicant/wpa_supplicant.conf -u -f /var/log/wpa_supplicant.log
I dont know whether is this dbus' command that lead the problem.
Below is the screen shot.
kept logging msg:(
Partly fix the problem.
There are two things that make the wifi information keep on loging on the screen.
First, if you use NetworkManager, it will automate run the command under /etc/sysconfig/wpa_supplicant, where you can find the default command write there has no -B parameter which will keep the log in the background.So you can either add an -B to that command or stop NetworkManager and start wifi connection with your own command like in the question.
Second, when you install the driver, the default run status is power saving mode, so when you transfer data with wifi, you can see the screen keeping log infomation like "get into the pw_saving","get out pw_saving" etc.
To fix this, you can shut down the power saving mode like this:
create a file /etc/modprobe.d/8192cu.conf with the following contents:
options 8192cu rtw_power_mgnt=0 rtw_enusbss=0
Or you can rewrite driver code to stop print the info to screen, which I still don't know how to do.

Raspberry pi has static IP address

Hi my raspberry pi has got static ip address.Even when its connected to router instead of 192.168.0.* as it ip address,it is showing 10.21.113.60.
I have tried changing /etc/network/interfaces file
My /etc/network/interfaces file
auto lo
iface lo inet loopback
When I looked into raspberry pi login screen i'm able to see two ip address.But ssh only works with 10.21.113.60
Someone please help me with this.
We should get lots of responses (the laptop is responding the Raspberry Pi through the network cable)! Ctrl+C to stop that. (If you didn’t then double check your connections and the IP addresses you are using).
As mentioned before, the new IP address will be lost when we reboot, so we need to ensure it is set every time we boot (or at least every time we want it to be when we boot).
To do that we can edit the “cmdline.txt” file which is located on the boot partition of the SD Card.
Make a copy of the file, with the following command:
sudo cp /boot/cmdline.txt /boot/cmdline.normal
Next edit the original file using nano:
sudo nano /boot/cmdline.txt
At the end of the long line, add the following (you will need to add a space between the last item and “ip”:
ip=192.168.0.x, for x your number
Ctrl+x and y to save and exit.
Make a copy of this file too:
sudo cp /boot/cmdline.txt /boot/cmdline.direct
You can now reboot the Raspberry Pi (sudo reboot), and next time the IP address will be automatically set.
To change between configurations, simply use the following commands (just remember to edit /boot/cmdline.direct if you need to change the IP address in future).
sudo cp /boot/cmdline.normal /boot/cmdline.txt
sudo cp /boot/cmdline.direct /boot/cmdline.txt
go to this instructions:
https://pihw.wordpress.com/guides/direct-network-connection/

Linux - shutdown-script with SSH

I would like to make a shutdown-script for my raspberry pi to shut down anothe raspberry pi over ssh.
The script works if it is running itself but at the shutdown routine the ssh command is not executed.
So that I have done until now:
Made the script in /etc/init.d:
#!/bin/sh
# the first thing is to test if the shutdown script is working
echo "bla bla bla " | sudo tee -a /test.txt
ssh pi#10.0.0.98 sudo shutdown -h now
Made it executable
sudo chmod +x /etc/init.d/raspi.sh
Made a symlink to the rc0.d
sudo ln -s /etc/init.d/raspi.sh /etc/rc0.d/S01raspi.sh
Now I know so far that the shutdown script is working outside of the shutdown routing by calling itself and the shutdown symlink I made is also working partially because I see the changes in the test.txt file every time I shut down.
Can anyone help me how to solve my problem?
Have you tried with single quotes?
The first link in Google has it
http://malcontentcomics.com/systemsboy/2006/07/send-remote-commands-via-ssh.html
What about the sudo, how do you solve entering the password?
https://superuser.com/questions/117870/ssh-execute-sudo-command
Please check this or other links on the web that have useful information.
I would have send all this in a comment but I cant yet because of reputation.
I have now got the script running by myself. I do not really know why it is now working but I write it down beneath and maybe someone else can clearifiy it.
I don´t think the first two changes at my system makes a difference but I also write it down. In the meanwhile because I do not managed the script to get working I had made a button to shutdown the system manually. Also I made a script which backs the mysql-database up (which is on the Raspberry Pi which I would like to switch off with the script) and copies the backup to the raspberry pi which should switch of the other raspberry automatically via the shutdown-script. This happens with scp and also for the password is a key generated.
I have also changed my script to get a log-message out of the script.
#!/bin/sh
ssh -t -t pi#10.0.0.99 'sudo shutdown -h now' >> /home/osmc/shutdown.log 2>&1
To get it into the shutdown-routine I used:
sudo update-rc.d raspi-b stop 01 0
I hope somebody can say me why my code now worked on the first day but not on the next few days until now.
I structured a command to suspend or shutdown a remote host over ssh. You may find this useful. This may be used to suspend / shutdown a remote computer without an interactive session and yet not keep a terminal busy. You will need to give permissions to the remote user to shutdown / suspend using sudo without a password. Additionally, the local and remote machines should be set up to SSH without an interactive login. The script is more useful for suspending the machine as a suspended machine will not disconnect the terminal.
local_user#hostname:~$ ssh remote_user#remote_host "screen -d -m sudo pm-suspend"
source: कार्यशाला (Kāryaśālā)

Programmatically reload network settings on Linux

What is the best way to programmatically reload the network settings from the config file /etc/network/interfaces ?
At the moment I use the system command to execute ifdown and ifup on the interface, but i'm not happy with this solution.
being more specific would help ...
but,
maybe you could write some script passing the interfaces as parameters
and turning it down and up, the usual way to re-read the /etc/network/interfaces file is that (I think so).
for interface in $#
do
sudo ifdown ${interface} && sudo ifup ${interface}
done

Linux send URL my IP address on startup

So, I'm trying to write a simple bash script to send my internal IP address to a website of mine on startup. I am on a network with DHCP, so I don't always know what the IP address of my Raspberry Pi will be after I do a reboot over ssh. I figured I could fix this by sending my website the current IP on startup. I haven't written many bash scripts, and I'm not really sure how to send data to my website. Right now I was just trying in the terminal this:
wget -qO- http://http://mywebsite.com/private/CurrentIP.php?send=$(/sbin/ifconfig eth0|grep 'inet addr:')
But I'm not having any luck. I don't actually know much about linux, and I'm trying to learn. That's why I got the raspberry pi actually. Anyway, can someone head me in the right direction?
I already know I need to put it in /etc/init.d/.
You could do this:
IP_ADDR=$(ifconfig eth0 | sed -rn 's/^.*inet addr:(([0-9]+\.){3}[0-9]+).*$/\1/p')
wget -q -O /dev/null http://mywebsite.com/private/CurrentIP.php?send=${IP_ADDR}
...but if your machine is stuck behind NAT, $IP_ADDR won't be your externally-visible address. Might want to use $_SERVER['REMOTE_ADDR'] in your PHP instead of/in addition to this to get the address for your client that your server sees.
Edit: Sounds like you want to be able to find your Raspberry Pi on your local (DHCP-managed) network after reboots. Have you considered using Multicast DNS instead?
How it works in practice: Let's say you've set the hostname of your RasPi to gooseberry. If you've enabled a multicast DNS server on that machine, other computers on the same network segment that can send multicast DNS queries will be able to find it at the domain name gooseberry.local. This is a peer-to-peer protocol and not dependent on gooseberry receiving any specific address via DHCP - so if it reboots and receives a new address, other machines should still be able to find it.
Mac OS X has this enabled out of the box; this can be enabled on most Linux distros (on Debian/Ubuntu you'd install the avahi-daemon and libnss-mdns packages); not sure about Windows, but a quick Google shows encouraging results.
This worked for me (wget part untested, but it finds IP address):
interface="eth0"
ip_addr=$(ifconfig ${interface} | sed -rn 's/^.*inet *([0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}).*$/\1/p')
wget -q -O /dev/null http://mywebsite.com/private/CurrentIP.php?send=${ip_addr}
Can't you use:
hostname --ip-address

Resources