Execute script after a network connection has been established

Hello kind people,

I recently bought a used HP ProBook 6460b in order to convert it into a machine to do some coding. I installed the latest Manjaro XFCE Edition on it.

In order to make my life easier, i wanted to get manjaro to mount my network share automatically, but only when im at home.

so i did the following

create an .smbcredentials file
create a target folder in /mnt
test a command to mount the drive in the target folder
move .smbcredentials to / and change ownership to root
chmod 600 . smbcredentials
test the mount command again.

until this point, no problem.
in order to check if i am home or not and than mount the drive, i created the following script

/usr/local/bin/fritzmount.sh

#!/bin/bash

sleep 30s
if [[ $(/usr/bin/arping -c 1 192.168.178.1|cgrep -i  --color --no-numbers "F0:B0:XX:XX:XX:XX") ]]; then
	mount -t cifs -o credentials=/.smbcredentials,vers=1.0,noserverino //192.168.178.1/FRITZ.NAS/Nas/user /mnt/fritznas/
else
echo "not found"
fi

and that script seems to work too

i finally created a .service file with the following content

[Unit]
# Type=simple|forking|oneshot|dbus|notify|idle
Description=Automount for Fritz Nas
## make sure we only start the service after network is up
Wants=network-online.target
After=network.target

[Service]
## here we can set custom environment variables
Type=oneshot
User=root
WorkingDirectory=/usr/local/bin
ExecStart=fritzmount.sh
StandardOutput=append:/mount.log
StandardError=append:/mounterror.log
# don't use 'nobody' if your script needs to access user files
# (if User is not set the service will run as root)
#User=nobody

# Useful during debugging; remove it once the service is working
#StandardOutput=console

[Install]
WantedBy=multi-user.target

and ran
sudo systemd-analyze verify automount_fritzbox.service
wich passed without problems

sudo systemctl daemon-reload
sudo systemctl enable automount_fritzbox.service

However, after restarting the pc, the drive was not mounted.
No matter where i look, i cant seem to figure out why and as such i turn to you for insight.

Thanks you,
Sulumar

Not good. Do not do this. Get rid of Working directory and use the absolute path in ExecStart.

Also check the journal.

journalctl -u automount_fritzbox.service

Is this really nessesay? Why not put it in the journal with all the other messages from this unit?

Is this really nessesay? Why not put it in the journal with all the other messages from this unit?

Because i have no idea how. at least this way i knew where the logs went
I will try editing the .service file according to your suggestion.

If you dont mind, would you tell me why using Working Dir is not good ?

Because it does not change the directory before ExecStart is executed. It changes the directory after that.

It basically does a cd /path after the script is started, but before the first command from the script is executed.

However, use always a full path to the script or binary in ExecStart.

Heres the journal output, looks like the script got executed

↪ journalctl -u automount_fritzbox.service                                                                                                     14:06:31
-- Journal begins at Thu 2021-01-14 20:31:50 CET, ends at Wed 2021-01-20 14:07:44 CET. --
Jan 18 19:13:08 jerome-devstation systemd[1]: /etc/systemd/system/automount_fritzbox.service:21: Failed to parse output specifier, ignoring: console
Jan 18 19:17:14 jerome-devstation systemd[1]: /etc/systemd/system/automount_fritzbox.service:22: Failed to parse output specifier, ignoring: console
-- Boot 7eaa771c1b294e0b8ea17e04d8cc9a57 --
Jan 18 19:22:37 jerome-devstation systemd[1]: Starting Automount for Fritz Nas...
Jan 18 19:22:50 jerome-devstation systemd[1]: automount_fritzbox.service: Succeeded.
Jan 18 19:22:50 jerome-devstation systemd[1]: Finished Automount for Fritz Nas.
Jan 18 19:26:25 jerome-devstation systemd[1]: Starting Automount for Fritz Nas...
Jan 18 19:26:27 jerome-devstation systemd[1]: automount_fritzbox.service: Succeeded.
Jan 18 19:26:27 jerome-devstation systemd[1]: Finished Automount for Fritz Nas.
Jan 18 19:30:40 jerome-devstation systemd[1]: /etc/systemd/system/automount_fritzbox.service:22: Failed to parse output specifier, ignoring: console
-- Boot 7978e997ddc14c10a4f9a4033a07ef82 --
Jan 18 19:37:41 jerome-devstation systemd[1]: Starting Automount for Fritz Nas...
Jan 18 19:38:37 jerome-devstation systemd[1]: automount_fritzbox.service: Succeeded.
Jan 18 19:38:37 jerome-devstation systemd[1]: Finished Automount for Fritz Nas.
-- Boot 2bc140dad13549b184c78ddab9484299 --
Jan 18 19:43:38 jerome-devstation systemd[1]: Starting Automount for Fritz Nas...
Jan 18 19:44:32 jerome-devstation systemd[1]: automount_fritzbox.service: Succeeded.
Jan 18 19:44:32 jerome-devstation systemd[1]: Finished Automount for Fritz Nas.
Jan 18 19:50:40 jerome-devstation systemd[1]: Starting Automount for Fritz Nas...
Jan 18 19:51:12 jerome-devstation systemd[1]: automount_fritzbox.service: Succeeded.
Jan 18 19:51:12 jerome-devstation systemd[1]: Finished Automount for Fritz Nas.
-- Boot 10d9edff1477428b8cb63c140e2e5a36 --
Jan 20 11:38:40 jerome-devstation systemd[1]: Starting Automount for Fritz Nas...
Jan 20 11:39:17 jerome-devstation systemd[1]: automount_fritzbox.service: Succeeded.
Jan 20 11:39:17 jerome-devstation systemd[1]: Finished Automount for Fritz Nas.
-- Boot 92e6e0eacef4449bbcff089ab876e7a4 --
Jan 20 13:35:44 jerome-devstation systemd[1]: Starting Automount for Fritz Nas...
Jan 20 14:01:36 jerome-devstation systemd[1]: automount_fritzbox.service: Succeeded.
Jan 20 14:01:36 jerome-devstation systemd[1]: Finished Automount for Fritz Nas.
sulumar at jerome-devstation in /m/fritznas
↪ 

however, ls -la

[sulumar@jerome-devstation]
[14:11:36 fritznas]$ ls -la
insgesamt 8,0K
drwxr-xr-x 2 root root 4,0K 18. Jan 19:45 .
drwxr-xr-x 3 root root 4,0K 18. Jan 19:45 ..                                                                                                                                             

it seems its still not mounted

i checked the mounterror.log and found this

↪ cat mounterror.log                                                                                                                           
/usr/bin/arping: NETLINK_ROUTE unexpected iov element
/usr/bin/arping: NETLINK_ROUTE unexpected iov element
/usr/bin/arping: NETLINK_ROUTE unexpected iov element
/usr/bin/arping: NETLINK_ROUTE unexpected iov element

looks like as of the ping command doesn’t work.

my best guess is the arping goes off before the wifi connection is established
i tried to get the script to check by pinging the router and waiting for a reply but i cant get the bash script to work propperly.

Hell, it cant be that difficult to check for a working network connection. i must be missing something.
Here is the bash script i wrote, but that wont even run manually
throws out
.> /fritzmount.sh: Zeile 3: [: syntax error: `-q’ unexpected

./fritzmount.sh: Zeile 3: 20: Datei oder Verzeichnis nicht gefunden
./fritzmount.sh: Zeile 9: [: Fehlende »]«

before proceeding to try and mount the drive, if connected
if not connected or not on my home network it pings indefinitely

i=0
while [ ! ping -q -c 1 -W 1 192.168.178.1 >> /dev/null ] || [ i < 20 ]; do
	let "i = i+1"
	sleep 1
	
done

if [ i >= 20]; then
	echo "Network Down"
else

	if [[ $(/usr/bin/arping -c 1 192.168.178.1|cgrep -i --no-numbers "F0:B0:14:57:E5:21") ]]; then
		mount -t cifs -o credentials=/.smbcredentials,vers=1.0,noserverino //192.168.178.1/FRITZ.NAS/Nas/Jerome /mnt/fritznas/
	else
		echo "not home"
	fi
fi

you’d think that after coding some C, some java and some python, id grasp bash but obviously not

Well, maybe it is easier to use NM dispatcher. It can start scripts (as root) after a connection is established.

https://wiki.archlinux.org/index.php/NetworkManager#Mounting_of_SMB_shares

If you go with the systemd stuff, add Requires=network-online.target and make sure it is set.

Also check first if the wifi devices has an IP (if possible check for a specific IP). Only then start pining.