#!/usr/bin/perl # # Copyright (c) 2002 TAKANO Yuji # All rights reserved. # # Redistribution and use in source and binary forms, with or without # modification, are permitted provided that the following conditions # are met: # 1. Redistributions of source code must retain the above copyright # notice, this list of conditions and the following disclaimer. # 2. Redistributions in binary form must reproduce the above copyright # notice, this list of conditions and the following disclaimer in the # documentation and/or other materials provided with the distribution. # # THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE # ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS # OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF # SUCH DAMAGE. # # bgprouteget.pl - Show IPv6 BGP # ###################################################################### # モジュール呼び出し ###################################################################### use Net::Telnet (); ###################################################################### # Zebra コマンドプロンプト ###################################################################### my $Prompt_passwd = 'Password: '; ###################################################################### # 初期変数設定 ###################################################################### #ホスト名、port、パスワードの設定 my $Host_Name = 'cisco-router'; my $Port_No = '2605'; my $Passwd = '3750'; # zebra に送るコマンドイメージ #my $Zebra_Command = 'show ip bgp'; my $Zebra_Command = 'show ipv6 bgp'; #出力形式を編集するかしないかの設定 #[-v] オプションで綺麗に出力してくれる(冗長出力オプション) my $List_mode = ''; ###################################################################### # パラメータの取得(パラメータが無ければ初期変数設定を使用) ###################################################################### my $In = shift; if ($In eq '-h' || $In eq '--help') { print "usage: $0 [-v] [hostname:port] [password]\n\n"; exit 1; } if ($In eq '-v') { $List_mode = 'EDIT'; $In = shift; } ($Host_Name,$Port_No) = split(':',$In) if ($In); $In = shift; $Passwd = $In if ($In); ###################################################################### # コマンドの実行 ###################################################################### # ログファイルの設定 # ログが欲しい時には new Net::Telnet のコメントを外すと取れるようになる my $Log_input = '/tmp/input'; my $Log_output = '/tmp/output'; my $Log_dump = '/tmp/dump'; my $TelSock = new Net::Telnet( Host => $Host_Name, Port => $Port_No, # Input_log => $Log_input, # Output_log => $Log_output, # Dump_Log => $Log_dump, Errmode => "return" ); $TelSock->waitfor(String => $Prompt_passwd); #$TelSock->waitfor($Prompt_passwd); $TelSock->print("$Passwd"); # -- More -- の抑制 $TelSock->cmd("terminal length 0"); print "Network\t\t\tWeight Path\n" if ($List_mode eq 'EDIT'); foreach $Line ($TelSock->cmd("$Zebra_Command")) { if ($List_mode eq 'EDIT') { &Cooked($Line); } else { print $Line; } } $TelSock->close; print "\n\n"; exit; ###################################################################### # 出力形式を編集するロジック(あんまり綺麗では無い;-) ###################################################################### sub Cooked { my $Line = shift; my $Put_Line = ""; my ($Head,$Net,$t1,$t2,$t3,@tmp) = split(' ',$Line); if ($Head =~ /\*>/) { if ($tmp[0]) { $Put_Line = "$Net\t\t$t3 @tmp\n"; } else { $Put_Line = "$Net"; } } elsif ($Head =~ /fe80::249d:4c/) { $Put_Line .= "\t$t1 $t2 $t3 @tmp\n"; } elsif ($Head =~ /::/) { $Put_Line .= "\t$t1 $t2 $t3 @tmp\n"; } else { return; } print "$Put_Line"; return; }