给出任何ipv4 IP地址(例如:10.24.13.91),应将其转换为ipv6地址(预期输出::: FFFF:0A18:0D5B) 如何编写将上述ipv4地址转换为ipv6地址的TCL脚本?
答案 0 :(得分:1)
ip
package in Tcllib在这里很有帮助,但不能完成全部工作。 (您已经完成了RFC 3513中定义的IPv4映射的IPv6地址。)
package require ip
proc IPv4toIPv6 {addr} {
if {![ip::is ipv4 $addr]} {
error "need an IPv4 address"
}
set raw [ip::toInteger $addr]
return [ip::normalize [format "::FFFF:%04x:%04x" [expr {$raw >> 16}] [expr {$raw & 0xFFFF}]]]
}
set v6 [IPv4toIPv6 10.24.13.91]
puts "Full address: $v6"
# Full address: 0000:0000:0000:0000:0000:ffff:0a18:0d5b
puts "Abbreviated: [ip::contract $v6]"
# Abbreviated: ::ffff:a18:d5b
向我看。
答案 1 :(得分:0)
proc IPv4toIPv6 { addr } {
if {![ip::is ipv4 $addr]} {
error "addr should be an IPv4 address"
}
set raw 0
set octets [lreverse [split $addr .]]
for { set i 0 } { $i < 4 } { incr i } {
set raw [expr { 256 ** $i * [lindex $octets $i ] + $raw }]
}
set ipv6 [ip::normalize [format "::FFFF:%04x:%04x" [expr {$raw >> 16}] [expr {$raw & 0xFFFF}]]]
return [ip::contract $ipv6]
}
只是一种解决方法。上面的逻辑将按预期适用于任何Ipv4 ip。 !!!!