Home >Operation and Maintenance >Linux Operation and Maintenance >Detailed explanation of shell to implement SSH automatic login

Detailed explanation of shell to implement SSH automatic login

藏色散人
藏色散人forward
2020-01-25 16:44:193755browse

Detailed explanation of shell to implement SSH automatic login

Preface

The company uses docker for development. Every time you log in to your development machine, you must Enter ssh user_name@ip_string, and then confirm the entry of password. If you are fast, you will often make mistakes. As a lazy person, I must find a trick. I checked the ssh command. Since it requires an encrypted interaction with the server, there is no option to directly log in with a password, so I had to give up.

Recommendation: "Linux Tutorial"

When a colleague was sharing technology a few days ago, I saw that he only entered a line of command ./test. sh successfully logged into the development machine. I was very surprised, so I came back to search and research, and finally wrote this article.

Basics of shell scripts

Before writing the ssh automatic login script, let me first talk about the basics of shell scripts. This basics is not some grammar or something, it is everywhere on the Internet. Here is a summary of the operating mechanism of shell scripts~

How to run shell scripts

First of all, let me talk about several startup methods of shell, which is to start by stepping on the script. I just used the script that originally took ten minutes to complete, but it took two hours to complete. At the same time, it also allows us to run the shell and know why.

Execute through the file name

Shell scripts can be executed directly through the file name. It should be noted that the file requires execution permission. Add execution permissions to the file through sudo chmod x ./file_name.sh ;

Specify the script interpreter to execute the file

We commonly use sh file_name.sh specifies the script interpreter /bin/sh to interpret and execute the script; common script interpreters include: /bin/bash Wait, we can use the ls -l /bin/*sh command to view the currently available script interpreters;

Use the ../file_name or source command to execute the script

This method does not fork a child process to execute the script like the first two methods, but uses the current shell environment to execute. It is used when .bashrc or .bash_profile is modified, we do not have to restart the shell. Or log in to the system again to make the current changes take effect.

shebang

When we write a shell script, we always add a line #!/binbash at the front, which is the shebang of the script. As for why it is called so It's a strange name. Dennis Ritchie, the developer of C language and Unix, said it may be a British-style descriptive text similar to "hash-bang";

Post an explanation on wiki:

In computer science, a shebang is a string line consisting of a pound sign and an exclamation mark, which appear as the first two characters of the first line of a text file. When a Shebang exists in the file, the program loader of the Unix-like operating system will analyze the contents after the Shebang, use these contents as interpreter instructions, call the instruction, and use the file path containing the Shebang as the interpreter parameters.

Simply put, it indicates the interpreter when this script is run. Therefore, when using the file name to directly execute the shell script, the shebang must be included; in addition, we can also append directly after the shebang option, we use the option by default when executing;

For example, the shebang of test.sh is #!/bin/sh -x, then when we execute the script:

./test.sh hello

is equivalent to:

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

To write an ssh automatic login script, the shebang (interpreter) needed is /usr/bin/expect;

It should be noted that when specifying the script interpreter to execute the script , the shebang will be overwritten by the specified script interpreter, that is, the specified script interpreter will be used first to execute the script (I habitually use sh ./test.sh but prompt command not found)

expect explanation

expect is an interpreter that can realize automatic and interactive tasks. It can also interpret common shell syntax commands. Its features are the following commands:

spawn command:

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

In the ssh automatic login script, We use spawn ssh user_name@ip_str, fork a child process to execute the ssh login command;

expect command:

expect command is the key command of the expect interpreter, and its The general usage is expect "string", that is, if you expect to get a string string, you can use wildcards such as * in the string string;

After string matches the information returned by the command line, expect will be executed immediately. Script;

set timeout command:

set timeout n command sets the wait timeout of the expect command to n seconds, and the expectation has not been obtained within n seconds. command, if expect is false, the script will continue to execute;

send command:

The general usage of the send command is send "string", they will be our usual Enter a piece of information into the command line just like entering a command. Of course, don’t forget to add \r after the string to indicate Enter;

interact command:

interact命令很简单,执行到此命令时,脚本fork的子进程会将操作权交给用户,允许用户与当前shell进行交互;

完成脚本

以下是一个完成版的脚本 test.sh:

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

执行 sudo chmod +x ./test.sh命令给shell脚本添加执行权限;

运行 ./test.sh命令,一键登陆成功!

简单的几个命令,,搭配起来解决了与命令行的交互问题后,很多复杂的功能也不在话下了~

alias别名

脚本完成了,可是还是有些小瑕疵:

输入./file_name.sh命令太长。。。

只能在脚本目录中才能执行,不然使用绝对路径输出的命令更长。

这里我们想到了linux的alias命令:

alias命令:

alias命令使用方式为 alias alias_name="ori_command",将alias_name设置为ori_command的别名,这样我们输入执行alias_name,就相当于执行了ori_command;

可是,我们会发现,当你关闭当前shell后,再打开一个shell窗口,再使用alias_name,系统提示command not found;

有没有能保持命令的方式呢?编辑bash_profile文件。

bash_profile文件

我们编辑bash_profile文件,此文件会在终端窗口创建的时候首先执行一次,所以可以帮我们再设置一次别名;

执行命令vim ~./bash_profile,在文件内部添加:

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

保存后,再使用 . ~./bash_profilesource ~./bash_profile 在当前脚本执行一遍设置别名命令,完成设置;

这样,我们无论在哪个目录,只要输入alias_name命令,回车,真正的一键登陆!

The above is the detailed content of Detailed explanation of shell to implement SSH automatic login. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:cnblogs.com. If there is any infringement, please contact admin@php.cn delete