With a day's time, Linux under the expect implementation of SSH automatic login server, despise under the online various copy to copy to the residual jokes

Source: Internet
Author: User

Because of the client side of the fast 30 projects in a special sequence of restart, so have to find a way to write a script, online read a lot of jokes. It's a broken can. No one can work properly. I'll record it in order.


The script itself


Use expect to implement automatic logon script, there are many online, but there is no clear explanation, beginners are generally copied, collection. But why do you want to write this but do not know it. This article uses a shortest example to illustrate the principle of scripting.
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 code in the operating system script to use that Shell to execute. The expect here is actually a kind of thing with bash under Linux and cmd under Windows.
Note: This line is required on the first line of the script.
2. [Set timeout 30]
Basically know English is known this is set timeout time, now you just remember his timing unit is: seconds
3. [Spawn ssh-l username 192.168.1.1]
Spawn is a expect internal command that can be executed after entering the expect environment, if no expect is installed or the Spawn command is not found directly under the default shell. So don't use commands like "which spawn" to find spawn commands. Like dir in Windows is an internal command that comes with the shell and 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 the interactive instructions.
4. [Expect "Password:"]
Here expect is also a expect internal command, a bit dizzy, expect shell command and internal command is the same, but not a function, habit is good. This command means to determine whether the last output contains the string "Password:", if any, return immediately, or wait for a period of time to return, where the waiting length is the previous set of 30 seconds
5. [Send "ispass\r"]
This is where the interactive action is performed, which is equivalent to the action of entering the password manually.
Tips: The end of the command string do not forget to add "\ r", if the status of abnormal waiting can be checked.
6. [Interact]
After the completion of the implementation of the interactive State, the control to the console, this time can be manually operated. If this is not done, it will exit instead of remaining on the remote terminal. If you just log in past to execute
#!/usr/bin/expect #注意安装的路径, not sure Whereis expect a bit.
# Change a login shell to bash
Set user [lindex $argv 0]
Spawn Bash $user
Expect "]:"
Send "/bin/bash"



And then the problem comes.

First to install

#apt-get Install expect or yum install expect


Then, the above script uses SH life is unable to run, this must be remembered, otherwise it will be reported as follows:


Start.sh:line 3:spawn:command not found
": No such file or directory:
Start.sh:line 5:send:command not found
Start.sh:line 6:interact:command not found


The solution is as follows:

Scripts that contain expect cannot be executed with bash and bash cannot parse. After you add executable permissions, directly./your_script.

Then the problem comes again, it will be reported/usr/bin/expect^m:bad interpreter: No file or directory

This is how it should be done:

Run the script, there was an error, after opening did not find the so-called ^m, after the check to know that the original file format problem, that is, Linux and Windows are not fully compatible ...
Specific details regardless, if verified:

Vim test.sh
: Set FF?
If there is a fileforma=dos then it is almost certain that this is the problem.
: Set Fileformat=unix
: Wq

Ok ....


bash:./eth0-access:/bin/bash^m:bad Interpreter: No file or directory

Error Analysis:

Because the operating system is windows, I edit the script under Windows, so there may be non-visible characters.

The script file is in DOS format, that is, the line end of each line has been identified, its ASCII code is 0x0d, 0x0A.

There are many ways to see if the file is in DOS or UNIX format, or in MAC format.

Workaround:

vim filename

and then use the command

:set ff? #可以看到dos或unix的字样. If the DOS format is true.

and then use

:set Ff=unix #把它强制为unix格式的 and then save to exit.

Run the script again.

Good tired, finally finished.



With a day's time, Linux under the expect implementation of SSH automatic login server, despise under the online various copy to copy to the residual jokes

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.