Bash 退出 SSH 后脚本退出
Bash Script Quits After Exiting SSH
我正在尝试编写一个 Bash 脚本,用于登录 2 个不同的 linux 基于电源板 (Ubiquiti Mpower Pros) 并关闭 2 个不同的灯(每个电源板上一个)。为此,我登录到第一个电源条,将适当的文件更改为 0(从而关闭灯),然后退出,在下一个电源条上重复相同的过程。但是,在我退出第一个 SSH 连接后,脚本停止工作。有人可以建议修复吗?我唯一的想法是将此脚本封装在 python 程序中。这是我的代码:
#!/bin/bash
ssh User@192.168.0.100
echo "0" > /proc/power/relay1
exit
# hits the enter key
cat <(echo "") | <command>
ssh User@192.168.0.103
echo "logged in"
echo "0" > /proc/power/relay1
exit
cat <(echo "") | <command>
ssh
作为应用程序在 运行ning 时阻塞,echo
和 exit
由本地 shell 执行,而不是由远程执行机器。所以你在做:
- ssh 到远程机器
- 退出远程shell
- 本地回显
- 本地退出
砰的一声,你的脚本已经死了。如果 echo/exit 在远程系统上应该是 运行,那么你应该这样做:
ssh user@host command
^^^^^---executed on the remote machine
例如
ssh foo@bar 'echo ... ; exit'
您显然试图 运行 到 ssh
的命令实际上正在本地执行。您可以将您想要的命令传递给 运行 ssh,它会执行此操作(无需显式 exit
)
ssh User@192.168.0.110 'echo "0" > /proc/power/relay1'
会这样做,其他 ssh 命令也类似
我正在尝试编写一个 Bash 脚本,用于登录 2 个不同的 linux 基于电源板 (Ubiquiti Mpower Pros) 并关闭 2 个不同的灯(每个电源板上一个)。为此,我登录到第一个电源条,将适当的文件更改为 0(从而关闭灯),然后退出,在下一个电源条上重复相同的过程。但是,在我退出第一个 SSH 连接后,脚本停止工作。有人可以建议修复吗?我唯一的想法是将此脚本封装在 python 程序中。这是我的代码:
#!/bin/bash
ssh User@192.168.0.100
echo "0" > /proc/power/relay1
exit
# hits the enter key
cat <(echo "") | <command>
ssh User@192.168.0.103
echo "logged in"
echo "0" > /proc/power/relay1
exit
cat <(echo "") | <command>
ssh
作为应用程序在 运行ning 时阻塞,echo
和 exit
由本地 shell 执行,而不是由远程执行机器。所以你在做:
- ssh 到远程机器
- 退出远程shell
- 本地回显
- 本地退出
砰的一声,你的脚本已经死了。如果 echo/exit 在远程系统上应该是 运行,那么你应该这样做:
ssh user@host command
^^^^^---executed on the remote machine
例如
ssh foo@bar 'echo ... ; exit'
您显然试图 运行 到 ssh
的命令实际上正在本地执行。您可以将您想要的命令传递给 运行 ssh,它会执行此操作(无需显式 exit
)
ssh User@192.168.0.110 'echo "0" > /proc/power/relay1'
会这样做,其他 ssh 命令也类似