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 add indexes to MySQL
>>: jQuery+Ajax to achieve simple paging effect
Sorting Problem I recently read "45 Lectures...
DetachKeyPair Unbind SSH key pairs from one or mo...
Table of contents What is maintainable code? Code...
Today, let’s get straight to the point and talk a...
I slept late yesterday and was awake the whole da...
Preface Using Docker and VS Code can optimize the...
1. Command Introduction The watch command execute...
1. Introduction to KVM The abbreviation of kernel...
Adding the attribute selected = "selected&quo...
This article shares the specific code for WeChat ...
Preface This article will explain how Vue compone...
What is it? Spring Boot is a sub-project of the S...
name Specify a name for the tag. Format <input...
Table of contents 1. What is redux? 2. The princi...
There is another tree structure Javascript object...