mac terminal transformation

Time:2022-11-25

background

In windows, there are things likeXShellSecureCRTWait for the ssh connection artifact, but in Mac, there are not many tools to choose from.ITerm2I was blown to the sky by a group of programmers, but it was not easy to use in practice. The obsession with the terminal made me basically try all the terminal tools that I could find on the Mac, and finally chosetermius, the tool interface is a bit geek style, the design is simple, and the connection speed is fast. Unfortunately, if the user quits, all records will be cleared. After being cleared twice, I finally gave up the only terminal tool under Mac. After giving up termius But it is difficult to find a satisfactory terminal. The terminal that comes with the mac is actually okay, but it is not easy to use. The ideal terminal should meet the following functions

  • Able to send heartbeat packets, so you don’t need to reconnect due to network disconnection or long idle time
  • It is also the most important requirement to be able to have certain management functions, remember the password, and not need to enter the address and account every time.
  • Support tab page, support split screen
  • Ability to upload and download files
  • good looks
  • You can choose to copy, because you often need to copy in the terminal, most terminals can be selected to copy, no need to manually ctrl+c

If the built-in terminal of mac can meet the above requirements, it can be used for daily development, then we will transform the terminal step by step

heartbeat detection

Anyone who has used the ssh terminal knows that if the network is disconnected or does not operate for a long time, the terminal will be disconnected, which means that the keyboard does not respond and needs to be reconnected. Sometimes we need to switch to the current server when we log in again in the working directory of the server. Directory, very inconvenient, so the general terminal can configure a heartbeat detection, the terminal will send heartbeat packets regularly to maintain the connection, Mac terminal also provides this configuration, edit the file/etc/ssh/ssh_configAdd the following configuration

$ sudo vi /etc/ssh/ssh_config

ServerAliveInterval 60
ServerAliveCountMax 999
  • ServerAliveInterval: interval for sending heartbeat packets, in seconds
  • ServerAliveCountMax: the maximum number of failed sending times before disconnecting

in addition/etc/ssh/ssh_configfile hasSendEnv LANG LC_*Configuration, set the server language environment to the same environment as the local environment. Generally, our local machine is in the Chinese environment, but not all Linux servers support the Chinese environment. Sometimes garbled characters may appear, so it is recommended to comment out the configuration here.

Appearance

Appearance really depends on the beholder, mac terminal provides the function of custom theme, in the menuTerminal -> Preferencesmiddle

mac terminal transformation

Alternatively you can edit the file/etc/motdfile, customize the terminal welcome page

mac terminal transformation

You can also install third-party shells, such asOh My Zsh

management function

This requirement is a hard requirement. It is impossible to re-enter the address and account information every time you connect. There must be a plan to record the user name and password, and it can be automatically connected through scripts. The ssh tool itself is not provided in the command line for security reasons. Specifies the password function, that is, you cannot pass something likessh {password} {user}@{host}If you want to connect in the same way, you must manually enter the password, so the first step is to solve the automatic login problem.

automatic log-in

sshpassIt is to solve the problem of manually entering the password for ssh, you can install sshpass through brew

brew install https://raw.githubusercontent.com/kadwanev/bigboybrew/master/Library/Formula/sshpass.rb

If brew is not installed, you can downloadsource codeCompile and install by yourself

After installing sshpass, you can log in to the server with the following command

# login ssh

sshpass -p "password" ssh [email protected]

# login sftp

sshpass -p "password" sftp [email protected]

manage

To solve the problem of automatic login, then there must be a place to manage connections. In fact, it is to move the interface operations of other terminal tools to the console. We assume that there is a tool calledPowerTerminalReferred to as pw, pw provides the following functions

# Create a new connection

pw create $connection name $ip $username $password

# list all connections

pw

# Search for connections based on keywords

pw $keyword

# After displaying all connections, you can choose to connect a connection according to the number

# sftp

pw sftp

interface effect

create connection

$ pw create
➜ Group: huaweiyun
➜ ip    : 192.168.10.100
➜ Name: oam
➜ Login name: oam
➜ Password: oam
Add host 192.168.10.100 successfully

server list

$ pw

server list
1.  aliyun       ➡  192.168.1.100(hr)
2.  aliyun       ➡  192.168.1.101(bpm)
3.  huaweiyun    ➡  192.168.10.100(oam)
4.  huaweiyun    ➡  192.168.10.101(soa)

➜ Enter number:

keyword search

$ pw huawei
server list
1.  huaweiyun    ➡  192.168.10.100(oam)
2.  huaweiyun    ➡  192.168.10.101(soa)

➜ Enter number:

After entering the previous number, it will automatically connect. I hope to open a new tab page instead of the current page. Just execute the following command in the script to open a new tab page in the terminal and execute the command on the new tab page

osascript -e 'tell application "Terminal" to activate' -e 'tell application "System Events" to tell process "Terminal" to keystroke "t" using command down' -e 'tell application "Terminal" to do script "'"$v_script"'" in selected tab of the front window'

$v_scriptIt is the command to be executed. Here, the script function that comes with the Mac is used, so it cannot be cross-platform. If you open the connection on the current tab page, you can directly execute the sshpass command. Here also need to open script accessibility permissions, find Security and Privacy in System Preferences, add script editor and terminal to Accessibility

mac terminal transformation

Full script pw.sh

#!/bin/bash

v_pw_home=~/.pw
v_pw_data=$v_pw_data/data

function help(){
    echo "usage : pw [create|sftp|{searchKey}]"
    echo "create Create a connection according to the command prompt"
    echo "sftp enters sftp mode"
    echo " searchKey Fuzzy search based on keywords Links support search by group name, connection name, ip"
    echo "help help information"
}

function init(){
    if [ "$PW_HOME" != "" ]; then
        v_pw_home=$PW_HOME
    fi
    v_pw_data=$v_pw_home/data
    mkdir -p $v_pw_data
}

function login(){
    v_login_host=$1
    v_login_name=$2
    v_login_pwd=$3
    v_action=$4
    printf "\e[0mlogin to ➡ %s with user %s ....\n" $v_login_host $v_login_name
    if [ "$v_login_pwd" != "nil" ]; then
        v_script="sshpass -p '$v_login_pwd' $v_action ${v_login_name}@${v_login_host}"
    else
        v_script="sshpass $v_action ${v_login_name}@${v_login_host}"
    fi
    osascript -e 'tell application "Terminal" to activate' -e 'tell application "System Events" to tell process "Terminal" to keystroke "t" using command down' -e 'tell application "Terminal" to do script "'"$v_script"'" in selected tab of the front window'
}

init

cmd=$1


if [ "${cmd}" = "help" ]; then
    help
    exit 0
fi

v_action=ssh
v_spec_number="-1"
if [ "${cmd}" == "create" ]; then
    read -p "➜ group: " v_group
    read -p "➜ ip    : " v_host
    read -p "➜ name: " v_name
    read -p "➜ login name: " v_username
    read -p "➜ Password: " v_password
    if [ "$v_host" == "" ]; then
        echo "ip address cannot be empty"
        exit 1
    fi
    if [ "$v_name" == "" ]; then
        echo "Name cannot be empty"
        exit 1
    fi
    if [ "$v_group" == "" ]; then
        v_group="default"
    fi

    if [ "$v_password" == "" ]; then
        v_password="nil"
    fi
    
    echo ${v_host},${v_username},${v_password} >$v_pw_data/${v_group}#$v_host#${v_name}.kimy
    echo "Add host "${v_host}" successfully"
    exit 0
fi
if [ "${cmd}" == "sftp" ]; then
    v_action=sftp
    cmd=$2
fi

if [ "${cmd}" == "delete" ]; then
    v_action=delete
    cmd=$2
fi

v_search_key="*.kimy"
v_machine_id=-1

if [ "$cmd" != "" ]; then
    if [ -n "$cmd" ] && [ "$cmd" -eq "$cmd" ] 2>/dev/null; then
        v_machine_id=$cmd
    else
        v_search_key="*${cmd}*.kimy"
    fi
fi

if [ "$v_machine_id" == "-1" ]; then
    v_sequence=1
    printf "Server List\n"
    for f in $(find $v_pw_data -name ${v_search_key}|sort)
    do
        v_file_name=$(basename ${f})
        v_names=(${v_file_name//#/ })
        v_group=''
        v_host=''
        v_nick=''
        v_index=1
        for i in ${v_names[@]}  
        do
            if [ "$v_index" == "1" ]; then
                v_group=$i
            fi 
            if [ "$v_index" == "2" ]; then
                v_host=$i
            fi
            if [ "$v_index" == "3" ]; then
                v_nick=(${i//.kimy/})
            fi
            v_index=`expr $v_index + 1`
        done
        printf "\e[0m%-4s" $v_sequence"."
        printf "\e[32m%-12s" $v_group
        printf "\e[0m ➡  \e[35m%s(%s)\n\e[0m" $v_host $v_nick
        v_sequence=`expr $v_sequence + 1`
    done
    
    if [ "$v_sequence" != "2" ]; then
        printf "\n"
    
        read -p "➜ Enter ID: " v_machine_id
    else
        v_machine_id="1"
    fi
fi

v_sequence=1
for f in $(find $v_pw_data -name $v_search_key|sort)
do
    if [ "$v_machine_id" == "$v_sequence" ]; then
        v_content=`cat ${f}`
        v_sps=(${v_content//,/ })
        v_host=''
        v_name=''
        v_password=''
        v_index=1
        for i in ${v_sps[@]}  
        do
            if [ "$v_index" == "1" ]; then
                v_host=$i
            fi 
            if [ "$v_index" == "2" ]; then
                v_name=$i
            fi
            if [ "$v_index" == "3" ]; then
                v_password=$i
            fi
            v_index=`expr $v_index + 1`
        done

        if [ "$v_action" == "delete" ];then
            printf "delete file %s\n" $f
            rm -rf "$f"
        else
            login $v_host $v_name $v_password $v_action
        fi
    fi
    v_sequence=`expr $v_sequence + 1`
done

script installation

Save the script locally, such as/you/path/pwoerterminal/pw.sh,exist~/.bash_profileadd the following


alias pw='/you/path/pwoerterminal/pw.sh'
alias pwf='/you/path/pwoerterminal/pw.sh sftp'
PW_HOME=/you/path/pwoerterminal/data
export PW_HOME
  • pw defines the pw command for easy calling, so you can execute pw into the script anywhere
  • pwf defines the sftp command for easy calling, so you can enter pwf to enter sftp mode
  • PW_HOME: Where to save data

Execute after modification~/.bash_profilemake it effective

Effect

mac terminal transformation

other problems

  • Selecting the copy function is currently unavailable. It seems that you need to use a plug-in, so there is no need to toss here
  • The password here is saved in plain text, which has certain security issues.