脚本SSH到路由器和“启用”并运行命令

我有一个脚本,将ssh路由器的列表,并运行command.txt commands.txt 。 我想在通过ssh连接到路由器之前enable我在ssh执行其余的commands.txt我有两个问题。 首先,我如何传递启用命令? 它似乎没有做任何事情,当我把它作为我的第一行在commands.txt. 第二,一旦我找出enable提示部分,我将如何通过我的凭据的脚本是全自动的?

脚本

在这里输入图像说明

commands.txt中

在这里输入图像说明

当我运行这个当前脚本时,当它提示我连接到路由器时input我的密码。 然后它只显示% Type "show ?" for a list of subcommands % Type "show ?" for a list of subcommands然后继续并连接到下一个路由器,并要求input我的凭据。

考虑看RANCID 。 它主要用作思科和其他networking设备的configuration备份工具,但它的一个组件clogin本质上是一个Expect脚本,它完全符合你的要求。 在你的例子中使用命令和路由器文件列表,你可以像这样在所有的路由器上执行命令:

 clogin -u user -p pass -e enablepass -x commands.txt $(cat routers.txt) 

它可以读取configuration文件 ,您可以在其中存储login名和密码,这样可以避免在命令行上传递它们。 它有多个login选项,所以它可以处理你不使用aaa new-model ,只在VTY上有密码,而在较旧的情况下,要求input用户名以响应enable命令。

唯一真正的缺点是它不是特别安静。 例如,如果你想输出show version ,你会得到,但你也会得到所有的login横幅,login/密码提示和命令提示符在您的输出。

我会看看使用像预期的东西,而不是试图从文件redirect。 预计将处理密码提示等,并根据输出结果回复你想要的信息。

http://expect.sourceforge.net/

根据rhel6,这个软件包将被称为expect。 还有一个python模块,可以从rhel6更新库中findpexpect。 perl模块也是这样做的,称为perl-Expect。

这可能会给你更好的结果。

这页http://nixcraft.com/showthread.php/15060-script-to-auto-login-to-cisco-router-(-telnet-and-SSH)给出了你正在尝试做的一些例子。

编辑:刚刚发现从cisco另一个链接描述这也是:

https://supportforums.cisco.com/discussion/11553001/script-automate-tasks

我已经使用这个Perl脚本大约一年了。 它是我在各种论坛上发现的perl脚本的几个片段的演变加上一些试验和错误,但它运作良好。 您将需要安装模块Time :: HiRes for usleep,但是我发现它有助于在远程交换机和路由器上执行命令的响应时间稍长一些。 它将跳过不响应的设备或authentication失败的设备,并继续使用列表中的下一个设备,并为每个使用其IP地址命名的设备提供一个日志文件。

希望对某人有所帮助。

 # # usage: perl cisco.pl (target_hostfile) (ssl_password) (enable_password) # use Net::SSH2; use warnings; use strict; use Time::HiRes qw(usleep); use File::Slurp; print "\nEnter switch list filenane: "; my $input = <STDIN>; ### target hosts file from STDIN chomp($input); my @hostfile = read_file($input,chomp => 1); ### read target host file my $arrSize = @hostfile; my $user = "username"; ### your username print "\nEnter ssh password: "; my $password = <STDIN>; ### ssh password from STDIN chomp($password); print "\nEnter enable password: "; my $secret = <STDIN>; ### enable password from STDIN chomp($secret); print "\nEnter command delay in miliseconds: "; my $delay = <STDIN>; chomp($delay); my $delaytime = $delay/1000; print "\nCopmmand delay confirmed: $delaytime Seconds\n"; print "\nEnter command list filename: "; my $commandlist = <STDIN>; chomp($commandlist); my @commandfile = read_file($commandlist,chomp => 1); my $commandsize = @commandfile; my $i = 0; my $j = 0; $delay*=1000; ### convert delay into microseconds for usleep command for ($i = 0; $i < $arrSize; $i++){ my $ssh = Net::SSH2->new(); if(!$ssh->connect($hostfile[$i])){ print("Connection Failed ($hostfile[$i]\n"); next; } usleep($delay); ### time in microsecond if(!$ssh->auth_password($user,$password)){ print("Authentication Failed ($hostfile[$i]\n"); next; } usleep($delay); ### time in microsecond my $channel = $ssh->channel(); $channel->blocking(0); $channel->shell(); print $channel "enable\n"; usleep($delay); ### time in microsecond print $channel "$secret\n"; ### enable password from input string3 usleep($delay); ### time in microsecond print $channel "term len 0\n"; usleep($delay); ### time in microsecond for ($j = 0; $j < $commandsize; $j++){ print $channel "$commandfile[$j]\n"; usleep($delay); ### time in microsecond so 1 millisecond == 1000 microseconds } open (my $OUTPUTFILE, ">$hostfile[$i].log") or die "Can't open $hostfile[$i].log: $!"; ### target host as filename while (<$channel>) { chomp; print $OUTPUTFILE "$_"; } close $OUTPUTFILE or die "$OUTPUTFILE: $!"; } close $OUTPUTFILE or die "$OUTPUTFILE: $!"; }