微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

通过 ssh 和 cronsshpass、expect 或其他解决方案自动重启 ip-phones

如何解决通过 ssh 和 cronsshpass、expect 或其他解决方案自动重启 ip-phones

任务是每晚远程重启ip-phone。

sshpass:
sshpass -p 12345678 ssh -o StrictHostKeyChecking=no admin@192.168.2.1 reboot
响应:到 192.168.2.1 的连接被远程主机关闭
此外,电话与 CLI 中的往常一样,要求回答 y

预期:

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

当我从控制台自己做​​时,一切正常。使用cron,在debug.log 中,由于某种原因,在设备上授权后,输入expect \"*>\" 命令的行没有出现。即使我们删除 expect \"*>\" 并只使用 send,也没有反应。
我寻求解决问题的提示或提出其他解决方案。谢谢。

控制台和 cron 之间的debug.log不同
控制台

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

解决方法

我不知道这是否能解决您的问题:没有看到调试输出就无法判断。

当您将 expect 代码嵌入到 shell 脚本中时,我建议使用 heredoc —— 这样可以使引用更简单

expect <<'END_EXPECT'

    log_file debug.log
    exp_internal 1
    set timeout 10
    spawn ssh admin@192.168.2.1
    expect "password:"
    send "12345678\r"
    expect "*>"
    send "reboot\r"
    expect "y/N"
    send "y\r"
    expect eof

END_EXPECT

我增加了超时时间并删除了睡眠——如果模式正确,通常在期望脚本中不需要睡眠。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。