mirror of
https://github.com/bin456789/reinstall.git
synced 2025-01-19 04:49:13 +08:00
core: 完成 windows 下获取 ip
This commit is contained in:
parent
859ff9c5bf
commit
f9937a0660
33
reinstall.sh
33
reinstall.sh
@ -704,26 +704,39 @@ is_efi() {
|
|||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# TODO: 多网卡 单网卡多IP
|
||||||
collect_netconf() {
|
collect_netconf() {
|
||||||
if is_in_windows; then
|
if is_in_windows; then
|
||||||
# TODO:
|
echo_conf() {
|
||||||
echo
|
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
|
else
|
||||||
# TODO: 多网卡 单网卡多IP
|
|
||||||
nic_name=$(ip -o addr show scope global | head -1 | awk '{print $2}')
|
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}')
|
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}')
|
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}')
|
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}')
|
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}')
|
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
|
fi
|
||||||
|
|
||||||
|
echo 1 $mac_addr
|
||||||
|
echo 2 $ipv4_addr
|
||||||
|
echo 3 $ipv4_gateway
|
||||||
|
echo 4 $ipv6_addr
|
||||||
|
echo 5 $ipv6_gateway
|
||||||
}
|
}
|
||||||
|
|
||||||
install_grub_win() {
|
install_grub_win() {
|
||||||
|
3
trans.sh
3
trans.sh
@ -653,7 +653,8 @@ create_cloud_init_network_config() {
|
|||||||
|
|
||||||
get_netconf_to mac_addr
|
get_netconf_to mac_addr
|
||||||
|
|
||||||
# TODO: 没在windows下获取网络信息,先跳过cloud-init网络,不然网络不通
|
# TODO: 没获取到mac/ipv4或者ipv6就先跳过cloud-init网络
|
||||||
|
# 不然cloud-init配置文件有问题,网络不通
|
||||||
[ -z "$mac_addr" ] && return
|
[ -z "$mac_addr" ] && return
|
||||||
|
|
||||||
get_netconf_to ipv4_addr
|
get_netconf_to ipv4_addr
|
||||||
|
64
windows-get-netconf.ps1
Normal file
64
windows-get-netconf.ps1
Normal file
@ -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
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user