ラベル IOS の投稿を表示しています。 すべての投稿を表示
ラベル IOS の投稿を表示しています。 すべての投稿を表示

2017/03/21

section フィルタ / Cisco IOS の小技

Cisco IOS で「show running-config」などを実行すると、出力が長くなり、本当に見たい部分を探すのに手間がかかる。
従来から、includeexclude および begin 等のフィルタは準備されていたが、コンフィグの文法に基づいて動作するものではないため、少々不便。
そこで今回紹介するのが section フィルタ。
文字通り、指定したセクションのみを週出してくれるフィルタ。

以下の様に使うことができる:
Switch#sh run | section line
line con 0
line vty 0 4
 transport input ssh
line vty 5 15
 transport input ssh
Switch#

|」(パイプ)の後で使えるフィルタのバリエーションが限られているため、省略形「s」のみで使用することができる。
Switch#sh run | s interface
interface FastEthernet0
 no ip address
interface GigabitEthernet1/0/1
interface GigabitEthernet1/0/2
<<略>>
interface GigabitEthernet1/0/27
interface GigabitEthernet1/0/28

空白で区切って、長いセクション名を指定することができる。
Switch#show conf | s interface FastEthernet
interface FastEthernet0
 no ip address
Switch#

ただし、省略形を使用できない(場合がある)。
Switch#show run | s int Fas
Switch#

正規表現が使えるので、以下の様に実行すれば、期待した動作になる。
Switch#show run | s int.* Fas.*
interface FastEthernet0
 no ip address
Switch#

GigabitEthernet ポートの 20 番台のみを表示しようとして、以下の様に実行しても、ポート 2 が表示され、思った結果にならない。
Switch#show run | s interface GigabitEthernet1/0/2
interface GigabitEthernet1/0/2
interface GigabitEthernet1/0/20
interface GigabitEthernet1/0/21
interface GigabitEthernet1/0/22
interface GigabitEthernet1/0/23
interface GigabitEthernet1/0/24
interface GigabitEthernet1/0/25
interface GigabitEthernet1/0/26
interface GigabitEthernet1/0/27
interface GigabitEthernet1/0/28
Switch#
この場合は、以下の様に実行すればよい。
Switch#show run | s interface GigabitEthernet1/0/2.
interface GigabitEthernet1/0/20
interface GigabitEthernet1/0/21
interface GigabitEthernet1/0/22
interface GigabitEthernet1/0/23
interface GigabitEthernet1/0/24
interface GigabitEthernet1/0/25
interface GigabitEthernet1/0/26
interface GigabitEthernet1/0/27
interface GigabitEthernet1/0/28
Switch#

show コマンドではなく、more コマンドの後でも来した通り動作する。
Switch#$/archive/config-Mar-19-14-03-06.375-41 | section interface Vlan
interface Vlan1
 no ip address
interface Vlan1000
 ip address 10.0.0.1 255.255.255.0
Switch#

2017/02/06

IOS.shの実行中断

前回紹介したIOS.shの実行中断に関する補足。
前回は、以下のように紹介した:
他のコマンド(ping等)と同様、[Ctrl]+[Alt]+[6] (HEX 1E。MacのTerminalからだと[Ctrl]+[^]らしい)で実行を中断することができる。
しかし、実際には止まらない場合がある。例えば:
Switch#while true; do echo "#### `show clock`"; ping 10.0.0.3 repeat 1; sleep 1; done
#### 04:26:14.555 JST Tue Feb 7 2017
Type escape sequence to abort.
Sending 1, 100-byte ICMP Echos to 10.0.0.3, timeout is 2 seconds:
!
Success rate is 100 percent (1/1), round-trip min/avg/max = 7/7/7 ms
#### 04:26:15.569 JST Tue Feb 7 2017
Type escape sequence to abort.
Sending 1, 100-byte ICMP Echos to 10.0.0.3, timeout is 2 seconds:
!
Success rate is 100 percent (1/1), round-trip min/avg/max = 1/1/1 ms
#### 04:26:16.576 JST Tue Feb 7 2017
Type escape sequence to abort.
Sending 1, 100-byte ICMP Echos to 10.0.0.3, timeout is 2 seconds:
!
これを実行しているときに[Ctrl]+[Alt]+[6]を入力しても、実行中断することができない。

この場合、他のセッションを開いて、上のコマンドを実行中のセッションごと強制終了する。
例えば、上のコマンドを、シリアルコンソール(console 0)から実行している場合、SSHで新しいセッションを開いておいて、以下の通り実行する。
Switch#clear line console 0
[confirm]y [OK]
Switch#
もとのシリアルコンソールは、強制的にログアウトされ、ログインプロンプトが表示される。
Switch con0 is now available





Press RETURN to get started.









User Access Verification

Username:

2017/02/05

Cisco IOSでshell: IOS.sh

Cisco IOS (Apple iOSではない)は、CatalystやCiscoルータと言ったCiscoのネットワーク機器のOS。

コの業界、特にインフラ系では、Ciscoがデファクトスタンダードで、避けては通れない。

で、このIOSは主にCLIで操作するのだが、独自のコマンド体系で、UNIX/Linux系のシェルに慣れている技術者には、少々とっつきにくい。
例えば、パイプ(|)はサポートされているが、grepコマンドではなくincludeコマンドを使う、などなど。

これを少しだけ解決するのが、今回紹介するIOS.sh
IOS上でUNIX系のコマンドの一部と、Bourne Shell系の制御構造が使える。
バッチファイルを使うこともできる(エディタは無いが…)

IOS.shは、IOS 15.1(4)M、15.1(2)S以降のバージョンでサポートされている。
サポートしているIOS上で「cat ?」と入力すると、
Switch>cat ?
The command you have entered is available in the IOS.sh.
However, the shell is currently disabled. You can enable
it on this terminal by typing
   'term shell'
You can also enable it for all terminals by configuring the
   'shell processing full'
command. There is additional information in the man command.
For more information, enable shell, and then enter:
'man IOS.sh'
Switch>cat
と表示されることが確認できる。

起動と停止

IOSにログインして、「terminal shell」あるは短縮形で「term shell」と入力して起動する。
Switch>terminal shell
Switch>
停止は、「terminal no shell」もしくは「term no shell(「no term shell」じゃないんだな、これが)
Switch>terminal no shell
Switch>
または、ログアウトしても停止する。

現在状態の確認

「show terminal」コマンドで調べる。
Switch>show terminal | include Shell
Shell: enabled
Shell trace: off
Switch>

実行中断

他のコマンド(ping等)と同様、[Ctrl]+[Alt]+[6] (HEX 1E。MacのTerminalからだと[Ctrl]+[^]らしい)で実行を中断することができる。
Switch>i=0; while true; do echo $i; ((i=i+1)); done
0
1
2
<<略>>
[Shift]+[Ctrl]+[6]
105
106
107       ^
% Invalid input detected at '^' marker.

Switch>
ちょっとゴミが出てるが気にしない。
追記: [Shift]+[Ctrl]+[6]で実行停止できない場合について、補足した。

マニュアル

Linux/Unix同様、manコマンドが使える。
まずは、「man IOS.sh」を見よ。
Switch>man IOS.sh
NAME
    IOS.sh - process shell input

DESCRIPTION
The IOS.sh is a set of add-on features to the IOS
CLI. These features enables users to write scripts,
execute functions, evaluate numeric and logical expressions,
and perform loops over data using a standard, easy to learn
syntax.

In addition, the IOS.sh offers variable substitution, enhanced
quoting, and a series of builtin functions that are designed to
simplify the job of interacting with an IOS router.

In order to learn about the IOS.sh, a set of man pages such as
this one have been prepared to describe the various new features.
The following man pages may be used learn about the shell.

 man compatibility   -  gives a quick overview of compatibility between
                        shell and traditional IOS CLI
 man variables       -  describes the usage of variables
 man scripting       -  gives a quick overview of the scripting
                        capabilities of IOS.sh
 man expressions     -  describes the expression syntax built into the IOS.sh
 man loops           -  describes loop structures built into IOS.sh
 man functions       -  describes how to use and write functions
 man quoting         -  describes the new quoting facility
Switch>
ちなみに、上の「man .*」の内、手元の環境15.2(2)E5では、「man loops」のみエラーとなった。

なお、単に「man」だけ入力すると、manコマンドで調査可能な項目の一覧が表示される。
Switch>man
((              evaluate a numeric test expression
IOS.sh          The IOS shell
[[              evaluate a logical test expression
cat             output data from a pipe or file to the terminal
compatibility   compatibility information on IOS.sh
control         control constructs in IOS.sh
<<略>>
variables       usage of variables in IOS.sh
wc              count lines, words, and chars
while           iterate while an expression is TRUE

Switch>