Shutting down the server after inactivity

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?

Hmm … maybe this ??

:~> cnf netstat
                         
The program 'netstat' can be found in following packages:
  * net-tools-deprecated [ path: /bin/netstat, repository: zypp (Leap_15.6_Main_repo) ]
  * net-tools-deprecated [ path: /usr/bin/netstat, repository: zypp (Leap_15.6_Main_repo) ]

Try installing with:
    sudo zypper install net-tools-deprecated

:~>

AS @aggie explains, that is not true. It is still there (and I assume will be for quite some time).

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).

Thank you. It works :+1:
However, if anyone has a more efficient solution, I would be happy to hear it :slight_smile:

@rilicek If it ain’t broke, don’t fix it :wink:

Going forward, look at using ss instead of netstat and a systemd timer/service instead of cron.

@rilicek wrote:

“what” works?? It’s not clear who you’re responding to. (or I missed it)

The script works. I installed the net-tools-deprecated package where is the netstat :slight_smile:

1 Like

Success!
I used both ss and timer/service :slight_smile:

#!/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

1 Like