Shell Script: Execute Command
Often we need to connect to a remote machine and execute a command. If that system is Unix/Linux then with a simple script this becomes very easy to create a command line script to handle this for us:
#!/usr/bin/expect -f set user [lindex $argv 0] set host [lindex $argv 1] set password [lindex $argv 2] set packets [lindex $argv 3] puts "Connecting to $host as $user to install $packets" spawn ssh $user@$host "$packets" expect "password:*" send -- "$password\r" interact
Usage: execute_cmd <username> <hostname> <password> “<Command to execute in quotes>”
This simple script will connect to the host, using the credentials provided and execute the command.