In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/03 Report--
Preface
The company develops and uses docker. Every time you log on to your own development machine, you will always enter ssh user_name@ip_string, and then confirm that you enter password. When you are fast, you will often make a mistake. As a lazy person, I must find a tricky way to check the ssh command, because it has to have an encrypted interaction with the server, so there is no option to log in with a password directly, so I have to give up.
A few days ago, when a colleague was sharing technology, I was surprised to see that he successfully logged in to the development machine after entering only one line of command. / test.sh, so I came back to search and study, so I wrote this article.
Shell script Foundation
Before writing the ssh automatic login script, let's talk about the basis of the shell script, which is not some syntax or anything, it is everywhere on the Internet. Here is a summary of the running mechanism of the shell script.
The way shell scripts run
First of all, I would like to talk about several startup methods of shell. It is precisely the pit in which the script starts, and it takes two hours to use the script that was completed in ten minutes. It also allows us to run shell and know why.
Execute by file name
The shell script can be executed directly through the file name, but it is important to note that the file requires execution permissions. Add execution permissions to the file through sudo chmod + x. / file_name.sh
Specify a script interpreter to execute the file
Our commonly used sh file_name.sh is to specify a script interpreter / bin/sh to interpret and execute scripts; common script interpreters include: / bin/bash, etc. We can use the ls-l / bin/*sh command to view the currently available script interpreters
Use。 . / file_name or source command execution script
Instead of fork a child process to execute the script as in the previous two ways, it is executed using the current shell environment for when .bashrc or .bash _ profile is modified, we don't have to restart shell or log back into the system to make the current changes take effect.
Shebang
When we write a shell script, we always add a line #! / binbash at the beginning, which is the shebang of the script. As for why it is called such a strange name, Dennis Ritchie, the developer of C and Unix, called it a British descriptive text that might be similar to "hash-bang".
Post an explanation on wiki:
In computer science, a Shebang is a serial character consisting of a pound sign and an exclamation point that appears in the first two characters of the first line of a text file. In the case of Shebang in the file, the program loader of the Unix-like operating system analyzes the content after Shebang, takes it as an interpreter instruction, invokes the instruction, and takes the file path containing Shebang as the parameter of the interpreter.
To put it simply, it indicates the interpreter of the script when it runs, so when you execute the shell script directly with a file name, you must take shebang; with it. In addition, we can also append the option directly after the shebang, and we use the option to execute by default.
If the shebang of test.sh is #! / bin/sh-x, then when we execute the script:
. / test.sh hello
Equivalent to:
Bin/sh-x. / test.sh hello
To write an automatic login script for ssh, the shebang (interpreter) required is / usr/bin/expect.
It should be noted that when a script interpreter is specified to execute the script, the shebang is overridden by the specified script interpreter, that is, the specified script interpreter is preferred to execute the script (habitually using sh. / test.sh but prompting command not found)
Expect interpreter
Expect is an interpreter that implements automatic and interactive tasks. It can also interpret common shell syntax commands, which are characterized by the following commands:
Spawn command:
The spawn command command fork a child process to execute the command command, and then execute the following commands in that child process
In the ssh automatic login script, we use a child process of spawn ssh user_name@ip_str,fork to execute the ssh login command
Expect command:
The expect command is the key command of the expect interpreter. Its general usage is expect "string", that is, you expect to get the string string, and you can use wildcards such as * in the string string.
As soon as string matches the information returned from the command line, expect will immediately execute the script down
Set timeout command:
The set timeout n command sets the wait timeout of the expect command to n seconds, and the expected command has not been obtained within n seconds. If expect is false, the script will continue to execute downwards.
Send command:
The general usage of the send command is send "string". They enter a message to the command line as we usually enter the command. Of course, don't forget to add\ r to the string to indicate enter.
Interact command:
The interact command is simple. When the command is executed, the child process of the script fork will hand over the operation rights to the user, allowing the user to interact with the current shell.
Complete the script
The following is a completed version of the script test.sh:
#! / usr/bin/expect / / specify shebangset timeout 3 / / set the timeout to 3 seconds spawn ssh user_name@172.*** / / fork A child process executes the ssh command expect "* password*" / / expect to match to 'user_name@ip_string's password:' send " My_password\ r "/ enter the password to the command line and enter send" sudo-s\ r "send" cd / data/logs\ r "/ / help me switch to the commonly used working directory interact / / to allow users to interact with the command line
Execute the sudo chmod + x. / test.sh command to add execution permissions to the shell script
Run the. / test.sh command and log in successfully with one click!
After a few simple commands are matched to solve the problem of interacting with the command line, many complex functions are out of the question.
Alias alias
The script is complete, but there are still some minor flaws:
Entering the. / file_name.sh command is too long.
It can only be executed in the script directory, otherwise the command output using the absolute path will be longer.
Here we think of linux's alias command:
Alias command:
The alias command is used as alias alias_name= "ori_command". Set alias_name to the alias of ori_command, so that we enter execute alias_name, which is equivalent to executing ori_command.
However, we will find that when you close the current shell, 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, which will be executed first when the terminal window is created, so it can help us set the alias again
Execute the command vim ~. / bash_profile, and add:
Alias alias_name= "/ root_dir/../file_name.sh
Save it before using it. ~. / bash_profile or source ~. / bash_profile executes the set alias command once in the current script to complete the setting
In this way, no matter which directory we are in, just enter the alias_name command, enter, and log in with one click!
The above is a detailed explanation of shell to achieve SSH automatic login details, more please pay attention to other related articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.