2016 Update
This commit is contained in:
parent
948b86eafa
commit
dcf5d15da3
7965 changed files with 139854 additions and 31002 deletions
|
|
@ -0,0 +1,62 @@
|
|||
function Get-IpAddress
|
||||
{
|
||||
[CmdletBinding()]
|
||||
[OutputType([PSCustomObject])]
|
||||
Param
|
||||
(
|
||||
[Parameter(Mandatory=$true,
|
||||
ValueFromPipeline=$true,
|
||||
ValueFromPipelineByPropertyName=$true,
|
||||
Position=0)]
|
||||
$InputObject
|
||||
)
|
||||
|
||||
Begin
|
||||
{
|
||||
function Get-Address ([string]$Address)
|
||||
{
|
||||
if ($Address.IndexOf(".") -ne -1)
|
||||
{
|
||||
$Address, $port = $Address.Split(":")
|
||||
|
||||
[PSCustomObject]@{
|
||||
IPAddress = [System.Net.IPAddress]$Address
|
||||
Port = $port
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if ($Address.IndexOf("[") -ne -1)
|
||||
{
|
||||
[PSCustomObject]@{
|
||||
IPAddress = [System.Net.IPAddress]$Address
|
||||
Port = ($Address.Split("]")[-1]).TrimStart(":")
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
[PSCustomObject]@{
|
||||
IPAddress = [System.Net.IPAddress]$Address
|
||||
Port = $null
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Process
|
||||
{
|
||||
$InputObject | ForEach-Object {
|
||||
$address = Get-Address $_
|
||||
$bytes = ([System.Net.IPAddress]$address.IPAddress).GetAddressBytes()
|
||||
[Array]::Reverse($bytes)
|
||||
$i = 0
|
||||
$bytes | ForEach-Object -Begin {[bigint]$decimalIP = 0} `
|
||||
-Process {$decimalIP += [bigint]$_ * [bigint]::Pow(256, $i); $i++} `
|
||||
-End {[PSCustomObject]@{
|
||||
Address = $address.IPAddress
|
||||
Port = $address.Port
|
||||
Hex = "0x$($decimalIP.ToString('x'))"}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
$ipAddresses = "127.0.0.1","127.0.0.1:80","::1","[::1]:80","2605:2700:0:3::4713:93e3","[2605:2700:0:3::4713:93e3]:80" | Get-IpAddress
|
||||
$ipAddresses
|
||||
|
|
@ -0,0 +1 @@
|
|||
$ipAddresses[5].Address
|
||||
|
|
@ -0,0 +1 @@
|
|||
$ipAddresses | where {$_.Address.AddressFamily -eq "InterNetworkV6" -and $_.Port -ne $null}
|
||||
|
|
@ -1,35 +1,12 @@
|
|||
require 'socket'
|
||||
require 'ipaddr'
|
||||
|
||||
IP_ADDRESSES = ["127.0.0.1", "127.0.0.1:80",
|
||||
|
||||
TESTCASES = ["127.0.0.1", "127.0.0.1:80",
|
||||
"::1", "[::1]:80",
|
||||
"2605:2700:0:3::4713:93e3", "[2605:2700:0:3::4713:93e3]:80",
|
||||
"fe80::1%lo0", "1600 Pennsylvania Avenue NW"]
|
||||
output = []
|
||||
output << %w(String Address Port Family Hex Scope?)
|
||||
output << %w(------ ------- ---- ------ --- ------)
|
||||
"2605:2700:0:3::4713:93e3", "[2605:2700:0:3::4713:93e3]:80"]
|
||||
|
||||
# Parse _string_ for an IP address and optional port number. Returns
|
||||
# them in an Addrinfo object.
|
||||
def parse_addr(string)
|
||||
# Split host and port number from string.
|
||||
case string
|
||||
when /\A\[(?<address> .* )\]:(?<port> \d+ )\z/x # string like "[::1]:80"
|
||||
address, port = $~[:address], $~[:port]
|
||||
when /\A(?<address> [^:]+ ):(?<port> \d+ )\z/x # string like "127.0.0.1:80"
|
||||
address, port = $~[:address], $~[:port]
|
||||
else # string with no port number
|
||||
address, port = string, nil
|
||||
end
|
||||
|
||||
# Pass address, port to Addrinfo.getaddrinfo. It will raise SocketError if address or port is not valid.
|
||||
# IPAddr currently cannot handle ::1 notation, use Addrinfo instead
|
||||
ary = Addrinfo.getaddrinfo(address, port)
|
||||
|
||||
# An IP address is exactly one address.
|
||||
ary.size == 1 or raise SocketError, "expected 1 address, found #{ary.size}"
|
||||
ary.first
|
||||
end
|
||||
output = [%w(String Address Port Family Hex),
|
||||
%w(------ ------- ---- ------ ---)]
|
||||
|
||||
def output_table(rows)
|
||||
widths = []
|
||||
|
|
@ -38,27 +15,21 @@ def output_table(rows)
|
|||
rows.each {|row| puts format % row}
|
||||
end
|
||||
|
||||
family_hash = {Socket::AF_INET => "ipv4", Socket::AF_INET6 => "ipv6"}
|
||||
|
||||
IP_ADDRESSES.each do |string|
|
||||
begin
|
||||
addr = parse_addr(string)
|
||||
rescue SocketError
|
||||
output << [string, "illegal address", '','','','']
|
||||
else
|
||||
(cur_string ||= []) << string << addr.ip_address << addr.ip_port.to_s << family_hash[addr.afamily] # for output
|
||||
|
||||
# Show address in hexadecimal. We must unpack it from sockaddr string.
|
||||
if addr.ipv4?
|
||||
# network byte order "N"
|
||||
cur_string << "0x%08x" % IPAddr.new(addr.ip_address).hton.unpack('N') << ""
|
||||
elsif addr.ipv6?
|
||||
# 32 bytes for address, network byte order "N4"
|
||||
cur_string << "0x%032x" % IPAddr.new(addr.ip_address).to_i
|
||||
cur_string << (addr.ipv6_linklocal? ? ary[4] : "") # for Scope
|
||||
end
|
||||
output << cur_string
|
||||
TESTCASES.each do |str|
|
||||
case str # handle port; IPAddr does not.
|
||||
when /\A\[(?<address> .* )\]:(?<port> \d+ )\z/x # string like "[::1]:80"
|
||||
address, port = $~[:address], $~[:port]
|
||||
when /\A(?<address> [^:]+ ):(?<port> \d+ )\z/x # string like "127.0.0.1:80"
|
||||
address, port = $~[:address], $~[:port]
|
||||
else # string with no port number
|
||||
address, port = str, nil
|
||||
end
|
||||
|
||||
ip_addr = IPAddr.new(address)
|
||||
family = "IPv4" if ip_addr.ipv4?
|
||||
family = "IPv6" if ip_addr.ipv6?
|
||||
|
||||
output << [str, ip_addr.to_s, port.to_s, family, ip_addr.to_i.to_s(16)]
|
||||
end
|
||||
|
||||
output_table output
|
||||
output_table(output)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue