cat 从 /etc/hosts 到对话框的特定行范围

cat 从 /etc/hosts 到对话框的特定行范围

例如,我喜欢列出 /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

当编写脚本--menudialog,菜单中的第一列是标签,您可以选择是否显示。当菜单确定时,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

dialog您可以通过将其放入$(和来将其重定向到命令行中)

通过将快捷键 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

相关内容