前一篇玩了一下组播收发,今天再来分享一下在Windows和Linux下修改网卡的网络参数;
、在Windows下通过Management来实现:

2、Ubuntu下实现就相对麻烦一点:
/// <summary>/// 1、先通过 route -n 获取到使用的网络接口/// </summary>private string GetCurrentUseInterface(){ string interfaceName = ; var process = new Process() { StartInfo = new ProcessStartInfo { FileName = "/bin/bash", Arguments = "-c \"route -n\"", RedirectStandardOutput = true, UseShellExecute = false, CreateNoWindow = true, } }; process.Start(); //这一行没用是他:Kernel IP routing table process.StandardOutput.ReadLine(); ////这一行没用是他:Destination Gateway Genmask Flags Metric Ref Use Iface process.StandardOutput.ReadLine(); while(!process.StandardOutput.EndOfStream) { var line = process.StandardOutput.ReadLine(); if(line.StartsWith("0.0.0.0")) { var match = Regex.Match(line, @"\w+$"); if(match.Success) { interfaceName = match.Value; break; } } else if(interfaceName == ) { var match = Regex.Match(line, @"\w+$"); if(match.Success) { interfaceName = match.Value; } } } if(interfaceName != ) { Console.WriteLine($ "正在使用的网络接口是:{interfaceName}"); } else { Console.WriteLine("未找到正在使用的网络接口。"); } return interfaceName;}
②、创建网络参数配置:
创建网络配置config = $@ "network:version: 2renderer: networkdethernets:{ interfaceName: dhcp4: noaddresses: [ { ipAddress } /{subnetMask}] routes: -to: default via: { gateway } nameservers: addresses: [ { dns }] ";}
string tempConfigPath = Path.Combine(Directory.GetCurrentDirectory(), "tempConfigPath.yaml");// 将网络配置写入临时文件File.WriteAllText(tempConfigPath, config);
// 使用sudo mv命令将临时文件移动到/etc/netplan/01-netcfg.yamlstring command = $ "sudo mv {tempConfigPath} /etc/netplan/01-netcfg.yaml && sudo netplan apply";var processInfo = new ProcessStartInfo("bash", "-c \"" + command + "\""){ RedirectStandardOutput = true, UseShellExecute = false, CreateNoWindow = true};var process = Process.Start(processInfo);process.WaitForExit();
;echo 'pi ALL=(ALL) NOPASSWD: /usr/bin/dotnet ' | sudo tee -a /etc/sudoers
sudo dotnet UpdateIPDemo.dll
bash RunApp.sh
以后有时间的话,可以再去摸索一下更复杂的效果

;编程不息、Bug不止、无Bug、无生活
;
改
bug
的冷静、编码的激情、完成后的喜悦、挖坑的激动 、填坑的兴奋;这也许就是屌丝程序员的乐趣吧;
今天就到这里吧;希望自己有动力一步一步坚持下去;生命不息,代码不止;大家抽空可以看看今天分享的效果,有好的意见和想法,可以在留言板随意留言;我看到后会第一时间回复大家,多谢大家的一直默默的关注和支持!
