gpt4 book ai didi

linux - 使用脚本获取机器的 IP 地址和主机名

转载 作者:太空宇宙 更新时间:2023-11-04 04:20:31 24 4
gpt4 key购买 nike

我想在启动机器时获取机器的 IP 地址和主机名。我已经在 vmware 上安装了 3 个具有动态 IP 的虚拟机。其形式: xxxx.xxxx.xxxx.xxxx。主机名。

最佳答案

尝试获取Ipdata函数:

Function Get-IPData {
#this function assumes admin credentials
[cmdletBinding()]
Param(
[Parameter(Position=0,ValueFromPipeline=$True,ValueFromPipelineByPropertyName=$True)]
[ValidateNotNullOrEmpty()]
[Alias("name")]
[string[]]$computername=$env:computername
)

Process {
ForEach ($computer in $computername) {
Write-Verbose "Querying $($computer.ToUpper())"
Try
{
#get NICS that are IP and DHCP enabled
Get-WMIObject -Class win32_networkadapterconfiguration -computername $Computer `
-Filter "IPEnabled='TRUE' AND DHCPEnabled='TRUE'" -ErrorAction "Stop" |
Select Description,DNSHostname,
@{Name="IPAddress";Expression={$_.IPAddress[0]}},
@{Name="SubnetMask";Expression={$_.IPSubnet[0]}},
@{Name="DefaultGateway";Expression={$_.DefaultIPGateway[0]}},DNSDomain,
@{Name="PrimaryDNS";Expression={$_.DNSServerSearchOrder[0]}},DHCPServer,
@{Name="DHCPLease";Expression={$_.ConvertToDateTime($_.DHCPLeaseObtained)}},
@{Name="DHCPExpires";Expression={$_.ConvertToDateTime($_.DHCPLeaseExpires)}},
@{Name="DHCPTimeToLive";Expression={ $_.ConvertToDateTime($_.DHCPLeaseExpires) - (Get-Date)}},
MACAddress,
@{Name="Speed";Expression={
#use an Associators Of query to get the NIC
$nic=Get-WmiObject -query "associators of {Win32_NetworkAdapterConfiguration.Index=$($_.index)}" -computername $computer
$nic.Speed
}}
} #close Try
Catch
{
Write-Warning "Failed to retrieve IP configuration from $($computer.ToUpper())"
Write-Warning $_.Exception.Message

} #close Catch
} #close ForEach
} #close Process
} #end function

关于linux - 使用脚本获取机器的 IP 地址和主机名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13657245/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com