使用TCL语法将InternetGatewayDevice.LANDevice.1.Hosts.Host.2.IPAddress 192.168.254.10中的IP地址取出
时间: 2024-03-11 18:49:11 浏览: 113
在TCL中,可以使用正则表达式来匹配IP地址并取出。下面是一个例子:
```
set str "InternetGatewayDevice.LANDevice.1.Hosts.Host.2.IPAddress 192.168.254.10"
set ip [regexp -inline {\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}} $str]
puts $ip
```
运行上述代码,输出结果为:
```
192.168.254.10
```
其中,正则表达式`\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}`表示匹配IP地址,`\d{1,3}`表示匹配1到3位数字,`\.`表示匹配点号(需要转义),最终使用`-inline`参数将匹配到的字符串以列表形式返回,取出第一个元素即为IP地址。
相关问题
使用TCL语法将InternetGatewayDevice.LANDevice.1.Hosts.Host.1.IPAddress 192.168.254.100 InternetGatewayDevice.LANDevice.1.Hosts.Host.1.AddressSource Static InternetGatewayDevice.LANDevice.1.Hosts.Host.1.LeaseTimeRemaining 0 InternetGatewayDevice.LANDevice.1.Hosts.Host.1.MACAddress 28:d2:44:ae:2c:95 InternetGatewayDevice.LANDevice.1.Hosts.Host.1.HostName unknown28d244ae2c95 InternetGatewayDevice.LANDevice.1.Hosts.Host.1.InterfaceType Ethernet InternetGatewayDevice.LANDevice.1.Hosts.Host.1.Layer2Interface InternetGatewayDevice.LANDevice.1.LANEthernetInterfaceConfig.4 InternetGatewayDevice.LANDevice.1.Hosts.Host.1.Active 1 InternetGatewayDevice.LANDevice.1.Hosts.Host.1.X_0000C5_LastActive Thu Jan 1 00:20:53 1970 InternetGatewayDevice.LANDevice.1.Hosts.Host.1.IPv6AddressNumberOfEntries 0 InternetGatewayDevice.LANDevice.1.Hosts.Host.2.IPAddress 192.168.254.10 InternetGatewayDevice.LANDevice.1.Hosts.Host.2.AddressSource DHCP InternetGatewayDevice.LANDevice.1.Hosts.Host.2.LeaseTimeRemaining 2591999 InternetGatewayDevice.LANDevice.1.Hosts.Host.2.MACAddress 6c:63:9c:08:24:62 InternetGatewayDevice.LANDevice.1.Hosts.Host.2.HostName x5042-2460 InternetGatewayDevice.LANDevice.1.Hosts.Host.2.InterfaceType 802.11 InternetGatewayDevice.LANDevice.1.Hosts.Host.2.Layer2Interface InternetGatewayDevice.LANDevice.1.WLANConfiguration.8 InternetGatewayDevice.LANDevice.1.Hosts.Host.2.Active 1 InternetGatewayDevice.LANDevice.1.Hosts.Host.3.Active 0 InternetGatewayDevice.LANDevice.1.Hosts.Host.3.AddressSource DHCP中的IP地址192.168.254.10取出
set hosts [list {InternetGatewayDevice.LANDevice.1.Hosts.Host.1.IPAddress 192.168.254.100 InternetGatewayDevice.LANDevice.1.Hosts.Host.1.AddressSource Static InternetGatewayDevice.LANDevice.1.Hosts.Host.1.LeaseTimeRemaining 0 InternetGatewayDevice.LANDevice.1.Hosts.Host.1.MACAddress 28:d2:44:ae:2c:95 InternetGatewayDevice.LANDevice.1.Hosts.Host.1.HostName unknown28d244ae2c95 InternetGatewayDevice.LANDevice.1.Hosts.Host.1.InterfaceType Ethernet InternetGatewayDevice.LANDevice.1.Hosts.Host.1.Layer2Interface InternetGatewayDevice.LANDevice.1.LANEthernetInterfaceConfig.4 InternetGatewayDevice.LANDevice.1.Hosts.Host.1.Active 1 InternetGatewayDevice.LANDevice.1.Hosts.Host.1.X_0000C5_LastActive Thu Jan 1 00:20:53 1970 InternetGatewayDevice.LANDevice.1.Hosts.Host.1.IPv6AddressNumberOfEntries 0} {InternetGatewayDevice.LANDevice.1.Hosts.Host.2.IPAddress 192.168.254.10 InternetGatewayDevice.LANDevice.1.Hosts.Host.2.AddressSource DHCP InternetGatewayDevice.LANDevice.1.Hosts.Host.2.LeaseTimeRemaining 2591999 InternetGatewayDevice.LANDevice.1.Hosts.Host.2.MACAddress 6c:63:9c:08:24:62 InternetGatewayDevice.LANDevice.1.Hosts.Host.2.HostName x5042-2460 InternetGatewayDevice.LANDevice.1.Hosts.Host.2.InterfaceType 802.11 InternetGatewayDevice.LANDevice.1.Hosts.Host.2.Layer2Interface InternetGatewayDevice.LANDevice.1.WLANConfiguration.8 InternetGatewayDevice.LANDevice.1.Hosts.Host.2.Active 1} {InternetGatewayDevice.LANDevice.1.Hosts.Host.3.Active 0 InternetGatewayDevice.LANDevice.1.Hosts.Host.3.AddressSource DHCP中的IP地址192.168.254.10取出}]
foreach host $hosts {
if {[lindex [split $host]} 1] == "192.168.254.10"} {
set addressSource [lindex [split $host]} 3]
break
}
}
puts $addressSource ;# 输出 DHCP
使用TCL语法将InternetGatewayDevice.LANDevice.1.Hosts.Host.1.AddressSource Static InternetGatewayDevice.LANDevice.1.Hosts.Host.2.AddressSource DHCP中含有DHCP的一行匹配取出
可以使用TCL中的正则表达式来匹配字符串中含有"DHCP"的行,代码如下:
```
set str "InternetGatewayDevice.LANDevice.1.Hosts.Host.1.AddressSource Static InternetGatewayDevice.LANDevice.1.Hosts.Host.2.AddressSource DHCP"
set lines [split $str "\n"]
foreach line $lines {
if {[regexp {DHCP} $line]} {
puts $line
}
}
```
运行结果为:
```
InternetGatewayDevice.LANDevice.1.Hosts.Host.2.AddressSource DHCP
```
阅读全文