0

I need to check my connection to a spesific port every 5 minutes, currently i can't use ping command, so i need other alternative to do this.I want to execute this command in shell script

Can someone help me to show some example for this case?

Ahmad asy'ary
  • 77
  • 2
  • 9
  • http://stackoverflow.com/questions/4922943/test-from-shell-script-if-remote-tcp-port-is-open – Ipor Sircer Nov 16 '16 at 09:49
  • Is netstat working for you? Could possibly a combination of netstat with grep do the job? Also check netstat --help or man netstat for available options. – George Vasiliou Nov 16 '16 at 09:50
  • @GeorgeVasiliou i want to test it from server A to server B, so i think netstat wont work since netstat only check if the port is up or not. CMIIW – Ahmad asy'ary Nov 16 '16 at 09:56
  • @IporSircer great example, but my concern is that command will run forever till i kill it, CMIIW – Ahmad asy'ary Nov 16 '16 at 10:02
  • You want to run it forever till kill or you want not? I'm confused about your last comment... The command @IporSircer mentioned won't run forever if you add a timeout with `-w10` (replacing 10 with some number) – Moritz Sauter Nov 16 '16 at 10:54
  • @MoritzSauter i dont want to run it forever, so from IporSirce example, that process will be killed after 1s, correct? – Ahmad asy'ary Nov 17 '16 at 03:10
  • @MoritzSauter i think i'll use this command, is it safe? i dont want it to run forever -> timeout 1 bash -c 'cat < /dev/null > /dev/tcp/google.com/80' – Ahmad asy'ary Nov 17 '16 at 03:11
  • What do you mean with safe? The option `-w` let you set a timeout, eg as 1 second with `-w1`. And what do you mean with _timeout 1 bash -c 'cat < /dev/null > /dev/tcp/google.com/80'_? – Moritz Sauter Nov 17 '16 at 07:40
  • @MoritzSauter i used t "imeout 1 bash -c 'cat < /dev/null > /dev/tcp/google.com/80" to scan my connection, do i have to kill it or it will kill itself since i use "timeout 1" for that command – Ahmad asy'ary Nov 17 '16 at 15:02
  • `timeout` kills your command, if it is running longer than your specified duration, but `cat < /dev/null` receives immediatly a `EOF` and therefore it should return immediatly. but if this works for you, great. you also can answer your own question – Moritz Sauter Nov 17 '16 at 16:09
  • @MoritzSauter so there will be no open socket or something like that if i use that command, correct? – Ahmad asy'ary Nov 17 '16 at 16:57
  • I don't think so. – Moritz Sauter Nov 18 '16 at 06:57
  • @MoritzSauter sorry, do u mean it will run forever and repeated forever? how to kill it if i already run that command? – Ahmad asy'ary Nov 18 '16 at 08:59
  • You can look for the command via `ps -aux`. In the last column it shows which command is run. and in the second column is the PID. If you want to kill a process you can run `kill PID`; replacing *PID* with the PID of the process you want to kill – Moritz Sauter Nov 25 '16 at 08:25

1 Answers1

0
port=80
ip=8.8.8.8
checkIntervalSecs=5
timeoutSecs=1
while true ; do
    if $(nc -z -v -w$timeoutSecs $ip $port &>/dev/null); then
         echo "Server is up!"
    else
         echo "Server is down!"
    fi
    sleep $checkIntervalSecs
done

This runs until you kill it. For an explanation of the nc command, it is basically taken from SO question @IporSircer suggested.

Community
  • 1
  • 1
Moritz Sauter
  • 167
  • 1
  • 11