期待在Sever上登录和运行本地文件的脚本

时间:2018-01-31 15:23:32

标签: bash ssh expect

我尝试使用Expect首先登录服务器,然后在服务器上运行本地文件。

我有一个名为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]

emrScript.sh与Expect脚本位于同一目录中:

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

当我致电/usr/bin/expect sshScript.exp <username> <password> <host>时 它会显示提示Enter your AD Password:然后我收到错误bash: ./emrScript.sh: No such file or directory,因为它试图从服务器运行该文件

但是我可以只运行命令ssh -o StrictHostKeyChecking=no <username>@<host> bash -s < ./emrScript.sh并且它会询问我的密码并从本地位置相应地运行脚本。

我在期望剧本中做错了什么?

1 个答案:

答案 0 :(得分:1)

spawn ssh $user@$host "bash -s" < "./emrScript.sh"实际上会运行ssh $user@$host "bash -s" "<" "./emrScript.sh",与ssh $user@$host "bash -s < ./emrScript.sh"相同。

您可以这样写:

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