简体   繁体   English

期望脚本在服务器上登录并运行本地文件

[英]Expect Script to Login and Run Local File on Sever

I'm trying to use Expect to first login to the server then run a local file on the server. 我正在尝试使用Expect首先登录到服务器,然后在服务器上运行本地文件。

I have an Expect script called sshScript.exp : 我有一个称为sshScript.exp的Expect脚本:

#!/usr/bin/expect
set username [lindex $argv 0]
set password [lindex $argv 1]
set sshHost [lindex $argv 2]

# ssh and run script in emr
spawn ssh -o StrictHostKeyChecking=no $username@$sshHost "bash -s" < "./emrScript.sh"
expect "Enter your AD Password:" {
    send "$password\r"
    expect eof
}

# wait for exit code and return it
catch wait result
exit [lindex $result 3]

and emrScript.sh is located in the same directory as the Expect script: emrScript.sh与Expect脚本位于同一目录中:

#!/bin/bash
echo "Hello EMR - this script is local"
echo "this script will pass"
exit 0

When I call /usr/bin/expect sshScript.exp <username> <password> <host> It will display the prompt Enter your AD Password: then I get the error bash: ./emrScript.sh: No such file or directory because its trying to run the file from the server 当我调用/usr/bin/expect sshScript.exp <username> <password> <host> ,将显示提示Enter your AD Password:然后我收到错误提示bash: ./emrScript.sh: No such file or directory因为它试图从服务器运行文件

But I can run just the command ssh -o StrictHostKeyChecking=no <username>@<host> bash -s < ./emrScript.sh and it'll ask for my password and run the script accordingly from the local location. 但是我只能运行命令ssh -o StrictHostKeyChecking=no <username>@<host> bash -s < ./emrScript.sh ,它将询问我的密码并从本地位置相应地运行脚本。

What am I doing wrong in the expect script? 我在期望脚本中做错了什么?

spawn ssh $user@$host "bash -s" < "./emrScript.sh" would actually run ssh $user@$host "bash -s" "<" "./emrScript.sh" which is the same as ssh $user@$host "bash -s < ./emrScript.sh" . spawn ssh $user@$host "bash -s" < "./emrScript.sh"实际上会运行ssh $user@$host "bash -s" "<" "./emrScript.sh"ssh $user@$host "bash -s < ./emrScript.sh"相同ssh $user@$host "bash -s < ./emrScript.sh"

You can write like this: 您可以这样写:

spawn bash -c "ssh $user@$host bash -s < ./emrScript.sh"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM