Linux scripts automatically enter passwords

  

Programmers using Linux are no strangers to the idea of ​​entering passwords. Under Linux, users have strict permission restrictions. After doing a lot of things, you have to enter passwords, such as using superusers. Execute the command, such as ftp, ssh to connect to the remote host, etc., as shown below


Then the problem comes, what should I do when I need to enter the password when the script is executed automatically? For example, if you have a 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 answer that it is to establish a trust relationship, about establishing a ssh trust relationship. The method of Baidu Google, you only need two lines of simple commands to get it, but this is not a regular solution, if it is ftp connection, there is nothing wrong, and you can not give each of you to execute certain commands. The host to connect to manually establish ssh trust, which has deviated from the original intention of today's theme, today is to automatically enter the password in the script, we can imagine that the more elegant way should be to configure the password in the script, when Screen interaction needs to be input automatically when inputting. To achieve such an effect, you need to use expect

Installation

The installation command under CentOS is very simple, as follows

sudo yum install expect 

As for Mac users, you can install them through homebrew (you need to install homebrew first, please Google yourself)

brew install expect

Test script

We write a simple foot This implementation of the scp copy file, configure the password in the script, save as scp.exp as follows

#!/usr/bin/expectset timeout 20if { [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_passwdset passwderror 0spawn scp $local_file $remote_pathexpect { "*assword:*" { if { $passwderror == 1 } { puts "passwd is error" exit 2 } set timeout 1000 set passwderror 1 send "$passwd\
" exp_continue } "*es/no)?*" { send "yes \
" exp_continue } timeout { puts "connect is timeout" exit 3 }}

Note that the first line is important, usually the first line in our script is #!/bin/bash, and here is your machine The path of the expect program, indicating that this script is interpreted by expect, not by bash, so the code The method is different from the shell script, where set passwd your_passwd is set to your own password, and then execute the following command

./scp.exp ./local_file user@host:/xx/yy/

Make sure that scp.exp has execute permission before execution. The first parameter is your local file, and the second is the directory of the remote host. If you run the script and report error "connect is timeout", you can set the timeout to a little longer. Timeout 20 can set the timeout in seconds. The script execution effect is as follows


What else can you do?

Careful classmates must find out, in fact, expect provides an interaction mechanism with the terminal. Entering the password is only one of them. An application form, as long as the terminal is blocked and needs to be input, the automatic input can be completed through the expect script. For example, two interactive scenarios are configured in the previous script, and one is a password prompt when the terminal prompts "password:" There is a prompt "yes/no)?" when you enter “yes”, if the connection with the remote host is the first time, the execution of the scp.exp script is like this


So we can configure the input commands according to the prompts of the terminal, so that the automation effect can be achieved. As for dealing with other interactive scenes, just follow the above script and follow the gourd painting scoop.

Copyright © Windows knowledge All Rights Reserved