Python:
print("Active Device:"+subprocess.getoutput("ip link|grep \"state UP\"|cut -d : -f 2|cut -d \' \' -f2"))
我可以通过这样做在c中得到像That⬆这样的输出吗?: C:
printf("Active Device:%s",getsystemoutput("ip link|grep \"state UP\"|cut -d : -f 2|cut -d \' \' -f2"));
最佳答案
函数 getsystemoutput()
可能如下所示:
char *getsystemoutput( char *cmd )
{
static char output[1024];
FILE *fp;
if( (fp = popen( cmd, "r" )) == NULL )
return "";
if( fgets( outout, sizeof output, fp ) == NULL )
strcpy( output, "" );
pclose( fp );
return output;
}
此版本在出现错误时返回一个空字符串,并使用静态缓冲区进行输出,这使得在 printf()
中使用变得很容易,但出于一般目的,我建议在错误并传递 output
+ 它的大小作为参数。您可能还想检查 pclose()
的结果以确定所调用命令的退出状态。请注意,如果没有循环,它只会返回第一行输出。
关于c++ - c 或 c++ 中是否有类似 subprocess.getoutput() 的函数或方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47909072/