Recently, the project is engaged in the maintainability function, such as modifying the nic ip address, restarting/disabling the machine, and backing up and recovering the database. These functions are closely related to the shell script execution in Linux, however, shell scripts do not support automation. Currently, I know that scripts that support automation are also scripts that support automated processing in objective CT, Perl, and python, although Linux installed with the Perl Command library, you still need to install the third-party Dynamic Language of objective CT. Okay, you don't need to talk about it.Code:
#! /Usr/bin/CT
SET command [lindex $ argv 0]
Set username [lindex $ argv 1]
Set passwd [lindex $ argv 2]
Spawn Su-$ username-C $ command
Reset CT "Password :"
Send $ passwd \ r
CT EOF
Exit
#! /Usr/bin/expect if you do not know what this sentence means, you can run the witch epxect command to query the expect command library address. Here I am/usr/bin/expect
The script first defines the command, username, and passwd parameters. /shell. sh reboot Root is used for parameter passing. Do not use sh shell. sh method to execute the script, because the CT script itself cannot be started in the form of shell, otherwise the spawn, CT, send command error will be reported.
If you want to switch the user and call the script, you can write it as follows:
#! /Usr/bin/CT
Set username [lindex $ argv 0]
Set passwd [lindex $ argv 1]
Spawn Su-$ username
Reset CT "Password :"
Send $ passwd \ r
Expext {
"Su: Password error" {send "Echo Password error \ r "}
"* # *" {Send "Sh test. sh \ r "}
}
CT EOF
Exit
The Except CT installation package is available in my resources and can be downloaded if needed. The language idea is similar, but the writing methods of the script language are rigorous, therefore, everyone needs to be careful during the compilation process.