Facebook
From Prince, 1 Month ago, written in Dart.
Embed
Download Paste or View Raw
Hits: 160
  1. import 'dart:io';
  2.  
  3. void main() async {
  4.   final ipConfig = WindowsIpConfig();
  5.   final ip = await ipConfig.getIp();
  6.   print("Your IPv4 Address: $ip");
  7. }
  8. // Get-WmiObject -Class Win32_NetworkAdapterConfiguration -Filter IPENABLED=TRUE | Select IPAddress
  9.  
  10. abstract class IpConfig {
  11.   final String kLoopbackAddress = "127.0.0.1";
  12.  
  13.   String get command;
  14.  
  15.   List<String> get arguments;
  16.  
  17.   String extractIP(String output);
  18.  
  19.   Future<String> getIp() async {
  20.     final prResult = await Process.run(command, arguments);
  21.  
  22.     if (prResult.exitCode == 0) {
  23.       return extractIP(prResult.stdout);
  24.     }
  25.     return "";
  26.   }
  27. }
  28.  
  29. class WindowsIpConfig extends IpConfig {
  30.   final _winIpRegExp = RegExp(r'\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b');
  31.   @override
  32.   List<String> get arguments => [
  33.         "-c",
  34.         "Get-WmiObject",
  35.         " -Class",
  36.         "Win32_NetworkAdapterConfiguration",
  37.         "-Filter",
  38.         "IPENABLED=TRUE",
  39.         "|",
  40.         "Select",
  41.         "IPAddress",
  42.       ];
  43.  
  44.   @override
  45.   String get command => "powershell";
  46.  
  47.   @override
  48.   String extractIP(String output) {
  49.     return _winIpRegExp.firstMatch(output)!.group(0)!;
  50.   }
  51. }
  52.