Linux Implementation of Automatic Login to Remote Machines
Category Programming Techniques
When remotely logging into a machine on Linux, one often has to enter SSH with an account password, which is relatively cumbersome. In work, it is common to first log in to the company's intermediate machine (jump host) before being able to log in to the online machine, making each operation even more cumbersome. If a direct trust relationship can be established from A to B to avoid the need to enter a password each time. The current situation is that if there are three machines A, B, and C, and you want to go directly from A to C, you can only log in through B, which means you cannot establish a trust relationship from A to C (this operation is mainly to protect the online machine from being logged into casually). This script solves this kind of problem with multiple dependencies.
Precautions:
Use a real-time bash version >= 4.0, as associative arrays are needed in the configuration.
If you need to use it globally, just rename the autologin, and move it to the PATH path, for example: mv autologin /usr/local/bin/to (change to the name you want to use)
Script Code:
```
!/usr/local/bin/bash
@Version 0.3.1
@filename to
Fix the situation where you don't need to configure a jump host when executing commands. By default, fill in no for the position of the jump host configuration.
@Author [email protected]
Bash version >= 4.0 uses associative arrays
Usage: host user passwd port jump_host command
The four cases are as follows:
1. Directly log in to the target machine, such as A
2. Need an intermediate machine to log in to the target machine, such as C, where B is the intermediate machine. It will first log in to B and then log in to C from B and then execute the command
3. Directly log in to the target machine and execute the corresponding command, such as D
declare -A _server_config
_server_config['A']="a_host a_user a_passwd a_port" _server_config['B']="b_host b_user b_passwd b_port" _server_config['C']="c_host c_user c_passwd c_port B '(command eg) ls .'" _server_config['D']="d_host d_user d_passwd d_port no 'cd /home && ll'"
_config_keys=(${!_server_config[@]}) _length=${#_server_config[@]} _login_server=$1 _config_status=false
Whether to enter the login machine
if [ "$_login_server" == "" ];then echo -e "\033[40m\033[31m Please input login server, you can choose one from the following list \033[0m" for i in "${_config_keys[@]}";do echo -e "\033[41;37m $i \033[0m " done exit fi
Check if the login machine is configured
for i in "${_config_keys[@]}";do if [ "$_login_server" == "$i" ];then _config_status=true fi done
if [ "${_config_status}" == "false" ];then echo -ne "\033[40m\033[31m Not configured server info ... Please configure in _server_config like Host User Passwd Port Jump Command\033[0m" exit fi
Log in If a jump host is configured, first log in to the jump host and then log in to the target machine
_host=$(echo ${_server_config["${_login_server}"]} | awk '{print $1}') _user=$(echo ${_server_config["${_login_server}"]} | awk '{print $2}') _passwd=$(echo ${_server_config["${_login_server}"]} | awk '{print $3}') _port=$(echo ${_server_config["${_login_server}"]} | awk '{print $4}') _jump=$(echo ${_server_config["${_login_server}"]} | awk '{print $5}') _command=$(echo ${_server_config["${_login_server}"]} | awk -F"'" '{print $2}')
if [ "${_command}" != "" ]; then _command="expect \"*]*\" send \"${_command}\r\"" fi
if [ "${_jump}" != "" ] && [ "${_jump}" != "no" ]; then _jump_host=$(echo ${_server_config["${_jump}"]} | awk '{print $1}') _jump_user=$(echo ${_server_config["${_jump}"]} | awk '{print $2}')