2

Trying here to perform some tests on a device in a remote network.

Already managed to do it using SSH between client and server, then ping command, but thought if there wasn't better way to do it.

ssh -o ConnectTimeout=5 -t [email protected] "ping -i 1 -c 1 -W 1 192.168.12.115 > /tmp/ping.txt"

Any sugestion?

Follows diagram of current situation. Current Diagram

Baraujo85
  • 698
  • 7
  • 29
  • 1
    "Better" in what sense. Your current command leaves the `ping.txt` file on the server. Is that where you want it? If not, then just `ssh ... "ping ..." >/tmp/ping.txt` to save it on the client. – Kusalananda Aug 18 '19 at 15:18
  • Somehow faster, don't know! Would be SSH the most efficient way to keep pinging a device in a remote network every 5secs? Isn't there a command that would do it more efficiently? And yes, I'd prefer to keep the ping.txt file in the server for now! Maybe later I'd remove it, don't know! – Baraujo85 Aug 18 '19 at 15:32
  • 1
    If you want to ping it efficiently every 5 seconds, you would not connect with `ssh` every five seconds, but connect (once) and then use `ping -i 5 -c 0` (or without the `-c` option, depending on what `ping` implementation you are using). I'm not posting this as a solution as I don't know whether it solves any problem. – Kusalananda Aug 18 '19 at 15:35
  • I believe yes, it does solves the problem, if you're saying that it is currently the best way. But is there no problem in keeping an active ssh connection between server/client? – Baraujo85 Aug 18 '19 at 15:53
  • The main goal is to get this script running: `#!/bin/bash ssh -v -o ConnectTimeout=10 -t [email protected] while true; do date > /tmp/sdown.txt ; ping -i 1 -c 1 -W 1 192.168.12.116 ; sleep 1 ; if grep -q "64 bytes" ; then : else mutt -s "Device Down!" [email protected] < /tmp/sdown.txt ; sleep 10 ; fi done` But in the server I'm getting "Network Unreachable" from ping reply! – Baraujo85 Aug 18 '19 at 16:18

1 Answers1

2

Just managed to do it.

Main problem was Ssh syntax and missing Bash commands, as standard output redirection.

Another flaw was lack of touch command to previously create text file.

Follows current situation:

ssh -o ConnectTimeout=10 -t [email protected] ' ping -i 5 -c 1 -W 5 192.168.33.23 2> /dev/null | grep "ttl=64" || touch /tmp/servdown.txt ; date > /tmp/servdown.txt ; mutt -s "Device Down!!!" [email protected] < /tmp/servdown.txt '

Just found this answer too:

Run if statement on remote machine

Baraujo85
  • 698
  • 7
  • 29