Shell script can automatically enter the password to log in to the server

Time:2022-5-24

Programmers using Linux must be familiar with the act of entering passwords. Under Linux, there are strict permission restrictions on users. If they do many things beyond the permission, they have to enter passwords, such as using super users to execute commands, and connecting to remote hosts through FTP and SSH, as shown in the figure below:

So the question is, what if you need to enter a password when the script is executed automatically? For example, there is an SCP statement in your script. You can’t manually enter the password when the script executes this sentence.

For SSH or SCP commands, some people may say that the answer is to establish a trust relationship. For the method of establishing an SSH trust relationship, please Baidu Google. It only needs two simple commands, but this is not a conventional solution. If it is an FTP connection, there will be no way. Moreover, you can’t manually establish an SSH trust for each host you want to connect in order to execute some commands, which has deviated from the original intention of today’s theme, What we want to say today is to automatically enter the password in the script. We can imagine that a more elegant way should be to configure the password in the script and automatically enter it when the screen interaction needs to be entered. Expect is needed to achieve this effect.

install
The installation command under CentOS is very simple, as follows

Copy codeThe code is as follows:
sudo yum install expect 

For Mac users, you can install homebrew through homebrew (you need to install homebrew first, please Google by yourself)

Copy codeThe code is as follows:
brew install expect 

Test script
We write a simple script to copy the SCP file, configure the password in the script and save it as SCP Exp is as follows

Copy codeThe code is as follows:
#!/usr/bin/expect 
set timeout 20 
 
if { [llength $argv] < 2} { 
    puts “Usage:” 
    puts “$argv0 local_file remote_path” 
    exit 1 

 
set local_file [lindex $argv 0] 
set remote_path [lindex $argv 1] 
set passwd your_passwd 
 
set passwderror 0 
 
spawn scp $local_file $remote_path 
 
expect { 
    “*assword:*” { 
        if { $passwderror == 1 } { 
        puts “passwd is error” 
        exit 2 
        } 
        set timeout 1000 
        set passwderror 1 
        send “$passwd\r” 
        exp_continue 
    } 
    “*es/no)?*” { 
        send “yes\r” 
        exp_continue 
    } 
    timeout { 
        puts “connect is timeout” 
        exit 3 
    } 

Note that the first line is very important. Usually, the first line in our script is #/ Bin / bash, and here is the path of the expect program on your machine. It shows that this script is interpreted and executed by expect, not bash. Therefore, the syntax of the code is also different from that of the shell script, including set passwd your_ Set passwd to your own password, and then execute the following command

Copy codeThe code is as follows:
./scp.exp ./local_file [email protected]:/xx/yy/ 

Ensure SCP before execution Exp has execution permission. The first parameter is your local file, and the second parameter is the directory of the remote host. If you run the script and report an error “connect is timeout”, you can set the timeout a little longer. In the second line, set timeout 20, you can set the timeout in seconds. The execution effect of the script is as follows

What else can I do

Careful students must have found that, in fact, expect provides an interaction mechanism with the terminal, and entering the password is only one of the application forms. As long as the terminal is blocked and needs to be entered, it can be automatically entered through the expect script. For example, two interaction scenarios are configured in the previous script, one is to enter the password when the terminal prompts “password:”, and the other is to prompt “yes / no?” If “p.yes” is the first time to establish a remote connection, enter “p.yes” The exp script works like this

Therefore, we can configure the input command according to the prompt of the terminal, so as to achieve the effect of automation. As for dealing with other interaction scenarios, just follow the script above

Recommended Today

Deform a picture into different lengths and widths

Step 1: add the following code to the < body > area<img src=” https://imgs.developpaper.com/imgs/helpor.gif “Name =” U “border =” 0 “ALT =” fun “><script language=”JavaScript”> var b = 1;var c = true; function fade(){if(document.all); if(c == true) {b++;}if(b==100) {b–;c = false} if(b==10) {b++;c = true;} if(c == false) {b–;}u.width=150 + b;u.height=125 – b;setTimeout(“fade()”,50);}</script> Step 2: […]