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

Code comment writing standards during web page production

<br />I have summarized the annotation writi...

RHEL7.5 mysql 8.0.11 installation tutorial

This article records the installation tutorial of...

Complete step-by-step record of MySQL 8.0.26 installation and uninstallation

Table of contents Preface 1. Installation 1. Down...

JavaScript to achieve a simple magnifying glass effect

There is a picture in a big box. When you put the...

Detailed explanation of JavaScript's built-in objects Math and strings

Table of contents Math Objects Common properties ...

Implementation of built-in modules and custom modules in Node.js

1. Commonjs Commonjs is a custom module in nodejs...

How to use localStorage in JavaScript

If you are a developer looking to get into the wo...

js to call the network camera and handle common errors

Recently, due to business reasons, I need to acce...

Build nginx virtual host based on domain name, port and IP

There are three types of virtual hosts supported ...

How to modify server uuid in Mysql

Source of the problem: If the slave server is the...

Solve the problem of using linuxdeployqt to package Qt programs in Ubuntu

I wrote some Qt interface programs, but found it ...

The solution of html2canvas that pictures cannot be captured normally

question First, let me talk about the problem I e...

Detailed explanation of the basic implementation principle of MySQL DISTINCT

Preface DISTINCT is actually very similar to the ...

In-depth understanding of JavaScript event execution mechanism

Table of contents Preface The principle of browse...

Detailed explanation of keepAlive use cases in Vue

In development, it is often necessary to cache th...