简体   繁体   English

将参数传递给远程SSH

[英]Pass parameter to remote SSH

I have a function: 我有一个功能:

function obtener_resumen(){
 ssh  root@${1} '
   echo "grep -iE ${2} ${3}";
   ifconfig;
 ' > "${4}"
}

I call it: 我称之为:

obtener_resumen "MY_IP" "PATRON" "/MY_FILE.txt" "/demo.log"

When I open demo.log only I see: 当我仅打开demo.log时,我看到:

grep -iE

When I need grep -iE PATRON /MY_FILE.txt 当我需要grep -iE PATRON /MY_FILE.txt

It's possible this?. 这可能吗? I appreciate your help . 我感谢您的帮助 。

You want the expansion to happen on the client side, so you can use double quotes to expand the value before it's passed to ssh : 您希望扩展发生在客户端,因此可以在将值传递给ssh之前使用双引号将其扩展:

# Unsafe version
function obtener_resumen(){
 ssh  root@${1} "
   echo \"grep -iE ${2} ${3}\";
   ifconfig;
  " > "${4}"
}

But to prevent command injection, use printf %q : 但是要防止命令注入,请使用printf %q

#Safe version
function obtener_resumen(){
 ssh  root@${1} "$(printf "%q " echo grep -iE "$2" "$3"); ifconfig" > "$4"
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM