Answer the question
In order to leave comments, you need to log in
How to display the ip address of interfaces in powershell and insert it into a file?
Hello, please help me write a script that will parse the ip addresses of windows network interfaces and output them to the configuration file, I found the first part of the script something like this:
(gwmi Win32_NetworkAdapterConfiguration | ? { $_.IPAddress -ne $null }).ipaddress | Out-File file.txt
auth none
log
socks -n -a -i127.0.0.1 -e 10.0.0.* -p8001
socks -n -a -i127.0.0.1 -e 10.0.0.* -p8002
socks -n -a -i127 .0.0.1 -e 10.0.0.* -p8003
socks -n -a -i127.0.0.1 -e 10.0.0.* -p8004
Answer the question
In order to leave comments, you need to log in
Greetings, I threw in such a script, it seems to do what it needs, but I didn’t quite understand with the interface indexes, you need to have -p800* where * is the index of the interface with IP? if so, can I correct the script later, or can you do it yourself and the script should only pull active interfaces with an IP address? I did so, if you need to write differently, I will correct it. And I took the IP address only IPv4 if you need IPv6 also write.
$Names = Get-NetAdapter | Where-Object {$_.status -eq "Up"}
$AllIP = Foreach ($Name in $Names)
{(Get-NetIPAddress | where-object {$_.InterfaceAlias -eq $name.name}).IPv4Address}
$toFile = @("auth none","log")
$n = 1
$toFile += foreach ($IP in $AllIP)
{if ($IP -ne $null)
{"socks -n -a -i127.0.0.1 -e$ip -p800$n"
$n +=1}}
$toFile | Out-File "C:\test\test.txt"
It's not very clear about interface indexes - are they always just like that? 1,2,3, 4?! Maybe they are taken from the system after all?..
And about the IP addresses of interfaces - only 1 address per interface?..
If so, then:
"auth none","log" | Out-File файл.txt
$a=1
gwmi Win32_NetworkAdapterConfiguration -Filter IPEnabled=$true | %{Out-File файл.txt -InputObject "socks -n -a -i127.0.0.1 -e$($_.ipaddress) -p800$a" -Append;++$a}
It's not clear in what order it should go. In accordance with the interface numbers or as you like.
And what to do with IPV6 if any.
But in general, somehow.
# 1 вариант получения адресов, в конце отфильтровываем ipv6
$ip = (gwmi Win32_NetworkAdapterConfiguration -Filter IPEnabled=$true).IPAddress -match '\d+\.\d+\.'
# 2 вариант получения адресов, отфильтровываем ipv6 и 127.0.0.1
$ip = (Get-NetIPAddress | ? {$_. AddressFamily -eq 'IPv4' -and $_.IPAddress -ne '127.0.0.1' }).IPAddress
# Формирование строки
$port = 8001
$lines = $ip | Foreach-Object {
"socks -n -a -i127.0.0.1 -e$_ -p$port"
$port++
}
# Вывод в файл
'auth none', 'log', $lines | Out-File -Encoding Ascii -FilePath 'd:\111'
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question