So let’s look at the effect

Select group

Began to link

Why write

I wrote a link tool for the character interface before, but it looks very simple, it looks like this:

whiptail

A brief introduction to Whiptail

I’m only using whiptail’s menu bar and tooltip here, so I’ll stick to these two uses:

Whiptail --menu menu name Height width display how many [mark menu item]...Copy the code

Example:

Whiptail --menu this is a menu 15, 30, 2, 1 menu one, 2 menu twoCopy the code

Whiptail --msgbox Message width and heightCopy the code

Example:

Whiptail --msgbox This is message box 10, 20Copy the code

Write the SSH configuration file

The configuration file I wrote earlier is not very clear and is error prone to add. Now I use a JSON file to save multiple SSH configurations. It looks like this:

[{
    "name": "What for nothing?"."server": [{
        "name": "Tencent"."ip": "127.0.0.1"."user": "Username"."pwd": "Password"}}]]Copy the code

An array is used, each array represents a group, name is the group information, server is the SSH configuration of this group, can have multiple. Now that the configuration file is written, let’s see how to parse JSON in the shell

Parse JSON in a shell script

This is done using jQ, which is not jquery. Its website is here at stedolan.github. IO /jq/. I’m just going to give you a quick overview of how this works. I use the SSH configuration example I wrote above: 1: Fetch the first element in the array

Jq.[0] ssh.json Result: {"name": "What for nothing?"."server": [{"name": "Tencent"."ip": "127.0.0.1"."user": "Username"."pwd": "Password"}}]Copy the code

2: the value is based on the KEY of the JSON file

Jq.[0].name ssh.json result (this result is quoted)"What for nothing?"Jq-r.[0].name ssh.json Result: nothingCopy the code

Suppose you have a JSON file, test.json, which contains the following contents:

{
      "name": "Tencent"."ip": "127.0.0.1"."user": "Username"."pwd": "Password"
}
Copy the code

Perform:

Get all keys: jq keys test.json"ip"."name"."pwd"."user"] to obtain the length of the execution result: above jq keys test. The json | jq length results: 4Copy the code

Now that you have the basic methods, let’s start writing

The SSH group information is displayed based on the SSH configuration

Here we need to find all the group names in json and display them as a menu. 1: First we need to find the name property of each object in the JSON array like this:

    for i in $(seq `jq length $config_file`)
    do
        index=`expr $i - 1`
		#Print all the names
        echo `jq -r .[$index].name $config_file`
    done
Copy the code

2: Display all names as menus. Because of whiptail’s requirements, we will splice the name into “mark 1 menu, mark 1 menu, mark 2 menu 2… “Like this, so

menu="" for i in $(seq `jq length $config_file`) do index=`expr $i - 1` menu=$menu$index" "`jq -r .[$index].name $config_file '" "Done whiptail --title" welcome to SSH link tool "--menu" Please select link group: "20 50 8 $menuCopy the code

So now I can display the group information, but there’s a problem, I don’t know which item the user chose. So also need to get the menu selection results, and determine whether to select the cancel button. This writing

Choose =$(whiptail --title "please use SSH link tool" --menu "please select link group:" 20 50 8 $menu 3>&1 1>&2 2>&3) exitStatus =$? If [$exitStatus = 0] then #		#It is mainly to find the corresponding JSON data according to the selected subscript, and then display the SSH information inside with a menu item
		#The user selects the IP user name and password again, and finally links SSH
		echo $choose
    else
        echo 'exit'
    fi
Copy the code

Exitstatus: indicates whether the user has selected the cancel button. If the value of the cancel button is not equal to 0, it indicates whether the user has selected the cancel button. 3: This step is almost the same as the previous step

And I’m actually done.

Finally, post all the code

#! /bin/bash
error_code=9999
#Here is the address of the configuration file
config_file='/home/hjx/ssh.json'

#Links to SSHSSH (){ssh_ip_user=${1} password=${2} command=${3} whiptail --title --msgbox $ssh_ip_user" 10 50 clear expect -c " set timeout 300 spawn ssh -p22 ${ssh_ip_user} ${command} expect { \"yes/no\" { send \"yes\r\"; exp_continue } \"password\" { send \"${password}\r\" } } interact " }
#Displays the servers in the groupshow_server(){ key=`jq -r .[$1].name $config_file` echo "choose key : $key" length=`jq .[$1].server $config_file | jq length` echo "length $length" menu="" for i in $(seq $length) do index=`expr $i - 1` menu=$menu$index" "`jq -r .[$1].server[$index].name $config_file`"-"`jq -r .[$1].server[$index].ip $config_file '" "Done choose=$(whiptail --title" $key" --menu" " 20 50 8 $menu 3>&1 1>&2 2>&3) exitstatus=$? if [ $exitstatus = 0 ] then ssh `jq -r .[$1].server[$choose].user $config_file`"@"`jq -r .[$1].server[$choose].ip $config_file` `jq -r .[$1].server[$choose].pwd $config_file` else index fi }

#Open menu navigationindex(){ menu="" for i in $(seq `jq length $config_file`) do index=`expr $i - 1` menu=$menu$index" "`jq -r .[$index]. Name $config_file '" "Done choose=$(whiptail --title" --menu" " 20 50 8 $menu 3>&1 1>&2 2>&3) exitstatus=$? if [ $exitstatus = 0 ] then show_server $choose else echo 'exit' fi } indexCopy the code

After the

Original address: http://www.hebaibai.com/?p=315