如何发送多个命令
我在 Windows 上运行了这个脚本。我需要帮助如何向当前仅在一台设备上运行的思科设备添加辅助命令。还有我如何在文件上而不是屏幕上打印结果。
#!\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";
I have this script running on windows. I need help how to add secondary command to cisco devices currently only working on one device. Also how I can print out the result on file instead of the screen.
#!\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";
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您问我认为您在问什么,只需在其自己的线程中运行每个
$chan->exec
命令(警告:未经测试):看看
perldoc perlthrtut
有关线程的更多内容。编辑添加:上述方法的缺点是您要启动两个 SSH 连接(每个线程一个),而不是仅一个。可以通过在线程之外启动一个连接(并将
$ssh2
作为一个共享
变量)然后使用信号量(甚至是一个锁
),以确保远程终端不会因一个线程的命令试图执行另一个线程的命令而感到困惑。If you're asking what I think you're asking, just run each
$chan->exec
command in its own thread (Warning: Untested):Take a look at
perldoc perlthrtut
for more stuff about threads.Edited to add: The disadvantage of the approach above is that you're firing up two SSH connections (one per thread) instead of just one. An extra layer of sophistication could be added here by firing up one connection outside of the threads (and having
$ssh2
as ashared
variable) and then using a semaphore (or even alock
) to make sure that the remote terminal isn't confused by one thread's command trying to step on the other thread's command.