class Inspec::Resources::WindowsHostProvider
@see blogs.technet.com/b/heyscriptingguy/archive/2014/03/19/creating-a-port-scanner-with-windows-powershell.aspx<br>@see blogs.technet.com/b/josebda/archive/2015/04/18/windows-powershell-equivalents-for-common-networking-commands-ipconfig-ping-nslookup.aspx<br>TODO: UDP is not supported yey, we need a custom ps1 script to add udp support
Windows
def ping(hostname, port = nil, _proto = nil)
def ping(hostname, port = nil, _proto = nil) # ICMP: Test-NetConnection www.microsoft.com # TCP and port: Test-NetConnection -ComputerName www.microsoft.com -RemotePort 80 request = "Test-NetConnection -ComputerName #{hostname} -WarningAction SilentlyContinue" request += " -RemotePort #{port}" unless port.nil? request += "| Select-Object -Property ComputerName, TcpTestSucceeded, PingSucceeded | ConvertTo-Json" cmd = inspec.command(request) begin ping = JSON.parse(cmd.stdout) rescue JSON::ParserError => _e return {} end { success: port.nil? ? ping["PingSucceeded"] : ping["TcpTestSucceeded"] } end
def resolve(hostname)
def resolve(hostname) addresses = [] # -Type A is the DNS query for IPv4 server Address. cmd = inspec.command("Resolve-DnsName –Type A #{hostname} | ConvertTo-Json") begin resolve_ipv4 = JSON.parse(cmd.stdout) rescue JSON::ParserError => _e return nil end resolve_ipv4 = resolve_ipv4.inject(:merge) if resolve_ipv4.is_a?(Array) # Append the ipv4 addresses resolve_ipv4.each_value do |ip| matched = ip.to_s.chomp.match(Resolv::IPv4::Regex) next if matched.nil? || addresses.include?(matched.to_s) addresses << matched.to_s end # -Type AAAA is the DNS query for IPv6 server Address. cmd = inspec.command("Resolve-DnsName –Type AAAA #{hostname} | ConvertTo-Json") begin resolve_ipv6 = JSON.parse(cmd.stdout) rescue JSON::ParserError => _e return nil end resolve_ipv6 = resolve_ipv6.inject(:merge) if resolve_ipv6.is_a?(Array) # Append the ipv6 addresses resolve_ipv6.each_value do |ip| matched = ip.to_s.chomp.match(Resolv::IPv6::Regex) next if matched.nil? || addresses.include?(matched.to_s) addresses << matched.to_s end addresses end