In OpenSuse 42.3 I had such a script to shut down the server when computers are no longer connected to server. Script was added to crown.
#!/bin/bash
netstat -tan | grep \:21 | grep ESTABLISHED >& /dev/null
if [ $? -ne 0 ]; then
hosts="komp1 komp2 komp3"
dlist=""
poweroff=true
for n in $hosts; do
fping -u $n
if [ $? = 0 ]; then
echo "One of the computers is working"
poweroff=false
break
fi
done
if $poweroff; then
sync
poweroff
fi
else
echo "FTP is working"
fi
In OS15.6 netsat,are no longer available in default repositories.
Do you have an idea how to write such a script better? Or maybe it can be achieved more smart?
netstat (net-tools) is deprecated since over a decade. One should use maintained, faster and secure replacements. For netstat, ss is the replacement which is in the package iproute2 (which is btw installed by default and a hard dependency for core components of the OS).
#!/bin/bash
ss -tan | grep \:21 | grep ESTAB >& /dev/null
if [ $? -ne 0 ]; then
hosts="komp1 komp2 komp3"
dlist=""
poweroff=true
for n in $hosts; do
fping -u $n
if [ $? = 0 ]; then
echo "One of the computers is working"
poweroff=false
break
fi
done
if $poweroff; then
sync
poweroff
fi
else
echo "FTP is working"
fi