diff --git a/reinstall.sh b/reinstall.sh index 2ee7aa3..eabeb84 100644 --- a/reinstall.sh +++ b/reinstall.sh @@ -704,26 +704,39 @@ is_efi() { fi } +# TODO: 多网卡 单网卡多IP collect_netconf() { if is_in_windows; then - # TODO: - echo + echo_conf() { + grep -w "$1" <<<"$netconf" | awk '{ print $2 }' + } + curl -L $confhome/windows-get-netconf.ps1 -o /tmp/windows-get-netconf.ps1 + ps1="$(cygpath -w /tmp/windows-get-netconf.ps1)" + netconf=$( + powershell -nologo -noprofile -NonInteractive \ + -ExecutionPolicy bypass \ + -File "$ps1" | + sed 's/://' + ) + mac_addr=$(echo_conf MACAddress) + ipv4_addr=$(echo_conf IPAddress4) + ipv6_addr=$(echo_conf IPAddress6) + ipv4_gateway=$(echo_conf DefaultIPGateway4) + ipv6_gateway=$(echo_conf DefaultIPGateway6) else - # TODO: 多网卡 单网卡多IP nic_name=$(ip -o addr show scope global | head -1 | awk '{print $2}') mac_addr=$(ip addr show scope global | grep link/ether | head -1 | awk '{print $2}') ipv4_addr=$(ip -4 addr show scope global | grep inet | head -1 | awk '{print $2}') ipv6_addr=$(ip -6 addr show scope global | grep inet6 | head -1 | awk '{print $2}') - ipv4_gateway=$(ip -4 route show default dev $nic_name | awk '{print $3}') ipv6_gateway=$(ip -6 route show default dev $nic_name | awk '{print $3}') - - echo 1 $mac_addr - echo 2 $ipv4_addr - echo 3 $ipv4_gateway - echo 4 $ipv6_addr - echo 5 $ipv6_gateway fi + + echo 1 $mac_addr + echo 2 $ipv4_addr + echo 3 $ipv4_gateway + echo 4 $ipv6_addr + echo 5 $ipv6_gateway } install_grub_win() { diff --git a/trans.sh b/trans.sh index e040a65..e5fcd1e 100644 --- a/trans.sh +++ b/trans.sh @@ -653,7 +653,8 @@ create_cloud_init_network_config() { get_netconf_to mac_addr - # TODO: 没在windows下获取网络信息,先跳过cloud-init网络,不然网络不通 + # TODO: 没获取到mac/ipv4或者ipv6就先跳过cloud-init网络 + # 不然cloud-init配置文件有问题,网络不通 [ -z "$mac_addr" ] && return get_netconf_to ipv4_addr diff --git a/windows-get-netconf.ps1 b/windows-get-netconf.ps1 new file mode 100644 index 0000000..2703345 --- /dev/null +++ b/windows-get-netconf.ps1 @@ -0,0 +1,64 @@ +# 物理网卡 +foreach ($NetAdapter in Get-NetAdapter -Physical) { + $Name = $NetAdapter.Name + $MACAddress = $NetAdapter.MACAddress -replace '-', ':' + + # IP条目 + $IPEntries = (Get-NetIPAddress -InterfaceAlias $Name | + Where-Object { + $_.PrefixOrigin -match "RouterAdvertisement|DHCP|Manual" -and + $_.AddressState -eq "Preferred" + }) + + if (!$IPEntries) { + continue; + } + + # IPv4 + foreach ($IPEntry in $IPEntries) { + if ($IPEntry.AddressFamily -eq "IPv4") { + $IPAddress4 = $IPEntry.IPAddress + $PrefixLength4 = $IPEntry.PrefixLength + break + } + } + + # IPv6 + foreach ($IPEntry in $IPEntries) { + if ($IPEntry.AddressFamily -eq "IPv6") { + $IPAddress6 = $IPEntry.IPAddress + $PrefixLength6 = $IPEntry.PrefixLength + break + } + } + + # IPv4 网关 + foreach ($NetRoute in Get-NetRoute | Where-Object { + $_.InterfaceAlias -eq $Name -and + $_.DestinationPrefix -eq "0.0.0.0/0" + }) { + $DefaultIPGateway4 = $NetRoute.NextHop + break + } + + # IPv6 网关 + foreach ($NetRoute in Get-NetRoute | Where-Object { + $_.InterfaceAlias -eq $Name -and + $_.DestinationPrefix -eq "::/0" + }) { + $DefaultIPGateway6 = $NetRoute.NextHop + break + } + + $OutputObj = New-Object -Type PSObject -Property @{ + MACAddress = "$MACAddress".ToLower() # 和linux保持一致 + IPAddress4 = $(If ($IPAddress4) { "$IPAddress4/$PrefixLength4" }) + IPAddress6 = $(If ($IPAddress6) { "$IPAddress6/$PrefixLength6" }) + DefaultIPGateway4 = $DefaultIPGateway4 + DefaultIPGateway6 = $DefaultIPGateway6 + } + + # 按指定顺序输出 + $OutputObj | Select-Object MACAddress, IPAddress4, IPAddress6, DefaultIPGateway4, DefaultIPGateway6 + break +}