ssh 和 chroot,然后是 shell 中的 cd
ssh and chroot followed by cd in shell
如何在 shell 脚本中对远程节点进行 chroot 后执行 cd 命令?
例如:
我需要这个。
ssh remote-node "chroot-path cd command here; extra commands"
没有 chroot 它工作正常,如果我把命令列表放在另一个 shell 脚本中并在 chroot 之后执行 shell 脚本似乎 运行 没问题。
但是 chroot 似乎破坏了 cd?
假设 chroot-path
你的意思是 chroot /some/root/path
。
chroot
只接受一个命令,而 cd
不是一个命令,它是一个 shell 内置命令,所以不会工作。
此外,在 chroot
设置下,只有 cd command here
运行(或试图)。 ;
之后的所有内容都是 运行ning 在主 shell.
中
脚本是执行您想要的操作的最简单方法。
使用 printf %q
让您的本地 shell(必须是 bash)为您提供有效的正确引用,并使用 bash -c
显式调用远程 shell 与您的 chroot
.
下的引用兼容(因为 %q
可以生成 bash-仅引用包含特殊字符的输入字符串)
cmd_str='cd /to/place; extra commands'
remote_command=( bash -c "$cmd_str" )
printf -v remote_command_str '%q ' "${remote_command[@]}"
ssh remote-node "chroot /path/here $remote_command_str"
bash -c
是必需的,因为 cd
是一个 shell 结构,并且 chroot
默认直接执行它的参数(没有 shell)。
printf %q
和 cmd_str
的正确(单引号)引号确保命令字符串由最终 shell 执行(bash -c
在chroot),不是本地 shell,也不是远程 pre-chroot shell.
如何在 shell 脚本中对远程节点进行 chroot 后执行 cd 命令?
例如: 我需要这个。
ssh remote-node "chroot-path cd command here; extra commands"
没有 chroot 它工作正常,如果我把命令列表放在另一个 shell 脚本中并在 chroot 之后执行 shell 脚本似乎 运行 没问题。
但是 chroot 似乎破坏了 cd?
假设 chroot-path
你的意思是 chroot /some/root/path
。
chroot
只接受一个命令,而 cd
不是一个命令,它是一个 shell 内置命令,所以不会工作。
此外,在 chroot
设置下,只有 cd command here
运行(或试图)。 ;
之后的所有内容都是 运行ning 在主 shell.
脚本是执行您想要的操作的最简单方法。
使用 printf %q
让您的本地 shell(必须是 bash)为您提供有效的正确引用,并使用 bash -c
显式调用远程 shell 与您的 chroot
.
%q
可以生成 bash-仅引用包含特殊字符的输入字符串)
cmd_str='cd /to/place; extra commands'
remote_command=( bash -c "$cmd_str" )
printf -v remote_command_str '%q ' "${remote_command[@]}"
ssh remote-node "chroot /path/here $remote_command_str"
bash -c
是必需的,因为 cd
是一个 shell 结构,并且 chroot
默认直接执行它的参数(没有 shell)。
printf %q
和 cmd_str
的正确(单引号)引号确保命令字符串由最终 shell 执行(bash -c
在chroot),不是本地 shell,也不是远程 pre-chroot shell.