Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

An example of automatic login based on Linux

2025-04-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/02 Report--

Use expect to achieve automatic login scripts, there are many on the Internet, but there is not a clear description, beginners generally copy, collection. But I don't know why I wrote it like this. This article uses the shortest example to illustrate the principle of the script.

The script code is as follows:

#! / usr/bin/expectset timeout 30spawn ssh-l username 192.168.1.1expect "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. Expect here is actually the same thing as bash under linux and cmd under windows.

Note: this line needs to be on the first line of the script.

2. [set timeout 30]

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

3. [spawn ssh-l username 192.168.1.1]

Spawn is an expect internal command that can only be executed after entering the expect environment. If you don't install expect or execute it directly under the default SHELL, you can't find the spawn command. So don't use commands like "which spawn" to find spawn commands. For example, dir in windows is an internal command, which comes with shell. You can't find an executable file for 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, a little dizzy, expect shell command and internal command is the same, but not a function, just get used to it. This command means to determine whether the last output contains a string of "password:". If so, return immediately, otherwise you will return after waiting for a period of time. Here, the waiting time is 30 seconds set above.

5. [send "ispass\ r"]

This is the interactive action, which is equivalent to entering the password manually.

Warm reminder: don't forget to add "\ r" at the end of the command string. You can check if there is an abnormal waiting status.

6. [interact]

After the execution is completed, the interactive state is maintained and the control is handed over to the console, which can be done manually. Without this sentence, you will log out after the login is completed, instead of leaving it on the remote terminal.

The above is the introduction of the relevant examples of automatic login in Linux. Thank you for your study and support.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report