Perl 如何发送多个命令
我有这个脚本在windows上运行。我需要帮助如何添加辅助命令到cisco设备目前只在一个设备上工作。还有如何在文件而不是屏幕上打印结果Perl 如何发送多个命令,perl,Perl,我有这个脚本在windows上运行。我需要帮助如何添加辅助命令到cisco设备目前只在一个设备上工作。还有如何在文件而不是屏幕上打印结果 #!\usr\bin\Perl\bin\perl use warnings; use strict; use NET::SSH2; my $host = "switchA"; # use the ip host to connect my $user = "XXX"; # your account my $pass = "XXXX"; # your pass
#!\usr\bin\Perl\bin\perl
use warnings;
use strict;
use NET::SSH2;
my $host = "switchA"; # use the ip host to connect
my $user = "XXX"; # your account
my $pass = "XXXX"; # your password
my $ssh2 = Net::SSH2->new();
$ssh2->debug(0);
$ssh2->connect($host) or die "Unable to connect host $@ \n";
$ssh2->auth_password($user,$pass);
#shell use
my $chan = $ssh2->channel();
$chan->exec('sh int desc');
my $buflen = 3000;
my $buf1 = '0' x $buflen;
$chan->read($buf1, $buflen);
print "CMD1:\n", $buf1,"\n";
# run another command still not working
$chan->exec('sh ver');
my $buflen2 = 3000;
my $buf2 = '0' x $buflen2;
$chan->read($buf2, $buflen2);
print "CMD2:\n", $buf2,"\n";
如果你问的是我认为你在问的问题,只需在自己的线程中运行每个
$chan->exec
命令(警告:未测试):
查看更多关于线程的内容
编辑后添加:上述方法的缺点是您要启动两个SSH连接(每个线程一个),而不是一个。通过在线程外启动一个连接(并将
$ssh2
作为共享的变量),然后使用信号量(甚至是锁
),可以在这里增加一层复杂度以确保远程终端不会被一个线程的命令所迷惑,该命令试图跨接另一个线程的命令。我不确定您在问什么。是否要异步运行两个$chan->exec
命令?如果是这样,您需要在线程中运行它们(或设置某种事件处理程序)。什么是“不工作”?它会打印错误信息吗?谢谢大家!第一个命令输出第二个命令是空白的只需打印“CMD2”我想我的问题是Jack指出的“但不确定如何添加“线程”或“处理程序”“执行这两个命令的脚本。@Jack Maney:您不能从两个不同的线程使用同一个Net::SSH2对象。SSH连接会断开。对于网络设备来说,每个连接只接受一个命令/通道并不少见……要打印到文件,只需将filehandle指定为print
的间接对象,例如:open file,“>”、“/some/file/name”或die“$!”代码><代码>打印文件“CMD2:\n”,…
@BRPocock-谢谢提醒。编辑已经完成。谢谢杰克,脚本现在有这个错误“无法在第50行和第35行调用方法‘EXEC’未定义值。我不知道为什么这是现在的错误。这不是以前的问题,您也没有更改任何内容。@DanielEstifanos-在子例程中,似乎$chan
没有得到定义。因为我不知道第35行和第50行在代码中的什么位置,所以您必须确保$chan
和$ssh2
都有正确的定义和范围。第35行是“$chan->exec('sh int desc');第50行是“$chan->exec('sh ver');我花了很多时间都不知道问题出在哪里。我在使用一个命令运行时没有看到任何问题,但现在没有了,我认为线程没有任何冲突问题。再次感谢!!
use warnings;
use strict;
use NET::SSH2;
use threads;
use threads::shared;
#We'll be making a Net::SSH2 object in each thread,
#so these parameters will need to be shared between the threads.
my $host :shared = "switchA"; # use the ip host to connect
my $user :shared = "XXX"; # your account
my $pass :shared= "XXXX"; # your password
#NOTE: The shell use (via $ssh2 and $chan) has been passed
#to the subroutines foo and bar.
#Create two threads,
#one which will perform the subroutine foo,
#and the other which will perform the subroutine bar.
my $thread1=threads->create(\&foo);
my $thread2=threads->create(\&bar);
#Wait for the threads to finish.
$thread1->join;
$thread2->join;
sub foo
{
my $ssh2 = Net::SSH2->new();
$ssh2->debug(0);
$ssh2->connect($host) or die "Unable to connect host $@ \n";
$ssh2->auth_password($user,$pass);
my $chan = $ssh2->channel();
$chan->exec('sh int desc');
my $buflen = 3000;
my $buf1 = '0' x $buflen;
$chan->read($buf1, $buflen);
open(my $write,">","/output/file/foo") or die $!;
print $write "CMD1:\n", $buf1,"\n";
close($write);
}
sub bar
{
my $ssh2 = Net::SSH2->new();
$ssh2->debug(0);
$ssh2->connect($host) or die "Unable to connect host $@ \n";
$ssh2->auth_password($user,$pass);
my $chan = $ssh2->channel();
$chan->exec('sh ver');
my $buflen2 = 3000;
my $buf2 = '0' x $buflen2;
$chan->read($buf2, $buflen2);
open(my $write,">","/output/file/bar") or die $!;
print $write "CMD2:\n", $buf2,"\n";
close($write);
}