【问题标题】:Automatic restart of ip-phones via ssh and cron (sshpass, expect or other solutions)通过 ssh 和 cron(sshpass、expect 或其他解决方案)自动重启 ip-phone
【发布时间】:2022-04-21 15:13:03
【问题描述】:

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

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

【问题讨论】:

    标签: ssh cron expect sshpass


    【解决方案1】:

    我不知道这是否能解决您的问题:没有看到调试输出就不可能说出来。

    当您将期望代码嵌入到 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
    

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

    【讨论】:

    • 我从 cron 中看到,你永远不会得到提示。尝试使用ssh -t 强制 tty。
    • 不工作。为了测试,我在 VBS 上为 SecureCRT 编写了一个脚本,它在任务调度程序中运行良好。很遗憾我可能不得不使用这个软件。也许Linux中还有其他方法?
    【解决方案2】:

    重启潮流VoIP终端-脚本解决方案:

    #!/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
    

    【讨论】:

    • 您的答案可以通过额外的支持信息得到改进。请edit 添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以找到更多关于如何写好答案的信息in the help center
    猜你喜欢
    • 1970-01-01
    • 2013-09-20
    • 1970-01-01
    • 2019-03-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-25
    相关资源
    最近更新 更多