【问题标题】:Batch file to SSH Cisco phone and reboot it批处理文件到 SSH Cisco 电话并重新启动它
【发布时间】:2019-03-22 07:30:20
【问题描述】:

第一篇文章,一般来说是批处理和脚本的新手,所以请放轻松。

我的任务是编写一个 Perl 脚本,该脚本将启动与 6 部手机的 SSH 会话,并向它们发送一个 CLI 命令以重新启动它们。有趣的是,我知道 JACK 关于 Perl。我确实知道一点批处理自动化,并认为使用 plink 来做我需要的事情会很简单。我错了。

这是我的 plink 批处理示例。

@echo off
plink.exe -t -ssh XXXXXXX@111.111.1.1 -pw Abcd1234 < "C:\commands\commands.txt">output.txt
pause

Commands.txt:

debug

debug

reset hard

运行批处理文件会返回一个 cmd 窗口显示:Using Username "XXXXXXX" 我的 output.txt 显示:

debug



debug



reset hardsnoopyplus login:

此时我意识到在发送重置命令之前,我需要了解如何处理第二组登录凭据。

我们现在是第 3 天,我的老板想要一些结果。我的剧本有大约 4 次损坏的迭代和良好的态度。我可以尝试什么?

另外,这是我发现的一个 Perl 脚本的一部分,它声称能够处理我无法工作的 cisco 电话的 snoopyplus 调试登录。我发现的一点信息告诉我用 OpenSSH 重写它,我认为 Windows 不支持它。

sub reboot{ 

my ($ip,$sshpassword,$sshusername,$debuglogin,$debugusername,$debugpassword,$debugprompt,$command) = @_;

print "\nPlease be patient whilst Cisco phone at address $ip is rebooted. This may take up to 90 seconds to complete...\n";  

my $ssh = Net::SSH::Perl->new("111.111.1.1"); 

my $login_output = $ssh->login();

#my $ssh->login($sshusername, $sshpassword) || myAuditLog(LOG,"Login has failed. $!");

$ssh->waitfor('/'.$debuglogin.'>.*$/');
$ssh->send($debugusername);

$ssh->waitfor('/Password :.*$/');
$ssh->send($debugpassword);

$ssh->waitfor('/'.$debugprompt.'>.*$/');
$ssh->exec($command);

$ssh->close();

【问题讨论】:

    标签: perl batch-file ssh cisco


    【解决方案1】:

    如果您使用的是 Windows,请查看 Net::SSH2,它包含在 Strawberry Perl 中。使用它,您应该能够与您的 IP 电话通话:

    #!perl
    use strict;
    use warnings;
    use Net::SSH2;
    
    my $ssh2 = Net::SSH2->new();
    
    $ssh2->connect('example.com') or $ssh2->die_with_error;
    
    if ($ssh2->auth_password($debuglogin, $debugpassword)) {
        my $chan = $ssh2->channel();
        $chan->shell();
        while (!$channel->eof) {
            if (my ($o, $e) = $channel->read2) {
                $out .= $o;
                $err .= $e;
            } else {
                $ssh2->die_with_error;
            }
        }
    };
    

    【讨论】:

    • 谢谢!当我研究 Net::SSH:Expect 的 Windows 兼容替代方案时,我查看了 Net::SSH2,但不幸的是,我对这种语言知之甚少,恐怕我看不到你的代码。我已经安装了 Strawberry Perl,并且我正在学习一些初学者课程来了解我的方向。与此同时,我仍在修补批处理文件和 Putty,并祈祷我能取得突破!
    【解决方案2】:

    试试这种格式的 plink 命令:

    plink.exe -t -ssh -pw Abcd1234 -m "C:\commands\commands.txt" XXXXXXX@111.111.1.1 >output.txt
    

    【讨论】:

    • 感谢您的回复。运行该格式后,我在关闭之前得到一个 cmd 窗口的闪烁,并且 output.txt 是空白的。在以前的迭代中,我无法使“-m”工作,它只在我使用 时运行
    • 尝试删除输出重定向&gt;output.txt并用命令pause再添加一行
    • 类似:@echo off plink.exe -t -ssh -pw Abcd1234 -m "C:\commands\commands.txt" XXXXXXX@111.111.1.1 pause(请原谅我从未尝试在评论中嵌入代码的编辑。)
    • 上面的代码打开了一个已经激活暂停的 cmd 窗口。没有迹象表明它实际上运行了命令。
    • 所以先打开你的控制台窗口,然后输入命令然后回车?现在会发生什么?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-08
    • 1970-01-01
    • 2016-11-27
    • 2013-09-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多