/etc/hostsからダイアログの特定の行範囲までcat

/etc/hostsからダイアログの特定の行範囲までcat

たとえば、私は/ etc / hostsのすべてのイーサネットスイッチを一覧表示し、ダイアログボックスに表示するのが好きです。その後、リストから選択したスイッチを接続したいと思います。

リストは特定のコメント行の間になければなりません。たとえば、次のようになります。

/etc/ホスト:

... 
# ETHERNET SWITCHES
192.168.0.2 SW1
192.168.0.3 SW2
192.168.0.4 SW3
# END SWITCHES
...

$HOST変数をリストされたスイッチ名に関連付けて/ etc / hostsのIPにsshを接続するにはどうすればよいですか?可能ですか?

会話スクリプト:

#!/bin/bash
DIALOG_CANCEL=1
DIALOG_ESC=255
HEIGHT=0
WIDTH=0
HOST=`cat /scripts/dialog.out`
IP=`grep '$HOST' /etc/hosts | awk '{print $1}'`

display_result() {
dialog --title "$1" \
--no-collapse \
--msgbox "$result" 0 0
}
while true; do
 exec 3>&1
 selection=$(dialog \
--backtitle "" \
--title "MENU" \
--clear \
--cancel-label "EXIT" \
--menu "SELECT OPTION:" $HEIGHT $WIDTH 6 \
"1" "SW1" \
"2" "SW2" \
"3" "SW3" \
2>&1>/scripts/dialog.out 1>&3)
exit_status=$?
exec 3>&-
case $exit_status in
$DIALOG_CANCEL)
  clear
  exit
  ;;
$DIALOG_ESC)
  clear
  echo "Program aborted." >&2
  exit 1
  ;;
esac
case $selection in
0 )
  clear
  echo "Program terminated."
  ;;
1 )
  ssh admin@$IP
  ;;
 esac
done

ベストアンサー1

スクリプトを書く--menuときdialog、メニューの最初の列は次のとおりです。商標、表示するかどうかを選択できます。メニューが決定されると、dialogこれらのラベルは出力に書き込まれます(通常は標準エラーですが、この--stdoutオプションは役に立ちます)。

IPアドレスをラベルに設定すると、実行から直接アドレスを取得できますdialog

内部に手動、このオプション機能は次のように説明されています。

   --no-items
          Some  widgets  (checklist, inputmenu, radiolist, menu) display a
          list with two columns (a "tag" and "item", i.e., "description").
          This  option  tells  dialog  to  read shorter rows, omitting the
          "item" part of the list.  This is occasionally useful, e.g.,  if
          the tags provide enough information.

          See  also --no-tags.  If both options are given, this one is ig-
          nored.

そして

   --no-tags
          Some  widgets  (checklist, inputmenu, radiolist, menu) display a
          list with two columns (a "tag" and "description").  The  tag  is
          useful  for scripting, but may not help the user.  The --no-tags
          option (from Xdialog) may be used to suppress the column of tags
          from  the  display.  Unlike the --no-items option, this does not
          affect the data which is read from the script.

          Xdialog does not  display  the  tag  column  for  the  analogous
          buildlist and treeview widgets; dialog does the same.

          Normally  dialog  allows  you  to quickly move to entries on the
          displayed list, by matching a  single  character  to  the  first
          character  of the tag.  When the --no-tags option is given, dia-
          log matches against the first character of the description.   In
          either case, the matchable character is highlighted.

/etc/hosts次のようなさまざまな方法でラインを選択できますsed

sed -e '1,/# ETHERNET SWITCHES/d' -e '/# END SWITCHES/,9999d' /etc/hosts

dialogandに入れて$(コマンドラインにリダイレクトできます)

ホットキー1、2、3をSW1、SW2、SW3と組み合わせて説明するこの--no-tagsオプションを使用すると、接続されたIPアドレスと説明を保持できます。

あなたが念頭に置いている例は次のとおりです。

#!/bin/bash
INPUT=/etc/hosts
let i=0 # define counting variable
W=() # define working array
while read -r addr line; do # process file by file
  let i=$i+1
  W+=($addr "$i $line")
done < <( sed -e '1,/# ETHERNET SWITCHES/d' -e '/# END SWITCHES/,9999d' $INPUT )
FILE=$(dialog --stdout --no-tags --title "List of Switches in /etc/hosts file" --menu "Chose one" 24 80 17 "${W[@]}" ) # show dialog and store output
clear
if [ $? -eq 0 ]; then # Exit with OK
  ssh $FILE
fi

おすすめ記事