'Automatic restart of ip-phones via ssh and cron (sshpass, expect or other solutions)

The task is to remotely reboot ip-phones every night.

sshpass:
sshpass -p 12345678 ssh -o StrictHostKeyChecking=no [email protected] reboot
In response: Connection to 192.168.2.1 closed by remote host.
In addition, the phone, as usual in CLI, asks to answer y.

expect:

COMM="
log_file debug.log
exp_internal 1
set timeout 5
spawn ssh [email protected]
expect \"password:\"
send \"12345678\r\"
sleep 5
expect \"*>\"
send \"reboot\r\"
sleep 5
expect \"*y/N*\"
send \"y\r\"
expect eof
"
expect -c "$COMM"

When I do it myself from the console, everything is ok. Using cron, in debug.log, for some reason, after authorization on the device, the line for entering the expect \"*>\" command does not appear. Even if we remove expect \"*>\" and just use send, there is no reaction.
I ask for hints in solving the problem or suggest another solution. Thanks.

different debug.log between console and cron
console:

expect: does " \r\n" (spawn_id exp5) match glob pattern "*>"? no
^[[1;36m

expect: does " \r\n\u001b[1;36m\r\n\t" (spawn_id exp5) match glob pattern "*>"? no
Grandstream GXV3240 Command-Line Interface Copyright 2017
                Grandstream Networks, Inc.
^[[0m
GXV3240>
expect: does " \r\n\u001b[1;36m\r\n\tGrandstream GXV3240 Command-Line Interface Copyright 2017\r\n\t\tGrandstream Networks, Inc.\r\n\u001b[0m\r\nGXV3240> " (spawn_id exp5) match glob pattern "*>"? yes
expect: set expect_out(0,string) " \r\n\u001b[1;36m\r\n\tGrandstream GXV3240 Command-Line Interface Copyright 2017\r\n\t\tGrandstream Networks, Inc.\r\n\u001b[0m\r\nGXV3240>"
expect: set expect_out(spawn_id) "exp5"
expect: set expect_out(buffer) " \r\n\u001b[1;36m\r\n\tGrandstream GXV3240 Command-Line Interface Copyright 2017\r\n\t\tGrandstream Networks, Inc.\r\n\u001b[0m\r\nGXV3240>"
send: sending "reboot\r" to { exp5 }

cron:

expect: does " " (spawn_id exp4) match glob pattern "*>"? no

^[[1;36m
        Grandstream GXV3240 Command-Line Interface Copyright 2017
                Grandstream Networks, Inc.
^[[0m
^[[6n
expect: does " \r\n\u001b[1;36m\r\n\tGrandstream GXV3240 Command-Line Interface Copyright 2017\r\n\t\tGrandstream Networks, Inc.\r\n\u001b[0m\r\n\u001b[6n" (spawn_id exp4) match glob pattern "*>"? no
expect: timed out
send: sending "reboot\r" to { exp4 }

expect: does " \r\n\u001b[1;36m\r\n\tGrandstream GXV3240 Command-Line Interface Copyright 2017\r\n\t\tGrandstream Networks, Inc.\r\n\u001b[0m\r\n\u001b[6n" (spawn_id exp4) match glob pattern "*y/N*"? no
expect: timed out


Solution 1:[1]

I don't know if this will fix your problem: without seeing the debug output it's impossible to say.

When you're embedding expect code into a shell script, I recommend using a heredoc -- that makes the quoting much simpler

expect <<'END_EXPECT'

    log_file debug.log
    exp_internal 1
    set timeout 10
    spawn ssh [email protected]
    expect "password:"
    send "12345678\r"
    expect "*>"
    send "reboot\r"
    expect "y/N"
    send "y\r"
    expect eof

END_EXPECT

I increased the timeout and removed the sleeps -- generally sleep is not required in an expect script if you have the patterns right.

Solution 2:[2]

Reboot Grandstream VoIP terminal – script solution:

#!/bin/bash
ip_adr="192.168.0.2"
pass="admin"
sshpass -p ${pass} ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no admin@${ip_adr} << EOF
reboot
exit
EOF
unset pass

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Akamos