core: 不用 powershell 获取网络配置

This commit is contained in:
bin456789 2023-10-22 16:54:45 +08:00
parent 1604af1dac
commit ea3dc1ddcf
No known key found for this signature in database
GPG Key ID: EE301B386DE6C11B
2 changed files with 73 additions and 80 deletions

View File

@ -720,26 +720,83 @@ is_efi() {
fi
}
to_upper() {
tr '[:lower:]' '[:upper:]'
}
to_lower() {
tr '[:upper:]' '[:lower:]'
}
del_cr() {
sed 's/\r//g'
}
# TODO: 多网卡 单网卡多IP
collect_netconf() {
if is_in_windows; then
echo_conf() {
grep -w "$1" <<<"$netconf" | awk '{ print $2 }'
convert_net_str_to_array() {
config=$1
key=$2
var=$3
IFS=',' read -r -a "${var?}" <<<"$(grep "$key=" <<<"$config" | cut -d= -f2 | sed 's/[{}"]//g')"
}
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/://' |
sed 's/\r//' # 删除 windows \r 换行符
)
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)
# 部分机器精简了 powershell
# 所以不要用 powershell 获取网络信息
ids=$(wmic nic where "PhysicalAdapter=true and MACAddress is not null and PNPDeviceID like '%VEN_%&DEV_%'" get InterfaceIndex | del_cr | sed '1d')
for id in $ids; do
config=$(wmic nicconfig where "InterfaceIndex='$id'" get MACAddress,IPAddress,IPSubnet,DefaultIPGateway /format:list | del_cr)
# 排除 IP/子网/网关为空的
if grep -q '=$' <<<"$config"; then
continue
fi
mac_addr=$(grep "MACAddress=" <<<"$config" | cut -d= -f2 | to_lower)
convert_net_str_to_array "$config" IPAddress ips
convert_net_str_to_array "$config" IPSubnet subnets
convert_net_str_to_array "$config" DefaultIPGateway gateways
# IPv4
# shellcheck disable=SC2154
for ((i = 0; i < ${#ips[@]}; i++)); do
ip=${ips[i]}
subnet=${subnets[i]}
if [[ "$ip" = *.* ]]; then
cidr=$(ipcalc -b "$ip/$subnet" | grep Network: | cut -d/ -f2 | xargs)
ipv4_addr="$ip/$cidr"
break
fi
done
# IPv6
ipv6_type_list=$(cmd /c "chcp 437 & netsh interface ipv6 show address $id normal")
for ((i = 0; i < ${#ips[@]}; i++)); do
ip=${ips[i]}
cidr=${subnets[i]}
if [[ "$ip" = *:* ]]; then
ipv6_type=$(grep "$ip" <<<"$ipv6_type_list" | awk '{print $1}')
# 还有类型 Temporary不过有 Temporary 肯定还有 Public因此不用
if [ "$ipv6_type" = Public ] ||
[ "$ipv6_type" = Manual ]; then
ipv6_addr="$ip/$cidr"
break
fi
fi
done
# 网关
# shellcheck disable=SC2154
for gateway in "${gateways[@]}"; do
if [[ "$gateway" = *.* ]]; then
ipv4_gateway="$gateway"
elif [[ "$gateway" = *:* ]]; then
ipv6_gateway="$gateway"
fi
done
break
done
else
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}')

View File

@ -1,64 +0,0 @@
# 物理网卡
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
}