如何发送多个命令

发布于 2024-12-21 00:20:59 字数 804 浏览 0 评论 0原文

我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

許願樹丅啲祈禱 2024-12-28 00:20:59

如果您问我认为您在问什么,只需在其自己的线程中运行每个 $chan->exec 命令(警告:未经测试):

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);
}

看看 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):

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);
}

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 a shared variable) and then using a semaphore (or even a lock) to make sure that the remote terminal isn't confused by one thread's command trying to step on the other thread's command.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文