Shell implementation SSH Automatic login

Source: Internet
Author: User
Tags shebang

Objective

Company development and use Docker, each landing their own development machine to input ssh [email protected]_string , and then confirm the input password , deft on hand also often will be wrong. As a lazy person, must find a trickery way, look at the next SSH command, because it to do once with the server encryption interaction, so there is no direct access to password login options, had to forget.

A few days ago in the technical sharing of colleagues, see him unexpectedly only entered a line of command ./test.sh on the successful landing of the development machine, it is amazing, so back to search for a bit, thus into this article.

Shell Script Basics

Before writing the SSH automatic landing script, say the foundation of Shell script, this foundation is not some grammar or anything, the web is everywhere, here summarizes the shell script running mechanism ~

How the shell script runs

First of all to say a few shell startup mode, it is stepping on the script to start the pit, only use the original 10 minutes to get the script, took two hours to fix. It also allows us to run the shell and know why.

Execute by file name

The shell script can be executed directly from the file name, and it is important to note that the files require execute permissions. by sudo chmod +x ./file_name.sh Adding execute permissions to the file;

Specify the script interpreter to execute the file

What we often use sh file_name.sh is to specify the script interpreter /bin/sh to interpret the execution script; The common script interpreter also has: and /bin/bash so on, we can ls -l /bin/*sh view the currently available script interpreter using the command;

Use. The./file_name or Source command executes the script

This approach does not fork a subprocess as in the previous two ways to execute the script, but instead uses the current shell environment to execute, for. BASHRC or. Bash_profile is modified, we do not have to restart the shell or log back in the system, we can make the current changes to take effect.

Shebang

When we write a shell script, we always get used to the #!/binbash first line, it is the script shebang , as for why it is called such a strange name, C language and UNIX developer Dennis Ritchie called it 可能是类似于"hash-bang"的英国风描述性文字 ;

Post a wiki explanation:

In computer science, Shebang is a string line consisting of a pound sign and an exclamation mark, which appears in the first two characters of a text file. In the case of shebang in a file, the program loader of the Unix-like operating system parses the contents of the Shebang, interprets the contents as an interpreter directive, invokes the instruction, and takes the file path containing the shebang as the parameter of the interpreter.

In a nutshell, it indicates the interpreter at runtime of this script, so you must bring the shebang with the file name to execute the shell script directly; In addition, we can add the option directly behind the shebang, which we use by default when executing;

As test.sh shebang for #!/bin/sh -x , when we execute the script:

./test.sh hello

Equivalent:

bin/sh -x ./test.sh hello;

and write an SSH automatic landing script, need to use the shebang (interpreter) for /usr/bin/expect ;

It is important to note that when the script interpreter is specified to execute the script, Shebang is overwritten by the specified script interpreter, which takes precedence over the specified script interpreter to execute the script (habitually using SH./test.sh but prompt command not found)

Expect interpreter

Expect is an interpreter that implements automatic and interactive tasks, and it can also explain common shell syntax commands, which are featured in the following commands:

Spawn command:

spawn commandThe command will fork a child process to execute command commands, and then execute the subsequent commands in this subprocess;

In the SSH automatic landing script, we use spawn ssh [email protected]_str , fork a child process to execute SSH login command;

Expect command:

The expect command is a key command of the expect interpreter, and its general usage is to expect "string" expect to get to string strings, which can be used in a string string, such as wildcard characters;

After the string matches the information returned by the command line, expect executes the script down immediately;

Set timeout command:

set timeout nThe command sets the wait timeout for expect commands to n seconds, has not acquired the desired command in n seconds, and expect is false, and the script continues to execute downward;

Send command:

The general use of the Send command is send "string" that they will enter a message to the command line as we normally would enter a command, and of course, do not forget to string add the following to indicate the \r input carriage;

Interact command:

The interact command is simple, and when executed to this command, the child process of the script fork gives the user the right to operate, allowing the user to interact with the current shell;

Complete the script

The following is a complete version of the script test.sh :

#!/usr/bin/expect                   // 指定shebangset timeout 3                       // 设定超时时间为3秒spawn ssh [email protected]***.***.*** // fork一个子进程执行ssh命令expect "*password*"                 // 期待匹配到 ‘[email protected]_string‘s password:‘ send "my_password\r"                // 向命令行输入密码并回车send "sudo -s\r" send "cd /data/logs\r"              // 帮我切换到常用的工作目录interact                            // 允许用户与命令行交互

Execute sudo chmod +x ./test.sh command to add Execute permission to shell script;

Run ./test.sh the command, a key login success!

A few simple commands, together with the command line to solve the problem of interaction, a lot of complex features is also a cinch ~

Alias aliases

The script is complete, but there are some minor flaws:

    • Input ./file_name.sh command too long ...
    • It can only be executed in the script directory, or the command output with absolute paths is longer.

Here we think of the Linux alias command:

Alias command:

The alias command is used to alias alias_name="ori_command" set the Alias_name to Ori_command alias, so that we enter the execution alias_name, which is equivalent to executing the ori_command;

However, we will find that when you close the current shell, then open a shell window, and then use Alias_name, the system prompts command not found ;

Is there a way to keep the order? Edit the Bash_profile file.

Bash_profile file

We edit the Bash_profile file, this file will be executed first in the terminal window when it is created, so we can set the alias again;

Execute the command vim ~./bash_profile to add inside the file:

alias alias_name="/root_dir/../file_name.sh

After saving, then use . ~./bash_profile or source ~./bash_profile in the current script to perform a set alias command, complete the setup;

In this way, we no matter in which directory, as long as the input alias_name command, enter, the real one-click Login!

Summarize

As a program ape, always keep 偷懒 consciousness (of course this lazy non-lazy ...) In Unix-like systems, do not waste shell this magical tool, let the computer serve us ~

One months did not write a blog, recently in the Reading APUE,UNP a set of books, C and Unix on the introduction is shallow, dare not write fraught, usually their own Notepad made notes are more scattered, not system

Slowly accumulate, there are appropriate projects will be written, welcome everyone to pay attention to ~

Transferred from: http://www.cnblogs.com/zhenbianshu/p/5867440.html

Shell implementation SSH Automatic login

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.