使用一个单一的系统()调用在C中执行多个命令 [英] Using a Single system() Call to Execute Multiple Commands in C

查看:175
本文介绍了使用一个单一的系统()调用在C中执行多个命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我工作的一个信息安全实验室,我一直在负责与一个调用系统()(用C语言编写,在Fedora上运行)执行多个命令。是什么,让我通过系统比执行命令的详细()的语法? (这个想法是,你可以通过一个程序运行在远程计算机上执行任意命令,如果该程序通过系统中的OS()调用交互。)

即:

 字符命令[] =????? \\ r \\ n;
系统(指挥);


解决方案

这取决于外壳被调用执行的命令,但总的来说大多数shell使用; 来独立的命令所以这样的事情应该工作:

 命令1;命令2;命令3

由于@dicroce提到的,你可以使用&放大器;&安培; 而不是; 这将在停止执行第一命令返回一个非零值。这可能会或可能不会进行所需的(和某些命令可能会返回成功非零),但是如果你想以处理可能会失败,你应该不串多个命令一起在一个系统()调用命令,你不用判断故障发生在这里的任何方式。在这种情况下你最好的选择要么是在一次执行一个命令或创建执行相应的错误处理一个shell脚本,并调用来代替。

In an information security lab I'm working on, I've been tasked with executing multiple commands with a single call to "system()" (written in C, running on Fedora). What is the syntax that will allow me to execute more than command through system()? (The idea being you could execute arbitrary commands through a program running on a remote computer, if the program interacts with the OS through the system() call.)

I.e.:

char command[] = "????? \r\n"; 
system(command);

解决方案

That depends on the shell being invoked to execute the commands, but in general most shells use ; to separate commands so something like this should work:

command1; command2; command3

[EDIT]

As @dicroce mentioned, you can use && instead of ; which will stop execution at the first command that returns a non-zero value. This may or may not be desired (and some commands may return non-zero on success) but if you are trying to handle commands that can fail you should probably not string multiple commands together in a system() call as you don't have any way of determining where the failure occured. In this case your best bet would either be to execute one command at a time or create a shell script that performs the appropriate error handling and call that instead.

这篇关于使用一个单一的系统()调用在C中执行多个命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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