The Linux script implementation automatically enters the password

Source: Internet
Author: User
Tags ftp connection install homebrew scp command

The use of Linux programmers to enter the password this move must not be unfamiliar, in Linux under the user has strict restrictions on permissions, do a lot of things over the permission to enter the password, such as the use of Super User command, such as FTP, SSH connection remote host, etc., such as

So the question is, what should I do if I need to enter a password when script automation executes? For example, there is an SCP statement in your script, you can't always enter the password manually when the script executes to this sentence.

For SSH or SCP command, some people may answer is to establish a trust relationship, about the method of establishing the SSH trust relationship, please Baidu Google, only two lines of simple commands can be done, but this is not a regular solution, if it is the FTP connection can not, moreover, You will not be able to execute certain commands to each of the hosts you want to connect to manually establish SSH trust, which has deviated from the original intention of today's theme, today is to say in the script to automatically enter the password, we can imagine that the more elegant way should be in the script itself to configure the password, when the screen interaction needs to be entered automatically, To achieve this effect, you need to use expect.

Installation

The installation command under CentOS is simple, as follows

sudo yum install expect

As for Mac users, you can install via homebrew (you need to install homebrew First, please google yourself)

Brew Install expect

Test scripts

We write a simple script to implement 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 "$ARGV 0 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-        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 important, usually the first line in our script is #!/bin/bash , and here is the path to the expect program on your machine, stating that the script is interpreted by expect and not executed by bash, so the syntax of the code is not the same as the shell script, where set passwd your_passwdset your own password and execute the following command

./scp.exp./local_file [Email protected]:/xx/yy/

Before executing ensure that Scp.exp has execute permissions, the first parameter is your local file, the second is the directory of the remote host, run the script if the error "Connect is timeout", you can set the time-out to a little longer, the second line set timeout 20 can setup time-out, in seconds. Script execution effect is as follows

What else can I do?

Careful classmate must find, in fact, expect provide is and terminal of an interactive mechanism, input password is only one application form, as long as the terminal block need input, can be completed by expect script automatic input, such as the previous script configured two interactive scenes, one is terminal prompt " Password: "When I enter a password, is there a hint" yes/no "?" When you enter "Yes", if and the remote host is the first time to establish a connection, execute the Scp.exp script effect is like this

So we can configure the input command according to the terminal's prompt, so we can achieve the result of automation. As for dealing with other interactive scenarios, just follow the above script and draw the gourd.

The Linux script implementation automatically enters the password

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.