Linux uses expect for manual password entry, linux password-free login

Source: Internet
Author: User

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"
Expect EOF
Exit

Automatic login with expect

One, what is expect?
When doing system management, we often need to enter a password, for example: Connect ssh, connect FTP,
So how can you do not enter the password?
We need to have a tool that can replace our interaction with the terminal,
Well, that's it: expect, one of the best friends of the Administrator
It can replace our interaction with the terminal, we don't have to wait to enter the password next to the computer,
or run the corresponding command according to the output of the system,
These can all be done by expect instead of us.

Description: What exactly is expect?
Expect is a scripting language that is very simple to use, and we look at the following example to see

Three, install expect

Note: Because the expect is TCL-based, you need to have TCL installed in your system
How to check?
[Email protected] ~]# Whereis tcl
Tcl:/usr/lib/tcl8.4/usr/share/tcl8.4
If you do not see the results, please install TCL first
Installation
[email protected] ~]# Yum install expect
You can also download the RPM package for the corresponding release version from Http://rpm.pbone.net

Four, using expect automatic login example
1, the contents of the program example:
First, do the functional description
This program SSH login to the IP address passed as a parameter.
Then execute: df-h
Free-m
Uptime
To check the condition of the system.


[email protected] ~]# cat Monitor_auto
#!/usr/bin/expect-f

#--------------------------------------------------About Us
# Product:monitorone
# Author:liuhongdi <[email protected]>
# Last MODIFIED:2008-05-13
# version:0.3.2
# user:this script would help you to monitor many Linux (Unix) machine
# License:this script is based GPL

#--------------------------------------------------Set the variable,you can modify the value

Set Loginuser "Root"
Set Loginpass {Passwordonthishost}

Set ipaddr [lrange $argv 0 0]
Set Timeout 300
Set Cmd_prompt "]#|~]?"

#--------------------------------------------------Login by SSH
Spawn ssh [email protected] $ipaddr
Set Timeout 300
Expect {
-re "is sure you want to continue connecting (yes/no)?" {
Send "yes\r"
}-re "Assword:" {
Send "$loginpass \ r"
}-re "Permission denied, please try again." {
Exit
}-re "Connection refused" {
Exit
} Timeout {
Exit
} EOF {
Exit
}
}

Expect {
-re "Assword:" {
Send "$loginpass \ r"
}
-re $cmd _prompt {
Send "\ r"
}
}

#----------------------------------------------------Now,we do some commands
EXEC sleep 1
Expect {
-re $cmd _prompt {
Send "df-h\r"
}
}

EXEC sleep 1
Expect {
-re $cmd _prompt {
Send "free-m\r"
}
}

EXEC sleep 1
Expect {
-re $cmd _prompt {
Send "uptime\r"
}
}
EXEC sleep 1


#--------------------------------------------------
Expect {
-re $cmd _prompt {
Send "exit\r"
}
}


Exit
#interact

2, the program running display results

[Email protected] ~]#/monitor_auto 209.209.94.107
Spawn ssh [email protected]
[email protected] ' s password:
Last Login:sun-01:42:39 from 201.103.105.49

[Email protected] ~]#
[Email protected] ~]# df-h
Filesystemèyò?ó?? éó? ò?ó?% 1ò???
/dev/mapper/volgroup00-logvol00
133G 72G 55G 57%/
/DEV/SDA1 99M 13M 82M 14%/boot
None 1014M 0 1014M 0%/dev/shm
209.209.94.109:/www/pics
5.9T 5.6T 138G 98%/bank/bank1
[Email protected] ~]# free-m
Total used free shared buffers Cached
Mem:2026 1955 71 0 72 1621
-/+ buffers/cache:261 1764
swap:1983 68 1915
[Email protected] ~]# uptime
01:48:00 up 561 days, 8:53, 2 users, Load average:0.13, 0.09, 0.07
[Email protected] ~]# [[email protected] ~]#


Four, the detailed description of this procedure:
1,set Loginuser "Root"
Set is used to define variables, and the defined variables can be used in the code after the definition
Note that you need to add a $ symbol when using
Example of use: Spawn ssh [email protected] $ipad

Linux uses expect for manual password entry, linux password-free login

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.