execute local script on remote server in expect

廉价感情. 提交于 2020-01-02 09:52:29

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!