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

  • 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 – Raphael 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
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
  • 1
    I was about to say "ping return succes/error code don't bother parsing output" – Archemar Nov 16 '16 at 15:00
  • 3
    shouldn't first test be a || according to OP spec ? – Archemar Nov 16 '16 at 15:02
  • @Archemar: you're right, corrected – Ipor Sircer Nov 16 '16 at 20:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.