Linux automatic login example explanation

Linux automatic login example explanation

There are many scripts on the Internet that use expect to achieve automatic login, but there is no clear explanation, and beginners usually copy and collect them. But I don’t know why it is written like this. This article uses a minimal example to illustrate the principle of the 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 line tells the operating system which shell to use to execute the code in the script. The expect here is actually the same thing as bash under Linux and cmd under Windows.

NOTE: This line needs to be the first line in the script.

2. [set timeout 30]

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

3. [spawn ssh -l username 192.168.1.1]

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

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

4. [expect "password:"]

The expect here is also an internal command of expect. It may be a bit confusing. The shell command of expect is the same as the internal command, but it is the same function. You just need to get used to it. This command means to determine whether the string "password:" is included in the last output result. If yes, it will return immediately. Otherwise, it will wait for a while before returning. The waiting time here is 30 seconds set above.

5. [send “ispass\r”]

This is to perform an interactive action, which is equivalent to manually entering a password.

Tips: Don’t forget to add “\r” at the end of the command string. If an abnormal waiting state occurs, you can check it.

6. [interact]

After the execution is completed, keep the interactive state and hand over the control to the console. At this time, you can operate manually. If this sentence is not included, the system will log out after login instead of staying on the remote terminal.

The above is the content of this article about the example of automatic login in Linux. Thank you for your learning and support for 123WORDPRESS.COM.

You may also be interested in:
  • How to automatically log in to the switch and save the configuration in linux expect
  • Linux expect to achieve automatic login script example code
  • Use expect script to realize automatic login of remote machine in Linux
  • How to implement automatic login in Linux operating system

<<:  How to add indexes to MySQL

>>:  jQuery+Ajax to achieve simple paging effect

Recommend

Detailed explanation of MySQL InnoDB secondary index sorting example

Sorting Problem I recently read "45 Lectures...

Unbind SSH key pairs from one or more Linux instances

DetachKeyPair Unbind SSH key pairs from one or mo...

Teach you how to write maintainable JS code

Table of contents What is maintainable code? Code...

Detailed process of using Vscode combined with docker for development

Preface Using Docker and VS Code can optimize the...

Use of Linux watch command

1. Command Introduction The watch command execute...

Html Select uses the selected attribute to set the default selection

Adding the attribute selected = "selected&quo...

WeChat applet implements video player sending bullet screen

This article shares the specific code for WeChat ...

How are Vue components parsed and rendered?

Preface This article will explain how Vue compone...

The difference between HTML name id and class_PowerNode Java Academy

name Specify a name for the tag. Format <input...

Explanation of the working principle and usage of redux

Table of contents 1. What is redux? 2. The princi...

Vue virtual Dom to real Dom conversion

There is another tree structure Javascript object...