Shell Automatic Login Remote terminal

Source: Internet
Author: User

first look at the effect

  What you need to do, configure the server information in the configuration file , Select the corresponding server , and make the connection.

Traditional Manual Connection
SSH [email protected]   # then enter server password # key login ssh -i identity_file  [email protected] #将本机 id_ Rsa.pub Add to Server Authorized_keys sshssh [email protected]

? The drawback is obvious, need to remember the server address, IP, key location, password, and so on, each time you need to play a long series of commands Ah ...

Automatic connection

? The idea is simple to extract the parameters of the connection required to the configuration file, read the specified line server information from the read configuration file to connect, the following is the configuration file

Server.conf

ID  Desc            username   ip              connect-type    key-path/password1)  test_server     Ec2-user   123. xx.xx.xx    key             key_abs_path2)  Prod_server     Root       121. xxx.x.x8    Password        dasdxxxxx

? In addition to the first line, each row represents a server connection information , the profile information is relatively simple, contains only the necessary information and simple instructions for the connection.

Process Description
    • Print server configuration file information

    • User input ID that needs to connect to the server

    • Reads the information of the user select row into the array, which makes up the corresponding shell execution

      where password input is required, automatic interaction is done by expect .

      What is expect?

      ? A gadget that can help users interact with the terminal (you can understand it, it's easy, it won't take 10 minutes)

      ? For example, the connection server needs to enter a password, you write good expect script, let it in the specified time, to help you enter your password, the following is the script content

expect_login.sh

#!/usr/bin/expect-F # @author: wangjia# @time: ./Geneva/ A  One:Ten# desc:expect Login? set timeout3#接受传入参数, #参数1是 [email protected], #参数2 passwordset user_ip [lindex ${argv}0]set password [lindex ${argv}1]? #套壳 send the corresponding information according to expect SpawnSSH${user_ip}# the corresponding action according to the expectation, automatically enter the password expect {"*assword:*"{Send"${password}\r"}} #将操作权交还给用户interact?
Related Code

The first step is to read the configuration file and print to the terminal

# Read configuration file to array # @param1 server information Configuration tablefunctionInit_server_info_arr () {Conf_file_path=$1; LineCount=0;  whileRead Oneline DoLines[linecount]=${oneline} let LineCount++;  Done<${conf_file_path} print_server_list}? #服务器信息打印 (array printing)functionprint_server_list () { forIinch "${!lines[@]}";  Do        Echo "${lines[i]}"     Done}

The second step, user interaction

# User chooses the server to connect tofunctionInteract_user () {Read-P"which server to connect? Input the server ID:"user_choose Input_check"${user_choose}"}? #用户交互 input CheckfunctionInput_check () {input=$1    if[[${input} =~ ^[1-9]+$]] && [${input}-Lt ${linecount}] Theninput="Pass"    Else        Echo "Wrong Enter"Exitfi}

Step three, connect

#进行连接 # @param1 The lines of the configuration filefunctionConnect_by_line () {Choose_server=$1; #将行信息按空格分割成数组 Server_info_arr= (${choose_server// / })#读取数组, key-login by key, password call expect by passwordif["${server_info_arr[4]}"=="Key" ]     Then        SSH-I."${server_info_arr[5]}" "${server_info_arr[2]}@${server_info_arr[3]}"; elif["${server_info_arr[4]}"="Password" ]     ThenPw_login"${server_info_arr[2]}@${server_info_arr[3]}" "${server_info_arr[5]}"; Else        SSH "${server_info_arr[2]}@${server_info_arr[3]}"; fi}?# Account Password Login expect script call # @param1 user login name @ip eg. [Email protected]123.23.12.22# @param2 PasswordfunctionPw_login () {/users/wangjia/coder/github/shell/SSH/expect_login.SH$1$2}

Tail
    • There is nothing esoteric about this article, just a little bit early on to understand the shell, the daily operation of a small improvement and optimization, I believe there are many better ways to optimize, but also hope that this article for some friends to help.

    • This automatic login needs expect support, if you do not want to install expect, you can ecpect related comments out, you need to enter the password, the printed password copy into it.

#进行连接 # @param1 The lines of the configuration filefunctionConnect_by_line () {Choose_server=$1; #按空格分割成数组 Server_info_arr= (${choose_server// / })    if["${server_info_arr[4]}"=="Key" ]     Then        SSH-I."${server_info_arr[5]}" "${server_info_arr[2]}@${server_info_arr[3]}";#    elif["${server_info_arr[4]}"="Password" ]#     Then# Pw_login"${server_info_arr[2]}@${server_info_arr[3]}" "${server_info_arr[5]}"; Else        SSH "${server_info_arr[2]}@${server_info_arr[3]}"; fi} 

all source addresses in the text Github-shell Automatic login

Shell Automatic Login Remote terminal

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.