使用SSH执行命令时逃脱特殊字符

发布于 2025-01-18 11:39:57 字数 1081 浏览 0 评论 0原文

当在远程服务器输出上本地运行命令时,

desired_kernel_version="5.4.0-105-generic"

cat /tmp/grb.bkp | grep GRUB_DEFAULT
GRUB_DEFAULT=0

kernel_position=$(awk -F\' '$1=="menuentry " || $1=="submenu " {print i++ " : " $2}; /\tmenuentry / {print "\t" i-1">"j++ " : " $2};' /boot/grub/grub.cfg | grep "${desired_kernel_version}" | grep -v recovery | awk '{ print $1}' | sed 's/ //g')

echo $k_position
1>2

sed -i "s/GRUB_DEFAULT=0/GRUB_DEFAULT=\"${k_position}\"/g" /tmp/grb.bkp

cat /tmp/grb.bkp | grep GRUB_DEFAULT
GRUB_DEFAULT="1>2"

从远程服务器运行时所需的输出: 将Grub_default值的0替换为引号中的kernel_position。

server=abcd

kernel_position=$(ssh -qT $server awk -F\' '$1=="menuentry " || $1=="submenu " {print i++ " : " $2}; /\tmenuentry / {print "\t" i-1">"j++ " : " $2};' /boot/grub/grub.cfg | grep "${desired_kernel_version}" | grep -v recovery | awk '{ print $1}' | sed 's/ //g')

ssh -qT $server "sed -i "s/GRUB_DEFAULT=0/GRUB_DEFAULT=\"${k_position}\"/g" /tmp/grb.bkp"

When ran commands locally on the remote server outputs would work as expected:

desired_kernel_version="5.4.0-105-generic"

cat /tmp/grb.bkp | grep GRUB_DEFAULT
GRUB_DEFAULT=0

kernel_position=$(awk -F\' '$1=="menuentry " || $1=="submenu " {print i++ " : " $2}; /\tmenuentry / {print "\t" i-1">"j++ " : " $2};' /boot/grub/grub.cfg | grep "${desired_kernel_version}" | grep -v recovery | awk '{ print $1}' | sed 's/ //g')

echo $k_position
1>2

sed -i "s/GRUB_DEFAULT=0/GRUB_DEFAULT=\"${k_position}\"/g" /tmp/grb.bkp

cat /tmp/grb.bkp | grep GRUB_DEFAULT
GRUB_DEFAULT="1>2"

desired output when ran from remote server:
replace 0 of GRUB_DEFAULT value to kernel_position within quotes.

server=abcd

kernel_position=$(ssh -qT $server awk -F\' '$1=="menuentry " || $1=="submenu " {print i++ " : " $2}; /\tmenuentry / {print "\t" i-1">"j++ " : " $2};' /boot/grub/grub.cfg | grep "${desired_kernel_version}" | grep -v recovery | awk '{ print $1}' | sed 's/ //g')

ssh -qT $server "sed -i "s/GRUB_DEFAULT=0/GRUB_DEFAULT=\"${k_position}\"/g" /tmp/grb.bkp"

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

定格我的天空 2025-01-25 11:39:57

建议避免引用地狱。

将Muli-line命令发送到ssh中,用所有行编写脚本远程script.sh

remote-script.sh

#!/bin/bash
source ~/.bash_profile
$k_position=$1
desired_kernel_version="5.4.0-105-generic"

cat /tmp/grb.bkp | grep GRUB_DEFAULT
GRUB_DEFAULT=0

kernel_position=$(awk -F\' '$1=="menuentry " || $1=="submenu " {print i++ " : " $2}; /\tmenuentry / {print "\t" i-1">"j++ " : " $2};' /boot/grub/grub.cfg | grep "${desired_kernel_version}" | grep -v recovery | awk '{ print $1}' | sed 's/ //g')

echo $k_position
1>2

sed -i "s/GRUB_DEFAULT=0/GRUB_DEFAULT=\"${k_position}\"/g" /tmp/grb.bkp

cat /tmp/grb.bkp | grep GRUB_DEFAULT
GRUB_DEFAULT="1>2"

remote-script.sh使用scp命令上,将当前用户执行权限命令

chmod u+x remote-script.sh

复制remoce> remote-script.sh to <代码> $服务器。如果可能的话,在部署时间只有一次。

scp -q remote-script.sh $server:/home/your-user

使用ssh命令在远程服务器上运行Remote-script.sh。通过$ k_position在命令行中。

ssh -qT $server "bash -c /home/your-user/remote-script.sh $k_position"

顺便说一句,在计算kernel_position中,建议折叠所有awkgrepsed> sed命令命令到单个中Awk脚本。

Suggesting to avoid quoting hell.

Send muli-line command into ssh by writing a script remote-script.sh with all lines.

remote-script.sh

#!/bin/bash
source ~/.bash_profile
$k_position=$1
desired_kernel_version="5.4.0-105-generic"

cat /tmp/grb.bkp | grep GRUB_DEFAULT
GRUB_DEFAULT=0

kernel_position=$(awk -F\' '$1=="menuentry " || $1=="submenu " {print i++ " : " $2}; /\tmenuentry / {print "\t" i-1">"j++ " : " $2};' /boot/grub/grub.cfg | grep "${desired_kernel_version}" | grep -v recovery | awk '{ print $1}' | sed 's/ //g')

echo $k_position
1>2

sed -i "s/GRUB_DEFAULT=0/GRUB_DEFAULT=\"${k_position}\"/g" /tmp/grb.bkp

cat /tmp/grb.bkp | grep GRUB_DEFAULT
GRUB_DEFAULT="1>2"

Give current user execution permissions on remote-script.sh

chmod u+x remote-script.sh

Use scp command to copy remote-script.sh to $server. If possible only once at deploy time.

scp -q remote-script.sh $server:/home/your-user

Use ssh command to run remote-script.sh on remote server. Pass $k_position in command line.

ssh -qT $server "bash -c /home/your-user/remote-script.sh $k_position"

BTW, in computing kernel_position, suggesting to fold all awk, grep, sed commands into a single awk script.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文