Example Explanation of Automatic Login by Linux

  • 2021-07-06 12:19:57
  • OfStack

Using expect to achieve automatic login script, there are many online, but there is no clear explanation, beginners 1 are copied, collection. But I don't know why I want to write it like this. In this paper, the shortest example is used to illustrate the principle of script.

The script code is as follows:


#!/usr/bin/expect
set timeout 30
spawn ssh -l username 192.168.1.1
expect "password:"
send "ispass\r"
interact

1. [#! /usr/bin/expect]

This 1 line tells the operating system that the code in the script uses which 1 shell to execute. expect here is actually a class of things like bash under linux and cmd under windows.

Note: This 1 line needs to be in line 1 of the script.

2. [set timeout 30]

Basically, anyone who knows English knows that this is to set the timeout time. Now you just need to remember that his timeout unit is: seconds

3. [spawn ssh-l username 192.168. 1.1]

spawn is an expect internal command that can be executed only after entering expect environment. If expect is not installed or executed directly under the default SHELL, spawn command cannot be found. So don't use commands like "ES40spawn" to find the spawn command. For example, dir in windows is an internal command, which comes with shell. You can't find an executable file of dir. com or dir. exe.

Its main function is to add a shell to the ssh running process to pass interactive instructions.

4. [expect "password:"]

expect here is also an internal command of expect, which is a bit dizzy. shell command and internal command of expect are the same, but not a function, so get used to it. This command means to judge whether the last output contains the string of "password:", if so, return immediately, otherwise, wait for a period of time to return, where the waiting time is 30 seconds set before

5. [send "ispass\ r"]

This is an interactive action, which is equivalent to manually entering passwords.

Warm Tip: Don't forget to add "\ r" at the end of the command string. If there is an abnormal waiting state, you can check 1.

6. [interact]

After the execution is completed, the interactive state is maintained, and the control is handed over to the console, which can be operated manually at this time. If you don't have this sentence, you will exit after logging in, instead of staying on the remote terminal.

The above is the introduction of Linux to achieve automatic login related examples of content, thank you for your learning and support for this site.


Related articles: