将字符串传递给C中的popen时保留双引号 [英] Keeping double quotes when passing string to popen in C

查看:260
本文介绍了将字符串传递给C中的popen时保留双引号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个两难的地方,我试图在C中通过popen传递一个字符串,但让它保持字符串中的双引号。该字符串如下所示:

  ssh%s @%s grep -c \%s\%s% s 

我需要运行此命令,以便从远程系统上的日志中获取警报并返回计数为了它。我通过字符串传递不同的参数,所以它会生成用户名和密码以及搜索字符串和目标。但是,搜索字符串包含多个由空白字符分隔的单词,所以我需要双引号完整,以便正确解析搜索字符串。然而,到目前为止,popen一直在剥离字符串中的双引号,因此搜索没有完成。我不确定使用远程ssh命令的另一种方式,或者在语句中保留双引号。任何想法?

谢谢!

*编辑:这是我用来生成的完整sprintf语句字符串以及popen命令。

  sprintf(命令,ssh%s @%s grep -c \ %s \%s%s,用户,服务器,搜索,路径,文件); 
fd = popen(command,r);


解决方案

popen 派生一个使用2个参数执行shell的子代: -c 和你传递给popen的命令字符串。所以你传递的对shell有意义的任何字符都需要被转义,这样shell才会对它们做正确的事情。在你的情况下,你需要shell来保存字符,这样远程shell才能得到它们,这是通过包装'引用它们:

  sprintf(command,ssh%s @%s grep -c'\\ \\%s \'%s%s,... 

但是,仅当您的搜索字符串不包含任何'字符时才起作用 - 如果是,则需要执行

  sprintf(command,ssh%s @%s grep -c \\\\%s \\\\%s%s,... 

但是如果你的搜索字符串有引号或者多个连续的空格或者其他空白的标签int,那么这将失败。为了处理所有的情况,你需要首先在搜索字符串中的所有相关字符之前插入反斜杠,再加上<$ c $

  //分配4 * strlen(search)+1 space for escaped_search 
for(p1 = search,p2 = escaped_search; * p1;){
if(* p1 =='\'')* p2 ++'\'';
if(strchr(\\\\\\\\\\\\\\')\\\\'' * p2 ++ ='\';
if(* p1 =='\'')* p2 ++'\'';
* p2 ++ = * p1 ++;}
* p2 = '\0';
sprintf(命令,ssh%s @%s grep -c'%s'%s%s,user,server,escaped_search,...


I'm having a dilemma where I'm trying to pass a string through popen in C, but have it maintain the double quotes in the string. The string reads as follows:

ssh %s@%s grep -c \"%s\" %s%s

I need to run this command so it greps from a log on a remote system and returns the count for it. I'm passing different arguments to through the string, so it generates the username and password as well as the search string and target. However, the search string contains multiple words separated by whitespace characters, so I need the double quotes intact so it parses out the search string correctly. However, so far popen has been stripping the double quotes from the string so the search doesn't complete. I'm not sure of another way to do the remote ssh command, or of keeping the double quotes in the statement. Any ideas?

Thanks!

*EDIT: Here is the full sprintf statement I'm using to generate the string as well as the popen command.

sprintf(command, "ssh %s@%s grep -c \"%s\" %s%s", user, server, search, path, file);
fd = popen(command, "r");

解决方案

popen forks a child which execs a shell with 2 arguments: -c and the command string you passed to popen. So any characters you pass that are meaningful to the shell need to be escaped so the shell does the right thing with them. In your case, you need the shell to KEEP the " characters so that the remote shell will get them, which is easiest to do by wrapping ' quotes around them:

sprintf(command, "ssh %s@%s grep -c '\"%s\"' %s%s", ...

However, this will only work if your search string does not contain any ' or " characters -- if it does, you need to do some more complex escaping. You could instead use backslash escapes:

sprintf(command, "ssh %s@%s grep -c \\\"%s\\\" %s%s", ...

but THIS will fail if your search string has quotes or multiple consecutive spaces or other whitespace like tabs int it. To deal with all cases you need first insert backslashes before all relevant charaters in the search string, plus ' are a pain to deal with:

// allocate 4*strlen(search)+1 space as escaped_search
for (p1 = search, p2 = escaped_search; *p1;) {
    if (*p1 == '\'') *p2++ '\'';
    if (strchr(" \t\r\n\"\\'{}()<>;&|`$", *p1))
        *p2++ = '\';
    if (*p1 == '\'') *p2++ '\'';
    *p2++ = *p1++; }
*p2 = '\0';
sprintf(command, "ssh %s@%s grep -c '%s' %s%s", user, server, escaped_search, ...

这篇关于将字符串传递给C中的popen时保留双引号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆