Shell script Implementation automatic input password login server _linux shell

Source: Internet
Author: User
Tags ftp connection install homebrew ssh

Use Linux Programmer to enter password This behavior must be not unfamiliar, under Linux to the user has the strict permission limit, does many things crosses the permission to have to enter the password, for instance uses the super user to execute the command, also like FTP, SSH connection remote host and so on, the following figure:

So the question is, what if you need to enter a password when the script automates execution? For example, you have an SCP in your script, and you can't manually enter a password when the script executes to this sentence.

For SSH or SCP commands, some people may answer is to establish a trust relationship, the way to establish the SSH trust relationship please own Baidu Google, only need two lines of simple command can be done, but this is not a conventional solution, if it is an FTP connection, and, moreover, You cannot manually establish SSH trusts for each host you want to connect to by executing certain commands, this has deviated from today's theme, and today is about typing the password automatically in the script, and we can imagine that the more elegant way would be to configure the password in the script and enter it automatically when the screen interaction needs to be entered. To achieve this effect, you need to use expect.

Installation
the installation command under CentOS is simple, as follows

Copy Code code as follows:

sudo yum install expect

As for Mac users, you can install via homebrew (you need to install homebrew First, please google yourself)
Copy Code code as follows:

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

Copy Code code as follows:

#!/usr/bin/expect
Set Timeout 20

if {[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_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 important, usually the first line in our script is #!/bin/bash, and here is the path to the expect program on your machine, which explains that the script is interpreted by expect and not executed by bash, so the syntax of the code and the shell script are different. Where the set passwd your_passwd is set to your own password, and then execute the following command

Copy Code code as follows:

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

Before execution, make sure Scp.exp has execute permission, 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 timeout a little longer, the second line set timeout 20 can set the timeout time, in seconds. Script execution effects are as follows

What else can I do?

The attentive classmate must have found out, in fact, expect provides an interactive mechanism with the terminal, the input password is only one of the application forms, as long as the terminal block needs to input, can be completed by the expect script automatic input, such as the previous script configured two interactive scenes, one is the terminal hint " Password: "When you enter a password, there is a hint" yes/no "?" When "Yes" is entered, if the connection is first established with the remote host, the execution of the Scp.exp script effect is like this

So we can configure the input command according to the prompt of the terminal, so we can achieve the automation effect. As for dealing with other interactive scenes, just follow the above script to the gourd to draw the scoop on the line

Related Article

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.