Expect implement automatic logon scripts

Source: Internet
Author: User

written in front: currently in the company is a person a virtual machine, most of the work to be done on the virtual machine, to do so many times a day to perform the "ssh xxx@xxxxxx" instructions to log on to the virtual machine, there are many ways to solve this problem, such as the use of Xshell, SecureCRT and other tools to record commonly used connections, I wrote a simple script to achieve one-click Login ~

The contents of the login.sh script are as follows:

#!/usr/bin/expect
spawn ssh xxx@xxxxxx
expect "*password:"
send  "password \ r"
interact

then the "./login.sh" configuration alias aliases, or moved to the executable directory, you can achieve "login" one-click login;

expect is an instruction that handles interactions, and through expect we can write the interactive process into a shell script to automate some of the actions.

expect has four core directives: Spawn: Start a new process followed by the instructions that the new process will execute; Expect: Specifies the string to listen for, triggering send if the spawn process returns a matching string, such as a standard input message; Send: Sends the specified string to the spawn process instead of the standard input; interact: The user participates in the interaction;

the execution process for login.sh is:
"Spawn" launches a new process to execute the SSH login instruction, "expect" listens to the standard input prompt "xxx@xxxxxx ' password" matches the matching rule, triggers "send" to send the password instead of the manual input, completes the login, "the Interact" The user participates in the interaction, at this time has logged on the virtual machine;

use expect can also complete such as: Batch update git code base, automatic login FTP and other functions, etc.

Blog move: https://segmentfault.com/blog/leeonfancy

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.