Home Assistant - Router Reboot with Ping, EXEC and SSH

Hi Everyone,

Hoping to get some direction here. I am trying to create a flow where I ping google every 10 mins, if ping is false, wait another 10 mins, ping again and if false, run ssh command to reboot the router (it an asus router that allows ssh). However, I am struggling with catching the output for a failed ping from the exec node and also the ssh command.

This is what my flow looks like:

I tried using putty to the router and once logged in all I need to do is type reboot and enter to reboot the router. But I can't seem to figure out how to do this in the ssh client node.

[
    {
        "id": "343e08a1.fc3668",
        "type": "tab",
        "label": "Reboot Router",
        "disabled": false,
        "info": ""
    },
    {
        "id": "6370cfde.18eb7",
        "type": "ping",
        "z": "343e08a1.fc3668",
        "name": "",
        "host": "www.google.com",
        "timer": "35",
        "x": 100,
        "y": 420,
        "wires": [
            [
                "e50aae5f.5c05c"
            ]
        ]
    },
    {
        "id": "e50aae5f.5c05c",
        "type": "switch",
        "z": "343e08a1.fc3668",
        "name": "Is False or > 0",
        "property": "payload",
        "propertyType": "msg",
        "rules": [
            {
                "t": "false"
            },
            {
                "t": "gt",
                "v": "0",
                "vt": "num"
            }
        ],
        "checkall": "true",
        "repair": false,
        "outputs": 2,
        "x": 300,
        "y": 420,
        "wires": [
            [
                "a3049fe5.bf31f"
            ],
            []
        ]
    },
    {
        "id": "4d4b7c6d.4e71f4",
        "type": "exec",
        "z": "343e08a1.fc3668",
        "command": "ping -c 2",
        "addpay": false,
        "append": "www.google.com",
        "useSpawn": "false",
        "timer": "20",
        "oldrc": false,
        "name": "",
        "x": 680,
        "y": 420,
        "wires": [
            [],
            [],
            [
                "df76a9b1.ad3628",
                "396bafc0.2d71d"
            ]
        ]
    },
    {
        "id": "df76a9b1.ad3628",
        "type": "switch",
        "z": "343e08a1.fc3668",
        "name": "?",
        "property": "payload",
        "propertyType": "msg",
        "rules": [],
        "checkall": "true",
        "repair": false,
        "outputs": 0,
        "x": 850,
        "y": 460,
        "wires": []
    },
    {
        "id": "9833a944.4ef348",
        "type": "ssh-client",
        "z": "343e08a1.fc3668",
        "debug": false,
        "ssh": "",
        "hostname": "",
        "name": "",
        "x": 980,
        "y": 380,
        "wires": [
            []
        ]
    },
    {
        "id": "a3049fe5.bf31f",
        "type": "delay",
        "z": "343e08a1.fc3668",
        "name": "",
        "pauseType": "delay",
        "timeout": "10",
        "timeoutUnits": "minutes",
        "rate": "1",
        "nbRateUnits": "1",
        "rateUnits": "second",
        "randomFirst": "1",
        "randomLast": "5",
        "randomUnits": "seconds",
        "drop": false,
        "x": 510,
        "y": 420,
        "wires": [
            [
                "4d4b7c6d.4e71f4"
            ]
        ]
    },
    {
        "id": "396bafc0.2d71d",
        "type": "debug",
        "z": "343e08a1.fc3668",
        "name": "",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "false",
        "x": 850,
        "y": 520,
        "wires": []
    }
]

The exit code for success (in the exec node) is 0, anything else is not good.

But I am not sure what your flow is doing. You have a ping node, switch, delay and an exec node that pings again (?)

Try this flow:

[{"id":"d0e65ce4.139f68","type":"inject","z":"c7a7f9b9.d798d8","name":"","topic":"","payload":"","payloadType":"date","repeat":"600","crontab":"","once":true,"onceDelay":0.1,"x":214,"y":528,"wires":[["6c3b9a6e.1bc3ac"]]},{"id":"6c3b9a6e.1bc3ac","type":"exec","z":"c7a7f9b9.d798d8","command":"ping -c1 google.com","addpay":false,"append":"","useSpawn":"false","timer":"","oldrc":false,"name":"","x":412,"y":528,"wires":[[],[],["5604c714.79324"]]},{"id":"1362fae2.70de5d","type":"debug","z":"c7a7f9b9.d798d8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":770,"y":480,"wires":[]},{"id":"5604c714.79324","type":"switch","z":"c7a7f9b9.d798d8","name":"success ?","property":"payload.code","propertyType":"msg","rules":[{"t":"eq","v":"0","vt":"num"},{"t":"else"}],"checkall":"true","repair":false,"outputs":2,"x":612,"y":528,"wires":[["1362fae2.70de5d"],[]]}]

I did not include the ssh client as I am not familiair with it.

Thank you @bakman2 for the flow. Is is odd that when I tried to catch the non-zero on the exec node, it didn't work for me. I will try your code.

If someone can please assist with the SSH node, that will be great! Thanks!

I haven't used the SSH node, when I want to execute a remote command via ssh I use a normal Exec node with a command something like ssh user@ip_address 'reboot'. If the reboot command needs to be sudo reboot then the device being rebooted needs to be configured to not need a password for that command.

thanks @Colin! so for me because i have a password, would it be ssh user:password@ip_address 'reboot'

I guess so, but you would be better to setup ssh keys, if possible, so you can access without having to include the password in plain text anywhere.

Ok, found that it is possible to use the bigssh node to run the command. I will post the resulting flow in a bit.

This is the solution I ended up with using inject, exec, delay, switch, and bigssh nodes. The 2nd ping is just to double check if the first ping was by error.

[
    {
        "id": "343e08a1.fc3668",
        "type": "tab",
        "label": "Reboot Router",
        "disabled": false,
        "info": ""
    },
    {
        "id": "be893600.a1e798",
        "type": "inject",
        "z": "343e08a1.fc3668",
        "name": "",
        "topic": "",
        "payload": "",
        "payloadType": "date",
        "repeat": "600",
        "crontab": "",
        "once": true,
        "onceDelay": 0.1,
        "x": 110,
        "y": 160,
        "wires": [
            [
                "b9cc89b6.26d908"
            ]
        ]
    },
    {
        "id": "b9cc89b6.26d908",
        "type": "exec",
        "z": "343e08a1.fc3668",
        "command": "ping -c 2",
        "addpay": false,
        "append": "www.google.com",
        "useSpawn": "false",
        "timer": "20",
        "oldrc": false,
        "name": "",
        "x": 280,
        "y": 160,
        "wires": [
            [],
            [],
            [
                "23b16edf.19e052"
            ]
        ]
    },
    {
        "id": "f56309e4.5808a8",
        "type": "debug",
        "z": "343e08a1.fc3668",
        "name": "success",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "true",
        "targetType": "full",
        "x": 600,
        "y": 140,
        "wires": []
    },
    {
        "id": "23b16edf.19e052",
        "type": "switch",
        "z": "343e08a1.fc3668",
        "name": "success ?",
        "property": "payload.code",
        "propertyType": "msg",
        "rules": [
            {
                "t": "eq",
                "v": "0",
                "vt": "num"
            },
            {
                "t": "else"
            }
        ],
        "checkall": "true",
        "repair": false,
        "outputs": 2,
        "x": 440,
        "y": 180,
        "wires": [
            [
                "f56309e4.5808a8"
            ],
            [
                "d2f00f74.e965b"
            ]
        ]
    },
    {
        "id": "1f458afc.ef4aa5",
        "type": "exec",
        "z": "343e08a1.fc3668",
        "command": "ssh username:yourpassword@192.168.1.1:23 'reboot'",
        "addpay": false,
        "append": "",
        "useSpawn": "false",
        "timer": "20",
        "oldrc": false,
        "name": "reboot",
        "x": 1070,
        "y": 420,
        "wires": [
            [],
            [],
            [
                "65455786.0d98f8"
            ]
        ]
    },
    {
        "id": "d2f00f74.e965b",
        "type": "delay",
        "z": "343e08a1.fc3668",
        "name": "",
        "pauseType": "delay",
        "timeout": "2",
        "timeoutUnits": "minutes",
        "rate": "1",
        "nbRateUnits": "1",
        "rateUnits": "second",
        "randomFirst": "1",
        "randomLast": "5",
        "randomUnits": "seconds",
        "drop": false,
        "x": 600,
        "y": 240,
        "wires": [
            [
                "300b8fc8.07e64"
            ]
        ]
    },
    {
        "id": "300b8fc8.07e64",
        "type": "exec",
        "z": "343e08a1.fc3668",
        "command": "ping -c 2",
        "addpay": false,
        "append": "www.google.com",
        "useSpawn": "false",
        "timer": "20",
        "oldrc": false,
        "name": "",
        "x": 760,
        "y": 240,
        "wires": [
            [],
            [],
            [
                "85541d7b.bd7d2"
            ]
        ]
    },
    {
        "id": "85541d7b.bd7d2",
        "type": "switch",
        "z": "343e08a1.fc3668",
        "name": "success ?",
        "property": "payload.code",
        "propertyType": "msg",
        "rules": [
            {
                "t": "eq",
                "v": "0",
                "vt": "num"
            },
            {
                "t": "else"
            }
        ],
        "checkall": "true",
        "repair": false,
        "outputs": 2,
        "x": 920,
        "y": 260,
        "wires": [
            [
                "658f0e50.3544b"
            ],
            [
                "6c8d313c.b659a",
                "1f458afc.ef4aa5"
            ]
        ]
    },
    {
        "id": "6c8d313c.b659a",
        "type": "debug",
        "z": "343e08a1.fc3668",
        "name": "error",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "payload",
        "targetType": "msg",
        "x": 1090,
        "y": 340,
        "wires": []
    },
    {
        "id": "658f0e50.3544b",
        "type": "debug",
        "z": "343e08a1.fc3668",
        "name": "success#2",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "payload",
        "targetType": "msg",
        "x": 1090,
        "y": 200,
        "wires": []
    },
    {
        "id": "65455786.0d98f8",
        "type": "debug",
        "z": "343e08a1.fc3668",
        "name": "reboot",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "payload",
        "targetType": "msg",
        "x": 1150,
        "y": 480,
        "wires": []
    }
]

You can simplify it somewhat using a Trigger node instead of the delay. Feed the Fail output from the ping into a trigger node set to send nothing then send the fail message after two minutes. Feed the Success output of the ping into a Change node that sets msg.reset and also feed that into the trigger. Set the Inject node to fire every 15 seconds (or whatever). Pass the output of the trigger to the reboot node.
The result is that a fail will start the trigger timing out but if a good one arrives it will reset it so aborting the reboot.
Set the timeout on the Trigger node long enough that the reboot will occur and a good ping will happen before it will trigger another one or you could get stuck in a perpetual reboot loop. Alternatively put a Delay node set to Rate Limit and Discard Intermediate Messages so that only 1 reboot can occur every 10 mins (or whatever).

1 Like