Shell code:
Copy Code code as follows:
#!/usr/local/bin/expect
Set PASSWD [lindex $argv 1]
Set IP [lindex $argv 0]
Set CMD [lindex $argv 2]
Spawn ssh $IP $CMD
Expect "(yes/no)?" {
Send "YESR"
Expect "Password:"
Send "$PASSWDr"
} "Password:" {send "$PASSWDr"} "*host" {Exit 1}
Expect EOF
Annotations:
The first line is to make use of the expect command in the/usr/local/bin directory to explain the following procedure.
The second line, three lines, four rows, gets the host IP address, login password, and the command to be executed from the command-line arguments respectively.
Line five, presumably to trigger such an event, to execute the SSH $IP $CMD command.
Lines 6th through 11th are the expect of the entire interaction process.
If read to the (yes/no) prompt, enter Yes and carriage return, and if read to password: Prompt for a string of passwords, enter the user login password (root user).
Of course, if it is not the first landing, has been logged in before, when the input SSH $IP $CMD carriage return, will be prompted to enter the password that is to read the string "* Password:", this time will enter the password return (send "$PASSWDr").
In addition, if the host is not up to, (yes/no) and "Password:" may not appear, the system will prompt:
"No route to host" exits the program.