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
<br />I have summarized the annotation writi...
This article records the installation tutorial of...
Table of contents Preface 1. Installation 1. Down...
There is a picture in a big box. When you put the...
Table of contents Math Objects Common properties ...
1. Commonjs Commonjs is a custom module in nodejs...
If you are a developer looking to get into the wo...
Recently, due to business reasons, I need to acce...
There are three types of virtual hosts supported ...
Source of the problem: If the slave server is the...
I wrote some Qt interface programs, but found it ...
question First, let me talk about the problem I e...
Preface DISTINCT is actually very similar to the ...
Table of contents Preface The principle of browse...
In development, it is often necessary to cache th...