Implementation method of implementing SSH automatic login server and batch management with expect _linux shell

Source: Internet
Author: User
Tags ssh

Shell code:

Copy Code code as follows:

#!/usr/local/bin/expect
Set PASSWD [lindex $argv 1]
Set IP [lindex $argv 0]
Set CMD [lindex $argv 2]
Spawn ssh $IP $CMD
Expect "(yes/no)?" {
Send "YESR"
Expect "Password:"
Send "$PASSWDr"
} "Password:" {send "$PASSWDr"} "*host" {Exit 1}
Expect EOF

Annotations:

The first line is to make use of the expect command in the/usr/local/bin directory to explain the following procedure.
The second line, three lines, four rows, gets the host IP address, login password, and the command to be executed from the command-line arguments respectively.
Line five, presumably to trigger such an event, to execute the SSH $IP $CMD command.
Lines 6th through 11th are the expect of the entire interaction process.
If read to the (yes/no) prompt, enter Yes and carriage return, and if read to password: Prompt for a string of passwords, enter the user login password (root user).
Of course, if it is not the first landing, has been logged in before, when the input SSH $IP $CMD carriage return, will be prompted to enter the password that is to read the string "* Password:", this time will enter the password return (send "$PASSWDr").
In addition, if the host is not up to, (yes/no) and "Password:" may not appear, the system will prompt:
"No route to host" exits the program.

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.