labunix's blog

labunixのラボUnix

複数の検証環境にmate-terminal+expectでtelnet自動ログインしてみる。

■複数の検証環境にmate-terminal+expectでtelnet自動ログインしてみる。
 以下に当てはめる。

 mate-terminal --tab -e [command] --tab -e [command] ...

■KVMホスト側での待受ポートを設定

$ for n in `seq 21 23` `seq 31 34`;do socat tcp-listen:${n}23,fork tcp-connect:192.168.0.${n}:23 & done
[1] 17254
[2] 17255
[3] 17256
[4] 17257
[5] 17258
[6] 17259
[7] 17260

■expectでtelnet自動ログインスクリプトを書く。
 4つの引数を受け取ってログインパスワード、
 enableパスワードも変えても良いけど、
 検証環境なのでこのままとする。

$ cat mytel 
#!/bin/bash

if [ $# -ne 2 ];then
  echo "Usage: $0 RHOST RPORT"
  exit 1
fi

RHOST=$1
RPORT=$2

expect -c "
  set timeout 5
  spawn   /usr/bin/telnet $RHOST $RPORT
  expect \"Password:\"
  send   \"cisco\n\"
  expect \".*>\"
  send   \"en\n\"
  expect \"Password:\"
  send   \"cisco\n\"
  expect \".*#\"
  interact
"

$ ./mytel 172.31.31.92 2123
spawn /usr/bin/telnet 172.31.31.92 2123
Trying 172.31.31.92...
Connected to 172.31.31.92.
Escape character is '^]'.


User Access Verification

Password: 
EIGRP-R1>en
Password: 
EIGRP-R1#

■mate-terminalの引数を確認

$ echo -e "`seq 21 23`\n`seq 31 34`" | \
    awk 'BEGIN{print "mate-terminal \\"}{print "  --tab -e \042 ./mytel 172.31.31.92 "$0"23 \042 \\"}END{print ""}'
mate-terminal \
  --tab -e " ./mytel 172.31.31.92 2123 " \
  --tab -e " ./mytel 172.31.31.92 2223 " \
  --tab -e " ./mytel 172.31.31.92 2323 " \
  --tab -e " ./mytel 172.31.31.92 3123 " \
  --tab -e " ./mytel 172.31.31.92 3223 " \
  --tab -e " ./mytel 172.31.31.92 3323 " \
  --tab -e " ./mytel 172.31.31.92 3423 " \

■mate-terminalのタブでログイン出来た。

$ echo -e "`seq 21 23`\n`seq 31 34`" | \
    awk 'BEGIN{print "mate-terminal \\"}{print "  --tab -e \042 ./mytel 172.31.31.92 "$0"23 \042 \\"}END{print ""}' | sh

■mate-terminalのタブでログインして、タブの名前を付ける。

$ echo -e "`seq 21 23`\n`seq 31 34`" | \
    awk 'BEGIN{print "mate-terminal \\"} \
              {print "  -t 192.168.0."$0" --tab -e \042 ./mytel 172.31.31.92 "$0"23 \042 \\"} \
         END{print ""}' | sh

■おまけ
 同時にひとつのコマンドを送って結果をコピペしたいという要件なら以下で十分かと。

$ cat mytel2 
#!/bin/bash

if [ $# -le 2 ];then
  echo "Usage: $0 RHOST RPORT [COMMAND]"
  exit 1
fi

RHOST=$1
RPORT=$2
shift;shift
CMD=$*

expect -c "
  set timeout 5
  spawn   /usr/bin/telnet $RHOST $RPORT
  expect \"Password:\"
  send   \"cisco\n\"
  expect \".*>\"
  send   \"en\n\"
  expect \"Password:\"
  send   \"cisco\n\"
  expect \".*#\"
  send   \"${CMD}\n\"
  expect \".*#\"
  interact
"

$ ./mytel2 172.31.31.92 2223 "show ip cef"
spawn /usr/bin/telnet 172.31.31.92 2223
Trying 172.31.31.92...
Connected to 172.31.31.92.
Escape character is '^]'.

User Access Verification

Password: 
EIGRP-R2>en
Password: 
EIGRP-R2#show ip cef
Prefix               Next Hop             Interface
0.0.0.0/0            no route
0.0.0.0/8            drop
0.0.0.0/32           receive              
127.0.0.0/8          drop
172.16.14.0/24       attached             Serial2/0
172.16.14.0/32       receive              Serial2/0
172.16.14.21/32      attached             Serial2/0
172.16.14.22/32      receive              Serial2/0
172.16.14.23/32      attached             Serial2/0
172.16.14.255/32     receive              Serial2/0
172.16.15.0/24       attached             Ethernet0/0
172.16.15.0/32       receive              Ethernet0/0
172.16.15.22/32      receive              Ethernet0/0
172.16.15.23/32      attached             Ethernet0/0
172.16.15.255/32     receive              Ethernet0/0
192.168.0.0/24       attached             Ethernet0/2
192.168.0.0/32       receive              Ethernet0/2
192.168.0.5/32       attached             Ethernet0/2
192.168.0.22/32      receive              Ethernet0/2
192.168.0.255/32     receive              Ethernet0/2
224.0.0.0/4          drop
224.0.0.0/24         receive              
240.0.0.0/4          drop
255.255.255.255/32   receive              

■以下でやろうとしたけど、xtermからのコピペが面倒だったり、
 初期設定が面倒だったので今回は必要無いと判断した。

■clustersshで複数のtelnetセッションを開く
 clustersshとmusshがマッチする。
 そのうち、ctelが使えるclustersshを導入する。

$ apt-cache search clusterssh
clusterssh - administer multiple ssh or rsh shells simultaneously
mussh - MUltihost SSH Wrapper

$ echo bin/{clusterssh,mussh} | tr ' ' '\n' | awk '{print "apt-file search "$0}' | sh
clusterssh: /usr/bin/clusterssh
mussh: /usr/bin/mussh

$ apt-file search bin/c | grep clusterssh
clusterssh: /usr/bin/ccon
clusterssh: /usr/bin/clusterssh
clusterssh: /usr/bin/crsh
clusterssh: /usr/bin/csftp
clusterssh: /usr/bin/cssh
clusterssh: /usr/bin/ctel

$ apt-file search bin/m | grep mussh
mussh: /usr/bin/mussh

$ sudo apt-get install -y clusterssh

■ctelで接続
 一度に7台のルータ、スイッチにコマンドを送ることができる。

$ echo -e "`seq 21 23`\n`seq 31 34`" | \
    awk '{printf "172.31.31.92:"$0"23 "}' | xargs ctel 
Opening to: 172.31.31.92:2123 172.31.31.92:2223 172.31.31.92:2323 172.31.31.92:3123 172.31.31.92:3223 172.31.31.92:3323 172.31.31.92:3423

■ctelもcsshもperlで書かれている。

$ perl -V | head -1
Summary of my perl5 (revision 5 version 24 subversion 1) configuration:

$ perl -MTk -e 'print $Tk::VERSION,$/'
804.033

$ perl -MX11::Protocol -e 'print $X11::Protocol::VERSION,$/'
0.56

$ grep -v "^#" /etc/csshrc $HOME/.clusterssh/config
grep: /etc/csshrc: そのようなファイルやディレクトリはありません

■xtermからmate-terminalに変更しようとしたところで、
 font関連につまづいて断念。
 xterm専用に書かれているっぽい。

 「引数を解析できませんでした: -bg は不明なオプションです」のエラー。

$ sed -i -e 's%#terminal=xterm%&\nterminal=mate-terminal%'  $HOME/.clusterssh/config 

$ man clusterssh | grep -B 1 "\-bg"
           terminal_colorize = 1
               If set to 1 (the default), then "-bg" and "-fg" arguments will be added to the terminal invocation command-line.

$ grep terminal_colorize $HOME/.clusterssh/config
#terminal_colorize=1

$ sed -i -e 's%#terminal_colorize=1%&\nterminal_colorize=0%'  $HOME/.clusterssh/config

 「引数を解析できませんでした: -xrm は不明なオプションです」のエラー。

$ man clusterssh | grep -A 2 \"\-xrm\"
           This will test the mechanisms used to open windows to hosts.  This could be due to either the "-xrm" terminal option
           which enables "AllowSendEvents" (some terminals do not require this option, other terminals have another method for
           enabling it - see your terminal documentation) or the configuration of "ssh".

$ grep xrm $HOME/.clusterssh/config
#terminal_allow_send_events=-xrm '*.VT100.allowSendEvents:true'

$ sed -i -e 's%#\(.*\)-xrm \(.*\)%&\n\1\2%'  $HOME/.clusterssh/config

 「引数を解析できませんでした: -T は不明なオプションです」

$ man mate-terminal | grep title
       -t, --title=TITLE
              Set the terminal title

$ grep "\-T" $HOME/.clusterssh/config
#terminal_title_opt=-T

$ sed -i -e 's%#\(terminal_title_opt=-\)T%&\n\1t%' $HOME/.clusterssh/config

 「引数を解析できませんでした: -font は不明なオプションです」

$ man clusterssh | grep -A 1 "\-font"
       --font '<font>', -f '<font>'
           Specify the font to use in the terminal windows. Use standard X font notation such as "5x8".

$ grep "font" $HOME/.clusterssh/config
#terminal_font=6x13

$ sed -i -e 's%#\(terminal_font=\)6x13%&\n\1%' $HOME/.clusterssh/config

$ echo -e "`seq 21 23`\n`seq 31 34`" |     awk 'BEGIN{printf "ctel "}{printf "172.31.31.92:"$0"23 "}END{print ""}' | sh
Fatal: Unrecognised font used ().
Please amend $HOME/.clusterssh/config with a valid font (see man page).

$ man clusterssh | grep -A 1 terminal_font
           terminal_font = 6x13
               Font to use in the terminal windows.  Use standard X font notation.

$ mate-terminal --help-terminal-options
用法:
  mate-terminal [オプション...]

端末のオプション (引数の先頭で --window や --tab を指定すると、すべてのタブやウィンドウに適用される):
  -e, --command                   端末内でこのオプションに対する引数を実行する
  --profile=PROFILE-NAME          指定したプロファイルを使用する
  -t, --title=TITLE               端末のタイトルを指定する
  --working-directory=DIRNAME     作業用ディレクトリを指定する
  --zoom=ZOOM                     端末の拡大率を指定する (1.0 = 標準サイズ)

MATE 端末エミュレーター