web-dev-qa-db-ja.com

ルータへのSSHのスクリプトを作成し、「有効化」してコマンドを実行する

ルーターのリストにsshしてcommands.txtからコマンドを実行するスクリプトがあります。 enableを通じてルーターに接続した後、sshを実行したいのですが、残りのコマンドをcommands.txtで実行する前に、2つの質問があります。 最初に enableコマンドを渡すにはどうすればよいですか? commands.txt.Second、の最初の行として挿入すると、何も実行していないようです。enableプロンプトの部分を理解したら、資格情報をどのように渡しますかスクリプトを完全に自動化するには

スクリプト

enter image description here

Commands.txt

enter image description here

この現在のスクリプトを実行するときに、ルーターへの接続を求めるメッセージが表示されたら、パスワードを入力します。次に、% Type "show ?" for a list of subcommandsが表示され、次に進み、次のルーターに接続して、資格情報を要求します。

2
runcmd

[〜#〜] rancid [〜#〜] を見ることを検討してください。これは主に、シスコやその他のネットワーキングデバイスの構成バックアップツールとして使用することを目的としていますが、そのコンポーネントの1つである clogin は、基本的に、期待どおりの処理を行うExpectスクリプトです。この例のコマンドとルーターファイルのリストを使用すると、次のようにすべてのルーターでコマンドを実行できます。

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

ログイン名とパスワードを保存できる 構成ファイル を読み取ることができるので、コマンドラインでそれらを渡すことを回避できます。複数のログインオプションがあるため、_aaa new-model_を使用せず、VTYにパスワードしかない場合や、enableコマンドに応答してユーザー名を要求する古いギアの場合に対処できます。

唯一の本当の欠点は、それが特に静かではないということです。たとえば、_show version_の出力が必要な場合はそれが表示されますが、出力にはすべてのログインバナー、ログイン/パスワードプロンプト、およびコマンドプロンプトも表示されます。

3

このPerlスクリプトを使用してから約1年になります。これは、私がさまざまなフォーラムで見つけたPerlスクリプトのいくつかのスニペットの進化に加えて、試行錯誤のビットですが、うまく機能します。 usleepのモジュールTime :: HiResをインストールする必要がありますが、コマンドへの応答時間が少し長いリモートスイッチおよびルーターでのコマンドの実行に役立つことがわかりました。認証に応答しない、または認証に失敗したデバイスをスキップして、リスト内の次のデバイスに進み、その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: $!";
}
2
dean

私は単にファイルからリダイレクトしようとするのではなく、期待のようなものを使用することを考えます。 expectは、パスワードのプロンプトなどを処理し、出力に基づいて必要な情報で応答します。

http://expect.sourceforge.net/

Rhel6では、パッケージはexpectと呼ばれます。また、pythonモジュールは、pexpectと呼ばれる同じことを行うモジュールで、rhel6更新リポジトリから入手できます。同じことを行うPerlモジュールは、Perl-Expectと呼ばれます。

これはおそらくあなたにより良い結果を与えるでしょう。

このページ http://nixcraft.com/showthread.php/15060-script-to-auto-login-to-Cisco-router-(-telnet-and-SSH) は、いくつかの例を示していますあなたがしようとしている。

編集:これも説明しているシスコの別のリンクを見つけました:

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

1
lsd