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