Linux script automatically enters the password

Source: Internet
Author: User

Linux script automatically enters the password

Programmers using Linux must be familiar with password input. in Linux, they have strict permission restrictions on users. If they do a lot of things, they must enter the password, for example, you can use a super user to execute commands, or use ftp or ssh to connect to a remote host. For example

So the question is, what should I do when I need to enter a password for automated script execution? For example, if your script contains an scp statement, you cannot manually enter the password when the script executes this statement.

For ssh or scp commands, some people may answer the following question: Establish a trust relationship. for methods to establish an ssh trust relationship, Baidu and Google only need two lines of simple commands, however, this is not a conventional solution. If it is an ftp connection, it will be the same. Moreover, you cannot manually create ssh Trust for each host you want to connect to in order to execute some commands, this has deviated from the intention of today's topic. Today we want to say that the password is automatically entered in the script. we can imagine that the more elegant way is to configure the password in the script, when screen interaction requires input, it is automatically input. To achieve this effect, you need to use reverse CT.

Install

The installation command in CentOS is very simple, as follows:

sudo yum install expect

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

brew install expect
Test script

We will write a simple script to copy files using scp, configure the password in the script, and save it 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\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. Generally, the first line in our script is #! /Bin/bash. Here is the path of the reverse CT program on your machine. It indicates that this script is interpreted and executed by Reverse CT instead of bash, therefore, the code syntax is different from the shell script. set passwd your_passwd to your own password, and then execute the following command:

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

Make sure scp. exp has the execution permission. The first parameter is your local file, and the second is the directory of the remote host. If the error "connect is timeout" is reported when running the script, you can set the timeout time to a little longer, you can set the timeout value for set timeout 20 in the second line, in seconds. The script execution result is as follows:

What else can I do?

Careful students must have discovered that, in fact, reverse CT provides an interaction mechanism with the terminal. The password is only one form of application, as long as the input is required when the terminal is blocked, you can use the reset CT script to complete automatic input. For example, two interactive scenarios are configured in the preceding script. One is to enter the password when the terminal prompts "password, another way is to prompt "yes/no )? "Input" yes ". If the connection is established for the first time with the remote host, execute the scp. exp script.

Therefore, we can configure the input command according to the prompts of the terminal, so as to achieve the automation effect. For other interaction scenarios, you only need to follow the above script to draw images based on the gourd image.

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.