K
K
kotjj2020-10-28 23:30:57
PowerShell
kotjj, 2020-10-28 23:30:57

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

In the output file, I have 4 lines with ip addresses of all adapters (interface index 1,2,3,4)
And you need the file to look like this:
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

Where is the bold font I need to insert the derived ip addresses from the script. As I understood from writing scripts, it is necessary to write each ip into a variable and insert it in the right place in the output file, but I don’t understand how to do this, help me figure it out

Answer the question

In order to leave comments, you need to log in

3 answer(s)
E
Eugene, 2020-10-29
@kotjj

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"

A
Andrew AT, 2020-10-29
@AAT666

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}

M
MaxKozlov, 2020-10-29
@MaxKozlov

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 question

Ask a Question

731 491 924 answers to any question