Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/61.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在C中的cgi编程中调用外部bash shell脚本_C_Bash_Cgi_Sh_Fastcgi - Fatal编程技术网

如何在C中的cgi编程中调用外部bash shell脚本

如何在C中的cgi编程中调用外部bash shell脚本,c,bash,cgi,sh,fastcgi,C,Bash,Cgi,Sh,Fastcgi,我想在用c编写的cgi程序中调用一个外部shell脚本 我在cgi代码中使用了system()命令。代码是 #include <stdio.h> #include <string.h> #include <ctype.h> #include<stdlib.h> #define MAXLEN 1024 #define CONFIG_FILE "/home/usr/webserver/properties.cfg" char *trim (cha

我想在用c编写的cgi程序中调用一个外部shell脚本

我在cgi代码中使用了system()命令。代码是

#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include<stdlib.h>

#define MAXLEN 1024
#define CONFIG_FILE "/home/usr/webserver/properties.cfg"


char *trim (char * s)
{

  char *s1 = s, *s2 = &s[strlen (s) - 1];
  while ( (isspace (*s2)) && (s2 >= s1) )
  s2--;
  *(s2+1) = '\0';
  while ( (isspace (*s1)) && (s1 < s2) )
  s1++;

  strcpy (s, s1);
  return s;
  }

  void parse_config ()
  {
   char *s, buff[1024];

   int i,j;
   FILE *fp = fopen (CONFIG_FILE, "r");
   if (fp == NULL)
   {
     printf("reached");
     return;
   }

     /* Read next line */
   while ((s = fgets (buff, sizeof buff, fp)) != NULL)
 {

/* Skip blank lines and comments */
if (buff[0] == '\n' || buff[0] == '#')
  continue;

/* Parse name/value pair from line */
char name[MAXLEN], value[MAXLEN],arr[]={0} ;
char new_str[MAXLEN+1] = {0};   
s = strtok (buff, "=");
if (s==NULL)
  continue;
else
  strncpy (name, s, MAXLEN);
s = strtok (NULL, "=");
if (s==NULL)
  continue;
else
  strncpy (value, s, MAXLEN);
trim (value); 
i= strlen(value);
if(value[0]!='"')
strncpy(new_str,value,MAXLEN);

else
strncpy(new_str, &value[1], i-2);

printf("<tr><td>%s</td><td><input type = \"text\" name =%s value=%s>",name,name,value);
printf("</td> </tr>");


}

 fclose (fp);
}


int main (int argc, char *argv[])
{

  char *test="hello";
  char *data;
  char ADDRESS;
    system("/home/usr/webserver/eth0script.sh"); 
  printf("Content-Type: text/html\n\n");
  printf("<html>\n");
  printf("<head>\n");
  printf("</head>\n");
  printf("<body>\n");


  printf("<form action =\"/cgi-bin/final.cgi\" method =\"POST\">");
  printf("<table style=\"width:100%\">");
  parse_config ();
  printf(" </table>");
  printf("<input type =\"submit\" name = \"submit\" value = \"submit\"></form>");
  printf("</body>\n");
  printf("</html>\n");
  return 0;
  }
如何在cgi代码中调用脚本


提前谢谢。

我测试了您的代码,我可以使用
system()
调用shell脚本eth0script.sh,没有问题。 不要忘记使脚本可执行以避免“权限被拒绝”:


您是否尝试使用了
system()
?是的。在main中,我使用system()system(“/home/usr/webserver/eth0script.sh”)调用外部脚本;像
execve
这样的exec派生也应该可以工作。那么你的问题是什么呢?我在cgi代码中使用了system()代码。然后我编译了代码,并将编译后的代码放在CGIBIN文件夹(apache2服务器)中。我使用webbrowser运行可执行代码。但是那一次system()调用不起作用。using./cgi代码起作用了。但我编译了代码,并将可执行文件放在apache2的cgi库中。它不起作用。在编译cgi代码后,您是否尝试过为所有操作系统用户更改可执行cgi代码和脚本的文件权限<代码>chmod a+x cgi代码
 #! /bin/bash
 sed -i "s/\b\DEFAULT_INTERFACE=\b.*/DEFAULT_INTERFACE=$(ifconfig -a |   awk '/eth/ {print $1}')/g" /home/usr/webserver/properties.cf
chmod +x eth0script.sh