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

Linux 执行远程登录和执行另一个本地脚本的脚本,linux,scripting,remote-access,expect,Linux,Scripting,Remote Access,Expect,我必须编写一个执行远程登录和身份验证的脚本,并在远程服务器上执行本地脚本 我的原始代码如下所示: #!/usr/bin/expect spawn ssh user@ip "bash -ls" < ./script.bash expect "password" send "abc123/r" interact 它成功登录 如果我不使用脚本直接运行终端,比如 ssh user@ip "bash -ls" < ./script.bash sshuser@ip“bash-ls”

我必须编写一个执行远程登录和身份验证的脚本,并在远程服务器上执行本地脚本

我的原始代码如下所示:

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

如果我不使用脚本直接运行终端,比如

ssh user@ip "bash -ls" < ./script.bash
sshuser@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
#/usr/bin/expect
繁殖sshuser@ip“bash-ls”
这是因为expect不理解shell的输入重定向。试试这个:

spawn sh -c {ssh user@ip "bash -ls" < ./script.bash}
spawn sh-c{sshuser@ip“bash-ls”<./script.bash}
如果“用户”或“ip”是一个变量,我们将需要不同的报价。比如

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

起初,我只使用完整路径。没有好处。它给出了同样的错误。我不是很精通shell脚本,因此请容忍我。但给出的答案是只使用shell脚本执行远程登录,我也成功地完成了。我在远程机器上执行本地脚本时遇到了问题。谢谢,它可以工作!你能解释一下这是怎么回事吗?
spawn sh -c {ssh user@ip "bash -ls" < ./script.bash}
spawn sh -c "ssh $user@$ip 'bash -ls'  < ./script.bash"