通过 RUST 程序传递网络命令

Passing Networking Commands through RUST Programs

我有一个关于 运行 of Windows cmd 命令通过 rust 程序的语法问题。我想传递这个命令:

netsh interface ipv4 set address name="Adapter123" static 192.0.0.4 255.255.255.0 192.0.0.4

(Syntax: netsh interface ipv4 set address name="YOUR INTERFACE NAME" static IP_ADDRESS SUBNET_MASK GATEWAY)

我在下面写的 Rust 程序 运行 时一直 运行 语法错误。上面命令的语法错误是什么以及如何编写代码语法?

use std::process::Command;
fn main()
{
Command::new("netsh")
.arg(&["interface", "ipv4","set","address","name = ",
"Adapter123","static", "192.0.0.4", "255.255.255.0", "192.0.0.4"])
.spawn()
.expect("ls command failed to start");

}

您的问题如下:您似乎忘记了向您的某个函数添加 's'。

use std::process::Command;

pub fn main() {
    Command::new("netsh")
        .args(&["interface", "ipv4","set","address","name = ",
        "Adapter123","static", "192.0.0.4", "255.255.255.0", "192.0.0.4"])
        .spawn()
        .expect("ls command failed to start");
}

我更改了以下内容:

.arg(...)

.args(...)

The method .arg() expects a single argument to add to its arguments 您想要向命令添加 多个 参数,因此,您必须使用 .args() method,它需要一个(在本例中)数组。

args 方法仅执行以下操作:

for arg in args {
    self.arg(args.as_ref());
}

无论你在做什么,祝你好运!