2

Sometimes network completely fails on my server for samba shares (e.g. I could'n connect to server via ssh and samba shares were unavailable). Since there is no working solution to fix it (full description here) I wrote small script which does network restart and I guess it would help to establish connection. In case If I do systemctl restart network it works. So I need that it has to restart network automatically.

The logic is if gateway is unavailable for ping (*1.121) it will check another host within LAN. Then if it won't get back response from the second host it will execute systemctl restart network. Can some review and add/remove something or propose another way to handle it?

#!/bin/bash

ADDR1="192.168.1.101";
ADDR2="192.168.1.5";
ACCS1=$(ping -c 1 -s 1 -w 1 $ADDR1 | grep time | awk -F ',' '{print $2}' | awk '{print $1}');
ACCS2=$(ping -c 1 -s 1 -w 1 $ADDR2 | grep time | awk -F ',' '{print $2}' | awk '{print $1}');


if [[ $ACCS1 == 0* ]];
        then
                if [[ $ACCS2 == 0* ]];
                                echo "Host $ADDR2 is unavailable" >> $HOME/blackout_time.log
                        then
                                systemctl restart network
                                echo "Netwotk restart time by script1 - $(date +%Y-%m-%d_%k:%M:%S)" >> $HOME/blackout_time.log
                                sleep 20
                        fi
        fi

Thanks in advance

Robert
  • 75
  • 1
  • 7
  • 2
    It might be better to expend your effort trying to identify the reason why the network stops in the first place. – roaima Nov 16 '16 at 14:32
  • Yes. I made the post about it but still there is no solution that is why I'm asking here – Robert Nov 16 '16 at 14:41
  • 1
    for once `grep time | awk -F ',' '{print $2}' | awk '{print $1}'`can be shorterned to `awk '/time/ { print $4 }' ` depending on ping status. – Archemar Nov 16 '16 at 14:57

1 Answers1

3

In one compact line:

ping 192.168.1.121 -c1 -s1 -w1 -q || ping 192.168.1.189 -c1 -s1 -w1 -q || systemctl restart network
Ipor Sircer
  • 14,376
  • 1
  • 27
  • 34