执行远程登录并执行另一个本地脚本的脚本

Script to perform remote login and execute another local script

我必须编写一个脚本来执行远程登录和身份验证,并在远程执行本地脚本。

我原来的代码是这样的:

#!/usr/bin/expect
spawn ssh user@ip "bash -ls" < ./script.bash
expect "password"
send "abc123/r"
interact

在 运行ning by ./my_script.sh

上给出了后续
spawn ssh user@ip "bash -ls" < ./script.bash
user@ip's password:
./script.bash: No such file or directory

但是,如果我 运行 没有参数的脚本,即

#!/usr/bin/expect
spawn ssh user@ip
expect "password"
send "abc123/r"
interact

登录成功

此外,如果我 运行 直接通过终端而不使用像

这样的脚本
ssh user@ip "bash -ls" < ./script.bash

然后通过提示获取密码后,本地文件在远程服务器上成功执行。因此,您能否建议如何使我的原始代码成功运行。

使用 script.bash

的完整路径
#!/usr/bin/expect
spawn ssh user@ip "bash -ls" < /the/full/path/script.bash
expect "password"
send "abc123/r"
interact

那是因为expect不理解shell的输入重定向。试试这个:

spawn sh -c {ssh user@ip "bash -ls" < ./script.bash}

如果 "user" 或 "ip" 是一个变量,我们将需要不同的引号。比如

spawn sh -c "ssh $user@$ip 'bash -ls'  < ./script.bash"