Linux Remote automatic login and execute commands
Telnet
This automatic login process is implemented through the expect inside the shell, similar to opening a cmd-like command segment output IP and password.
Note that the script can be executed if the expect is installed
Yum Install-y expect
Directly on the script:
#!/usr/bin/expect expect command path Whereis view
Set timeout 5 execution time out any input 5S after exiting
spawn/usr/bin/ssh [email protected] connected user and host IP
Expect "*password:" To determine if the results of the last output contain passwd there is no good wait 5S timeout exit
Send "redhat\r" Enter password
Expect "*]#" defines the start of the command
Send "df-h\r" sends the command to execute
Expect "*]#" defines the end of the command
Send "logout\r" sign Out
(Interact #用exact这个指令是为了把控制权交给用户,Replacesend "logout\r"Terminal does not disconnect)
Note: The script is expect script is not shell sh file. Cannot execute can./file or expect file
Extensions: You can write the above script in a loop, looping through the passwords and IP in the file for batch machine execution commands or other scripts.
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/70/70/wKioL1W4Rvfw4T1UAAEtqBWb7CI064.jpg "title=" 1.png " alt= "Wkiol1w4rvfw4t1uaaetqbwb7ci064.jpg"/>
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 TIMEOUT5]
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.132]
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 "df\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.
If there are errors or better suggestions, please point out!!
This article is from the "New One" blog, please be sure to keep this source http://welcomeweb.blog.51cto.com/10487763/1679528
Linux expect remote automatic login and execute commands