'Passing Networking Commands through RUST Programs
I had a syntax issue regarding the running of Windows cmd commands through rust program. I want to pass tis command:
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)
I kept running into syntax error while running the Rust program i have written below. what is the error in the syntax and how to write the code syntax for the command above?
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");
}
Solution 1:[1]
Your issue is the following: You seem to have forgotten to add an 's' to one of your functions.
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");
}
I changed the following:
.arg(...)
to
.args(...)
The method .arg() expects a single argument to add to its arguments
You want to add multiple arguments to the command, therefore, you have to use the .args() method, which expects an (in this case) array.
The args method just does the following:
for arg in args {
self.arg(args.as_ref());
}
Good luck with whatever you are building!
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | somekindofpanem |
