问题
I want to execute my local script on remote server.
#!/usr/bin/expect -f
set idNhost [lindex $argv 0]
set password [lindex $argv 1]
eval spawn ssh $idNhost \'bash -s\' < a.sh
expect -re "password"
send "$password\r"
interact
But expect interpret each argument as a single commend. So it find script on remote server.
Please help me....
回答1:
Try like this:
spawn bash -c "ssh $idNhost bash -s < a.sh"
回答2:
You can first send your script to remote server then attach a shell and execute it from there. Lastly, it would be cleaner to delete your script from the remote server. The following procedure is designed for this purpose.
proc exect_on_shell { script_name user ip pass dir } {
spawn scp $script_name $user@$ip:$dir/$user
expect "password:"
send "$pass\r"
expect eof
puts "running $script_name"
#Attach shell
puts "Connecting to the machine $ip for user $user"
spawn ssh $user@$ip
expect "yes/no" {
send "yes\r"
expect "*?assword" { send "$pass\r"}
} "*?assword" { send "$pass\r"}
#regular expression to match prompt
expect -re {\$ $}
send "su - root\r"
expect {
"Password: " {send "$pass\r"}
}
send "cd $dir/$user\r"
send "chmod +x $script_name\r"
send "$script_name\r"
send "rm -rf $script_name\r"
#logout from root
send "exit\r"
#logout from user
send "exit\r"
expect eof
}
You can call it like in the following snippet.
exect_on_shell "your_script.sh" "user_name" "192.168.2.3" "pass" "home/user_name"
来源:https://stackoverflow.com/questions/50363773/execute-local-script-on-remote-server-in-expect