lych 回复于:2005-04-29 09:03:29 |
sed -n '2,5p' text|awk '{print $2" "$5}'
可不可以哦 |
platinum 回复于:2005-04-29 09:07:25 |
但是,消息是循环的
比如我要要看的是dhcpd.leases,显示其中的IP和MAC 原始资料如下 [quote:cb11ee5fd4] [root@CRIFST dhcp]# cat dhcpd.leases # All times in this file are in UTC (GMT), not your local timezone. This is # not a bug, so please don't ask about it. There is no portable way to # store leases in the local timezone, so please don't request this as a # feature. If this is inconvenient or confusing to you, we sincerely # apologize. Seriously, though - don't ask. # The format of this file is documented in the dhcpd.leases(5) manual page. # This lease file was written by isc-dhcp-V3.0.1 lease 192.168.103.198 { starts 5 2005/04/29 00:55:41; ends 2 2038/01/19 03:14:06; tstp 2 2038/01/19 03:14:06; binding state active; next binding state free; hardware ethernet 00:30:05:06:af:14; uid "\001\0000\005\006\257\024"; client-hostname "secend"; } lease 192.168.103.193 { starts 5 2005/04/29 00:58:07; ends 2 2038/01/19 03:14:06; tstp 2 2038/01/19 03:14:06; binding state active; next binding state free; hardware ethernet 00:0c:6e:55:f7:dd; uid "\001\000\014nU\367\335"; client-hostname "gaohangjun"; } lease 192.168.103.192 { starts 5 2005/04/29 00:59:00; ends 2 2038/01/19 03:14:06; binding state active; next binding state free; hardware ethernet 00:30:05:03:8a:08; uid "\001\0000\005\003\212\010"; client-hostname "jj"; } lease 192.168.102.199 { starts 5 2005/04/29 00:59:46; ends 2 2038/01/19 03:14:06; binding state active; next binding state free; hardware ethernet 00:0f:3d:21:8d:2a; uid "\001\000\017=!\215*"; client-hostname "DLA502"; } lease 192.168.102.199 { starts 5 2005/04/29 00:59:46; ends 5 2005/04/29 01:01:16; tstp 5 2005/04/29 01:01:16; binding state free; hardware ethernet 00:0f:3d:21:8d:2a; uid "\001\000\017=!\215*"; } [/quote:cb11ee5fd4] 只显示每段的 lease 和 hardware 那两行中的IP和MAC,并合并为一行输出 |
platinum 回复于:2005-04-29 09:21:02 |
目前已经用获得了这样的数据
[quote:f829701a51] [root@CRIFST dhcp]# grep -E "{|hard" dhcpd.leases lease 192.168.103.198 { hardware ethernet 00:30:05:06:af:14; lease 192.168.103.193 { hardware ethernet 00:0c:6e:55:f7:dd; lease 192.168.103.192 { hardware ethernet 00:30:05:03:8a:08; lease 192.168.102.199 { hardware ethernet 00:0f:3d:21:8d:2a; lease 192.168.102.199 { hardware ethernet 00:0f:3d:21:8d:2a; lease 192.168.102.199 { hardware ethernet 00:0f:3d:21:8d:2a; lease 192.168.102.199 { hardware ethernet 00:0f:3d:21:8d:2a; lease 192.168.102.199 { hardware ethernet 00:0f:3d:21:8d:2a; lease 192.168.102.199 { hardware ethernet 00:0f:3d:21:8d:2a; lease 192.168.102.199 { hardware ethernet 00:0f:3d:21:8d:2a; lease 192.168.102.199 { hardware ethernet 00:0f:3d:21:8d:2a; lease 192.168.102.199 { hardware ethernet 00:0f:3d:21:8d:2a; lease 192.168.102.199 { hardware ethernet 00:0f:3d:21:8d:2a; lease 192.168.102.199 { hardware ethernet 00:0f:3d:21:8d:2a; lease 192.168.102.199 { hardware ethernet 00:0f:3d:21:8d:2a; lease 192.168.102.199 { hardware ethernet 00:0f:3d:21:8d:2a; lease 192.168.102.199 { hardware ethernet 00:0f:3d:21:8d:2a; lease 192.168.102.199 { hardware ethernet 00:0f:3d:21:8d:2a; lease 192.168.102.199 { hardware ethernet 00:0f:3d:21:8d:2a; lease 192.168.102.199 { hardware ethernet 00:0f:3d:21:8d:2a; lease 192.168.102.199 { hardware ethernet 00:0f:3d:21:8d:2a; [/quote:f829701a51] |
platinum 回复于:2005-04-29 09:36:56 |
现有一个方法
[code:1:38b672cd72] grep -E "{|hardware" dhcpd.leases|awk '{if(NR%2!=0)ORS="";else ORS="\n";print}'|awk '{print $2,$6}'|sed 's/;//g'|sort [/code:1:38b672cd72] 执行结果 [quote:38b672cd72] 192.168.102.197 00:0f:3d:21:8d:30 192.168.102.197 00:0f:3d:21:8d:30 192.168.102.197 00:0f:3d:21:8d:30 192.168.102.197 00:0f:3d:21:8d:30 192.168.102.197 00:0f:3d:21:8d:30 192.168.102.199 00:0f:3d:21:8d:2a 192.168.102.199 00:0f:3d:21:8d:2a 192.168.102.199 00:0f:3d:21:8d:2a 192.168.102.199 00:0f:3d:21:8d:2a 192.168.102.199 00:0f:3d:21:8d:2a 192.168.102.199 00:0f:3d:21:8d:2a 192.168.102.200 00:e0:4c:e4:ff:26 192.168.102.200 00:e0:4c:e4:ff:26 192.168.103.192 00:30:05:03:8a:08 192.168.103.193 00:0c:6e:55:f7:dd 192.168.103.198 00:30:05:06:af:14 [/quote:38b672cd72] 但有时候有问题 [quote:38b672cd72] 192.168.102.197 00:0f:3d:21:8d:30 192.168.102.197 00:0f:3d:21:8d:30 192.168.102.197 00:0f:3d:21:8d:30 192.168.102.197 00:0f:3d:21:8d:30 192.168.102.197 00:0f:3d:21:8d:30 192.168.102.197 00:0f:3d:21:8d:30 192.168.102.197 00:0f:3d:21:8d:30 192.168.102.199 00:0f:3d:21:8d:2a 192.168.102.199 00:0f:3d:21:8d:2a 192.168.102.199 00:0f:3d:21:8d:2a 192.168.102.199 00:0f:3d:21:8d:2a 192.168.102.199 00:0f:3d:21:8d:2a 192.168.102.199 00:0f:3d:21:8d:2a 192.168.102.200 00:e0:4c:e4:ff:26 192.168.102.200 00:e0:4c:e4:ff:26 192.168.103.192 00:30:05:03:8a:08 192.168.103.193 00:0c:6e:55:f7:dd 192.168.103.194 192.168.103.194 00:11:5b:22:8f:ae 192.168.103.195 192.168.103.196 192.168.103.196 00:11:5b:22:8f:ae 192.168.103.197 192.168.103.198 00:30:05:06:af:14 192.168.103.200 ethernet ethernet ethernet ethernet ethernet ethernet ethernet [/quote:38b672cd72] 有问题的[b:38b672cd72]部分[/b:38b672cd72]原始资料 [quote:38b672cd72] lease 192.168.103.200 { starts 5 2005/04/29 01:37:54; ends 5 2005/04/29 01:37:54; binding state free; client-hostname "lenovo-2f31e43d"; } lease 192.168.103.197 { starts 5 2005/04/29 01:38:27; ends 5 2005/04/29 01:38:27; tstp 5 2005/04/29 01:38:27; binding state free; } lease 192.168.103.196 { starts 5 2005/04/29 01:38:38; ends 5 2005/04/29 01:38:38; tstp 5 2005/04/29 01:38:38; binding state free; } lease 192.168.103.195 { starts 5 2005/04/29 01:38:49; ends 5 2005/04/29 01:38:49; tstp 5 2005/04/29 01:38:49; binding state free; } lease 192.168.103.194 { starts 5 2005/04/29 01:39:00; ends 5 2005/04/29 01:39:00; tstp 5 2005/04/29 01:39:00; binding state free; } lease 192.168.102.197 { starts 5 2005/04/29 01:38:27; ends 5 2005/04/29 01:39:57; tstp 5 2005/04/29 01:39:57; binding state free; hardware ethernet 00:0f:3d:21:8d:30; uid "\001\000\017=!\2150"; } [/quote:38b672cd72] |
lych 回复于:2005-04-29 10:09:39 |
awk '/\{|(hardware)/' bb.txt|tr -d '{'|awk '{print $NF}'|tr '\n' '-'|tr ';' '\n'|sed 's/^[\-]//'
我提供一种方法,你试验一下! ps:我在tru64,ksh下是通过了。 看了你的有问题的资料,上面的是不合适的 |
platinum 回复于:2005-04-29 10:13:14 |
试验结果如下
[quote:c0e0582fe2] [root@CRIFST dhcp]# awk '/\{|(hardware)/' dhcpd.leases|tr -d '{'|awk '{print $NF}'|tr '\n' '-'|tr ';' '\n'|sed 's/^[\-]//' 192.168.103.198-00:30:05:06:af:14 192.168.103.193-00:0c:6e:55:f7:dd 192.168.103.192-00:30:05:03:8a:08 192.168.102.200-00:e0:4c:e4:ff:26 192.168.102.199-00:0f:3d:21:8d:2a 192.168.102.197-00:0f:3d:21:8d:30 192.168.102.200-00:e0:4c:e4:ff:26 192.168.102.197-00:0f:3d:21:8d:30 192.168.102.199-00:0f:3d:21:8d:2a 192.168.102.199-00:0f:3d:21:8d:2a 192.168.102.199-00:0f:3d:21:8d:2a 192.168.102.199-00:0f:3d:21:8d:2a 192.168.102.197-00:0f:3d:21:8d:30 192.168.102.199-00:0f:3d:21:8d:2a 192.168.102.197-00:0f:3d:21:8d:30 192.168.102.197-00:0f:3d:21:8d:30 192.168.102.197-00:0f:3d:21:8d:30 192.168.103.200-192.168.103.199-00:0d:87:4e:4a:ac 192.168.102.199-00:0f:3d:21:8d:2a 192.168.103.197-00:11:5b:22:8f:ae 192.168.102.197-00:0f:3d:21:8d:30 192.168.103.197-192.168.103.196-00:11:5b:22:8f:ae 192.168.103.196-192.168.103.195-00:11:5b:22:8f:ae 192.168.103.195-192.168.103.194-00:11:5b:22:8f:ae 192.168.103.194-192.168.103.191-00:11:5b:22:8f:ae 192.168.102.199-00:0f:3d:21:8d:2a 192.168.103.200-192.168.103.197-192.168.103.196-192.168.103.195-192.168.103.194-192.168.102.197-00:0f:3d:21:8d:30 192.168.102.199-00:0f:3d:21:8d:2a 192.168.102.199-00:0f:3d:21:8d:2a 192.168.102.199-00:0f:3d:21:8d:2a 192.168.102.197-00:0f:3d:21:8d:30 192.168.102.199-00:0f:3d:21:8d:2a 192.168.102.197-00:0f:3d:21:8d:30 192.168.102.197-00:0f:3d:21:8d:30 192.168.102.197-00:0f:3d:21:8d:30 192.168.103.199-00:0d:87:4e:4a:ac 192.168.102.199-00:0f:3d:21:8d:2a 192.168.103.200-00:00:00:00:00:00 192.168.103.190-00:00:00:00:00:00 192.168.102.199-00:0f:3d:21:8d:2a 192.168.102.199-00:0f:3d:21:8d:2a 192.168.102.199-00:0f:3d:21:8d:2a 192.168.102.199-00:0f:3d:21:8d:2a 192.168.102.199-00:0f:3d:21:8d:2a 192.168.102.199-00:0f:3d:21:8d:2a 192.168.102.199-00:0f:3d:21:8d:2a 192.168.102.197-00:0f:3d:21:8d:30 192.168.102.197-00:0f:3d:21:8d:30 192.168.102.199-00:0f:3d:21:8d:2a 192.168.102.197-00:0f:3d:21:8d:30 [/quote:c0e0582fe2] |
platinum 回复于:2005-04-29 10:37:21 |
[code:1:e4c7f5031c]
cat dhcpd.leases|awk 'BEGIN{ORS=RS="}"}/hard/&&/lease/{print}'|grep -E "{|hardware"|tr "\n" " "|sed 's/lease//g;s/lease//g;s/hardware ethernet//g;s/{//g;s/;//g'|xargs -n2|sort|uniq -c|awk '{print $2" "$3}' [/code:1:e4c7f5031c] [code:1:e4c7f5031c] cat dhcpd.leases|awk 'BEGIN{ORS=RS="}"}/hard/&&/lease/{print}'|grep -E "{|hardware"|awk '{if(NR%2!=0)ORS="";else ORS="\n";print}'|awk '{print $2,$6}'|sed 's/;//g'|sort|uniq -c|awk '{print $2" "$3}' [/code:1:e4c7f5031c] 两个都可以,这样就完美了,不过太长了 |
begincwcw 回复于:2005-04-29 10:59:09 |
试试这个:
nawk '{ gsub(";","") if($0~/lease/) printf("%s ", $2) if($0~/hardware/) printf("%s\n", $3) }' filename 在unix下通过。 |
platinum 回复于:2005-04-29 11:12:37 |
[quote:b0fc44ed01="begincwcw"]试试这个:
nawk '{ gsub(";","") if($0~/lease/) printf("%s ", $2) if($0~/hardware/) printf("%s\n", $3) }' filename 在unix下通过。[/quote:b0fc44ed01] 我没有nawk,用awk替换,测试结果:失败 效果和 awk '/\{|(hardware)/' bb.txt|tr -d '{'|awk '{print $NF}'|tr '\n' '-'|tr ';' '\n'|sed 's/^[\-]//' 类似,只不过IP中间是空格,不是“-” |
guangzongy 回复于:2005-04-29 11:19:14 |
对搂主说的有问题的纪录不是很清楚,好像是有一个纪录hardware对应行的纪录取不到有效数据.
写了一个 :oops: [code:1:c3fa92d135] awk 'BEGIN{i=1}{if ($1=="lease") IP=$2;if ($1=="hardware"&&$3!="") {print IP"-"substr($3,1,length ($3)-1)}}' yourfile [/code:1:c3fa92d135] :em02: |
mocou 回复于:2005-04-29 11:24:44 |
nawk和awk不同的,偶的系统上只有gawk,莫有nawk |
mocou 回复于:2005-04-29 11:39:42 |
[code:1:0fd61111a0]cat file|grep ^[^#]|awk '{gsub(";","");if($0~/lease/)printf("%s ", $2);if($0~/hardware/) printf("%s\n", $3)}'|awk '{if(NF>=2)print $(NF-1),$NF}'
192.168.103.198 00:30:05:06:af:14 192.168.103.193 00:0c:6e:55:f7:dd 192.168.103.192 00:30:05:03:8a:08 192.168.102.199 00:0f:3d:21:8d:2a 192.168.102.199 00:0f:3d:21:8d:2a[/code:1:0fd61111a0] 将楼上的楼上的。。。。。。修改了一次 |
寂寞烈火 回复于:2005-04-29 12:04:51 |
来一个累点的 :mrgreen:
[code:1:914104e179] grep -o '[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}' data>d1 grep -o '[0-9a-f]\{2\}\:[0-9a-f]\{2\}\:[0-9a-f]\{2\}\:[0-9a-f]\{2\}\:[0-9a-f]\{2\}\:[0-9a-f]\{2\}' data>d2 paste d1 d2 -d' '>d3;rm d1 d2 [/code:1:914104e179] |
platinum 回复于:2005-04-29 12:07:29 |
烈火大哥,你又给俺捣乱,再弄点sleep不更好 :mrgreen: |
寂寞烈火 回复于:2005-04-29 12:31:51 |
[quote:facbbfb0f0="platinum"]烈火大哥,你又给俺捣乱,再弄点sleep不更好 :mrgreen:[/quote:facbbfb0f0]
[color=red:facbbfb0f0][size=24:facbbfb0f0]sorry[/size:facbbfb0f0][/color:facbbfb0f0] |
begincwcw 回复于:2005-04-29 12:42:23 |
[quote:03dfe0a207="platinum"]类似,只不过IP中间是空格,不是“-”[/quote:03dfe0a207]
我在unix下的运行结果和烈火兄的运行结果是一样的!!! :shock: #sh i 192.168.103.198 00:30:05:06:af:14 192.168.103.193 00:0c:6e:55:f7:dd 192.168.103.192 00:30:05:03:8a:08 192.168.102.199 00:0f:3d:21:8d:2a 192.168.102.199 00:0f:3d:21:8d:2a awk '{ gsub(/;/,"") if($0~/lease/) printf("%s ", $2) if($0~/hardware/) printf("%s\n", $3) }' filename 也许是gsub错,把""改为//试试。 |
寂寞烈火 回复于:2005-04-29 13:30:08 |
[code:1:c49d228633]
awk '{gsub(/^ |;/,"")};/^lease/{printf $2" "};/^hard/{print $3}' file[/code:1:c49d228633] |
網中人 回复于:2005-04-29 14:04:24 |
my try:
$ echo `cat dhcpd.leases` | tr '}' '\n' | grep '^ lease .*hardware'| sed 's/lease \([^ ]*\).*ethernet \([^ ]*\)\; .*/\1 \2/' |
platinum 回复于:2005-04-29 16:02:19 |
[quote:ee9bd51651]
guangzongy 代码: awk 'BEGIN{i=1}{if ($1=="lease") IP=$2;if ($1=="hardware"&&$3!="") {print IP"-"substr($3,1,length ($3)-1)}}' yourfile [/quote:ee9bd51651] 执行结果如下 [quote:ee9bd51651] 192.168.104.200-0 192.168.104.198-0 192.168.104.197-0 192.168.104.195-0 192.168.104.193-0 192.168.104.192-0 192.168.104.191-0 192.168.104.190-0 192.168.103.183-0 192.168.103.190-0 192.168.103.200-0 192.168.103.199-0 192.168.103.198-0 192.168.103.197-0 192.168.103.196-0 192.168.103.193-0 192.168.103.192-0 192.168.103.191-0 192.168.103.189-0 192.168.103.188-0 192.168.103.187- 192.168.103.186-0 192.168.103.185-0 192.168.103.184-0 192.168.103.182-0 192.168.102.200-0 192.168.102.199-0 192.168.102.198-0 192.168.102.197-0 192.168.102.196-0 192.168.102.195-0 192.168.101.200-0 192.168.101.197-0 192.168.101.196-0 192.168.101.195-0 192.168.101.194-0 192.168.101.193-0 192.168.101.192-0 192.168.101.102-0 192.168.103.195-0 192.168.103.194-0 192.168.103.181-0 192.168.103.181-0 192.168.102.197-0 192.168.102.197-0 192.168.102.197-0 192.168.102.197-0 192.168.102.197-0 192.168.102.197-0 192.168.102.197-0 192.168.102.197-0 192.168.102.197-0 192.168.102.197-0 192.168.102.197-0 192.168.102.197-0 192.168.103.199-0 192.168.102.197-0 192.168.102.197-0 192.168.102.197-0 192.168.102.197-0 192.168.103.199-0 192.168.102.197-0 192.168.102.197-0 [/quote:ee9bd51651] [quote:ee9bd51651] begincwcw 我在unix下的运行结果和烈火兄的运行结果是一样的!!! #sh i 192.168.103.198 00:30:05:06:af:14 192.168.103.193 00:0c:6e:55:f7:dd 192.168.103.192 00:30:05:03:8a:08 192.168.102.199 00:0f:3d:21:8d:2a 192.168.102.199 00:0f:3d:21:8d:2a awk '{ gsub(/;/,"") if($0~/lease/) printf("%s ", $2) if($0~/hardware/) printf("%s\n", $3) }' filename 也许是gsub错,把""改为//试试。 [/quote:ee9bd51651] 改成//或者不改,结果一样,如下 [quote:ee9bd51651] not store The This 192.168.104.200 00:10:5c:f6:6c:10 192.168.104.198 00:0f:ea:b4:cb:82 192.168.104.197 00:0f:ea:b4:bd:e6 192.168.104.195 00:0f:ea:b3:82:fb 192.168.104.193 00:0f:ea:b2:ac:8b 192.168.104.192 00:0f:ea:b4:cb:32 192.168.104.191 00:04:23:8c:a4:6f 192.168.104.190 08:00:46:b7:dd:f8 192.168.103.183 00:0a:e4:2e:37:e9 192.168.103.190 00:00:00:00:00:00 192.168.103.200 00:00:00:00:00:00 192.168.103.199 00:0d:87:4e:4a:ac 192.168.103.198 00:30:05:06:af:14 192.168.103.197 00:0a:e4:2e:37:e9 192.168.103.196 00:0f:3d:48:d8:75 192.168.103.193 00:0c:6e:55:f7:dd 192.168.103.192 00:30:05:03:8a:08 192.168.103.191 00:11:5b:22:8f:ae 192.168.103.189 00:11:09:6e:b4:22 192.168.103.188 00:e0:4c:e5:06:6c 192.168.103.187 50:78:4c:48:a8:a1 192.168.103.186 00:11:2f:d4:84:6e 192.168.103.185 00:e0:81:25:22:d2 192.168.103.184 00:e0:4c:53:9d:f5 192.168.103.182 00:0f:ea:b2:ab:98 192.168.102.200 00:e0:4c:e4:ff:26 192.168.102.199 00:0f:3d:21:8d:2a 192.168.102.198 00:c0:f6:b0:b4:55 192.168.102.197 00:0f:3d:21:8d:30 192.168.102.196 00:0f:3d:83:1b:2c 192.168.102.195 00:11:11:b4:4b:82 192.168.101.200 00:0e:35:b5:0c:e0 192.168.101.197 00:0a:e6:68:cd:4e 192.168.101.196 00:e0:4c:46:14:2e 192.168.101.195 00:e0:4c:57:bd:61 192.168.101.194 00:0f:1f:b0:95:42 192.168.101.193 00:c0:f6:b0:b4:5c 192.168.101.192 00:11:11:b4:4c:06 192.168.101.102 00:e0:4c:46:14:2e 192.168.103.195 00:11:2f:51:bc:ce 192.168.103.195 192.168.103.194 00:11:2f:51:bc:ce 192.168.103.194 192.168.103.181 00:11:2f:51:bc:ce 192.168.103.181 00:11:2f:51:bc:ce 192.168.102.197 00:0f:3d:21:8d:30 192.168.102.197 00:0f:3d:21:8d:30 192.168.103.195 192.168.103.194 192.168.102.197 00:0f:3d:21:8d:30 192.168.102.197 00:0f:3d:21:8d:30 192.168.102.197 00:0f:3d:21:8d:30 192.168.102.197 00:0f:3d:21:8d:30 192.168.102.197 00:0f:3d:21:8d:30 192.168.102.197 00:0f:3d:21:8d:30 192.168.102.197 00:0f:3d:21:8d:30 192.168.102.197 00:0f:3d:21:8d:30 192.168.102.197 00:0f:3d:21:8d:30 192.168.102.197 00:0f:3d:21:8d:30 192.168.103.199 00:0d:87:4e:4a:ac 192.168.102.197 00:0f:3d:21:8d:30 192.168.102.197 00:0f:3d:21:8d:30 192.168.102.197 00:0f:3d:21:8d:30 192.168.102.197 00:0f:3d:21:8d:30 192.168.103.199 00:0d:87:4e:4a:ac 192.168.102.197 00:0f:3d:21:8d:30 192.168.102.197 00:0f:3d:21:8d:30 192.168.102.197 00:0f:3d:21:8d:30 192.168.102.197 00:0f:3d:21:8d:30 [/quote:ee9bd51651] [quote:ee9bd51651] 寂寞烈火 代码: awk '{gsub(/^ |;/,"")};/^lease/{printf $2" "};/^hard/{print $3}' file [/quote:ee9bd51651] 结果如下 [quote:ee9bd51651] 192.168.104.200 192.168.104.198 192.168.104.197 192.168.104.195 192.168.104.193 192.168.104.192 192.168.104.191 192.168.104.190 192.168.103.183 192.168.103.190 192.168.103.200 192.168.103.199 192.168.103.198 192.168.103.197 192.168.103.196 192.168.103.193 192.168.103.192 192.168.103.191 192.168.103.189 192.168.103.188 192.168.103.187 192.168.103.186 192.168.103.185 192.168.103.184 192.168.103.182 192.168.102.200 192.168.102.199 192.168.102.198 192.168.102.197 192.168.102.196 192.168.102.195 192.168.101.200 192.168.101.197 192.168.101.196 192.168.101.195 192.168.101.194 192.168.101.193 192.168.101.192 192.168.101.102 192.168.103.195 192.168.103.195 192.168.103.194 192.168.103.194 192.168.103.181 192.168.103.181 192.168.102.197 192.168.102.197 192.168.103.195 192.168.103.194 192.168.102.197 192.168.102.197 192.168.102.197 192.168.102.197 192.168.102.197 192.168.102.197 192.168.102.197 192.168.102.197 192.168.102.197 192.168.102.197 192.168.103.199 192.168.102.197 192.168.102.197 192.168.102.197 192.168.102.197 192.168.103.199 192.168.102.197 192.168.102.197 192.168.102.197 192.168.102.197 192.168.102.197 [/quote:ee9bd51651] [quote:ee9bd51651] 網中人 my try: $ echo `cat dhcpd.leases` | tr '}' '\n' | grep '^ lease .*hardware'| sed 's/lease \([^ ]*\).*ethernet \([^ ]*\)\; .*/\1 \2/' [/quote:ee9bd51651] 结果如下 [quote:ee9bd51651] -bash: $: command not found [/quote:ee9bd51651] 修改为 cat dhcpd.leases | tr '}' '\n' | grep '^ lease .*hardware'| sed 's/lease \([^ ]*\).*ethernet \([^ ]*\)\; .*/\1 \2/' 后,结果如下…… (无输出) |
寂寞烈火 回复于:2005-04-29 16:44:52 |
[b:fadafacaf9] [/b:fadafacaf9] |
網中人 回复于:2005-04-29 20:27:32 |
[code:1:12c1871fd1]
[netman@www tmp]$ cat dhcpd.leases # All times in this file are in UTC (GMT), not your local timezone. This is # not a bug, so please don't ask about it. There is no portable way to # store leases in the local timezone, so please don't request this as a # feature. If this is inconvenient or confusing to you, we sincerely # apologize. Seriously, though - don't ask. # The format of this file is documented in the dhcpd.leases(5) manual page. # This lease file was written by isc-dhcp-V3.0.1 lease 192.168.103.198 { starts 5 2005/04/29 00:55:41; ends 2 2038/01/19 03:14:06; tstp 2 2038/01/19 03:14:06; binding state active; next binding state free; hardware ethernet 00:30:05:06:af:14; uid "\001\0000\005\006\257\024"; client-hostname "secend"; } lease 192.168.103.193 { starts 5 2005/04/29 00:58:07; ends 2 2038/01/19 03:14:06; tstp 2 2038/01/19 03:14:06; binding state active; next binding state free; hardware ethernet 00:0c:6e:55:f7:dd; uid "\001\000\014nU\367\335"; client-hostname "gaohangjun"; } lease 192.168.103.192 { starts 5 2005/04/29 00:59:00; ends 2 2038/01/19 03:14:06; binding state active; next binding state free; hardware ethernet 00:30:05:03:8a:08; uid "\001\0000\005\003\212\010"; client-hostname "jj"; } lease 192.168.103.200 { starts 5 2005/04/29 01:37:54; ends 5 2005/04/29 01:37:54; binding state free; client-hostname "lenovo-2f31e43d"; } lease 192.168.103.197 { starts 5 2005/04/29 01:38:27; ends 5 2005/04/29 01:38:27; binding state free; } lease 192.168.103.196 { starts 5 2005/04/29 01:38:38; ends 5 2005/04/29 01:38:38; tstp 5 2005/04/29 01:38:38; binding state free; } lease 192.168.103.195 { starts 5 2005/04/29 01:38:49; ends 5 2005/04/29 01:38:49; tstp 5 2005/04/29 01:38:49; binding state free; } lease 192.168.103.194 { starts 5 2005/04/29 01:39:00; ends 5 2005/04/29 01:39:00; tstp 5 2005/04/29 01:39:00; binding state free; } lease 192.168.102.197 { starts 5 2005/04/29 01:38:27; ends 5 2005/04/29 01:39:57; tstp 5 2005/04/29 01:39:57; binding state free; hardware ethernet 00:0f:3d:21:8d:30; uid "\001\000\017=!\2150"; } lease 192.168.102.199 { starts 5 2005/04/29 00:59:46; ends 2 2038/01/19 03:14:06; binding state active; next binding state free; hardware ethernet 00:0f:3d:21:8d:2a; uid "\001\000\017=!\215*"; client-hostname "DLA502"; } lease 192.168.102.199 { starts 5 2005/04/29 00:59:46; ends 5 2005/04/29 01:01:16; tstp 5 2005/04/29 01:01:16; binding state free; hardware ethernet 00:0f:3d:21:8d:2a; uid "\001\000\017=!\215*"; } [netman@www tmp]$ echo `cat dhcpd.leases` | tr '}' '\n' | grep '^ lease .*hardware'| sed 's/lease \([^ ]*\).*ethernet \([^ ]*\)\; .*/\1 \2/' 192.168.103.193 00:0c:6e:55:f7:dd 192.168.103.192 00:30:05:03:8a:08 192.168.102.197 00:0f:3d:21:8d:30 192.168.102.199 00:0f:3d:21:8d:2a 192.168.102.199 00:0f:3d:21:8d:2a [/code:1:12c1871fd1] |
一梦如是 回复于:2005-04-29 20:43:59 |
上面这个……帅呆了~ |
寂寞烈火 回复于:2005-04-29 20:48:35 |
[quote:fa5085992e="一梦如是"]上面这个……帅呆了~[/quote:fa5085992e]
netman,我的偶像! 当然帅D酷B :mrgreen: |
lych 回复于:2005-04-29 21:06:12 |
偶像啊,帅呆了 |
mocou 回复于:2005-04-29 21:59:14 |
MP, :em05: :em05: :em05: 偶的太笨了 |
platinum 回复于:2005-04-29 22:18:37 |
[quote:08b931dc27="網中人"]
代码: …… …… …… [/quote:08b931dc27] 越来越看不懂了 :em17: |
icesummit 回复于:2005-04-29 23:03:54 |
不得不拜一个。 |
寂寞烈火 回复于:2005-04-30 00:47:10 |
[quote:5f0851c5fc="icesummit"]不得不拜一个。[/quote:5f0851c5fc]
你该换签名啦 :mrgreen: |
begincwcw 回复于:2005-05-08 17:22:02 |
[quote="網中人"
192.168.103.193 00:0c:6e:55:f7:dd 192.168.103.192 00:30:05:03:8a:08 192.168.102.197 00:0f:3d:21:8d:30 192.168.102.199 00:0f:3d:21:8d:2a 192.168.102.199 00:0f:3d:21:8d:2a ][/quote] 纲中人:因为我的系统是unix,不能运行你的程序,但我发现,你的最后运行结果为5条纪录, 但你可以用手工数数,应该为6条,用你的dhcpd.leases作为源文件,我的程序如下: $cat dhcpd.leases # All times in this file are in UTC (GMT), not your local timezone. This is # not a bug, so please don't ask about it. There is no portable way to # store leases in the local timezone, so please don't request this as a # feature. If this is inconvenient or confusing to you, we sincerely # apologize. Seriously, though - don't ask. # The format of this file is documented in the dhcpd.leases(5) manual page. # This lease file was written by isc-dhcp-V3.0.1 lease 192.168.103.198 { starts 5 2005/04/29 00:55:41; ends 2 2038/01/19 03:14:06; tstp 2 2038/01/19 03:14:06; binding state active; next binding state free; hardware ethernet 00:30:05:06:af:14; uid "\001\0000\005\006\257\024"; client-hostname "secend"; } lease 192.168.103.193 { starts 5 2005/04/29 00:58:07; ends 2 2038/01/19 03:14:06; tstp 2 2038/01/19 03:14:06; binding state active; next binding state free; hardware ethernet 00:0c:6e:55:f7:dd; uid "\001\000\014nU\367\335"; client-hostname "gaohangjun"; } lease 192.168.103.192 { starts 5 2005/04/29 00:59:00; ends 2 2038/01/19 03:14:06; binding state active; next binding state free; hardware ethernet 00:30:05:03:8a:08; uid "\001\0000\005\003\212\010"; client-hostname "jj"; } lease 192.168.103.200 { starts 5 2005/04/29 01:37:54; ends 5 2005/04/29 01:37:54; binding state free; client-hostname "lenovo-2f31e43d"; } lease 192.168.103.197 { starts 5 2005/04/29 01:38:27; ends 5 2005/04/29 01:38:27; binding state free; } lease 192.168.103.196 { starts 5 2005/04/29 01:38:38; ends 5 2005/04/29 01:38:38; tstp 5 2005/04/29 01:38:38; binding state free; } lease 192.168.103.195 { starts 5 2005/04/29 01:38:49; ends 5 2005/04/29 01:38:49; tstp 5 2005/04/29 01:38:49; binding state free; } lease 192.168.103.194 { starts 5 2005/04/29 01:39:00; ends 5 2005/04/29 01:39:00; tstp 5 2005/04/29 01:39:00; binding state free; } lease 192.168.102.197 { starts 5 2005/04/29 01:38:27; ends 5 2005/04/29 01:39:57; tstp 5 2005/04/29 01:39:57; binding state free; hardware ethernet 00:0f:3d:21:8d:30; uid "\001\000\017=!\2150"; } lease 192.168.102.199 { starts 5 2005/04/29 00:59:46; ends 2 2038/01/19 03:14:06; binding state active; next binding state free; hardware ethernet 00:0f:3d:21:8d:2a; uid "\001\000\017=!\215*"; client-hostname "DLA502"; } lease 192.168.102.199 { starts 5 2005/04/29 00:59:46; ends 5 2005/04/29 01:01:16; tstp 5 2005/04/29 01:01:16; binding state free; hardware ethernet 00:0f:3d:21:8d:2a; uid "\001\000\017=!\215*"; } 程序如下: awk ' { gsub(/;/,"") if($0~/^lease/) f = sprintf("%s ", $2) if($0~/^hardware/) s = sprintf("%s", $3) if($0~/}/ && f != "" && s != ""){ printf("%s %s \n", f, s) f = "" s = "" } }' dhcpd.leases 运行结果: 192.168.103.198 00:30:05:06:af:14 192.168.103.193 00:0c:6e:55:f7:dd 192.168.103.192 00:30:05:03:8a:08 192.168.102.197 00:0f:3d:21:8d:30 192.168.102.199 00:0f:3d:21:8d:2a 192.168.102.199 00:0f:3d:21:8d:2a |
網中人 回复于:2005-05-17 22:13:45 |
oh. bug found...
要將 ^lease 改為 lease 了... ^_^ |
woodie 回复于:2005-05-18 00:24:29 |
我想这个问题用awk比较适合。试试这个:
[code:1:52299c1574]awk -F"[ ;]" '/^lease|hard/{printf $(NF-1)}/{/{printf " "}/}/{print ""}'[/code:1:52299c1574] 上面将硬件地址缺失的作为空字段列出,如果需要可以进一步过滤掉这些行: [code:1:52299c1574]awk -F"[ ;]" '/^lease|hard/{printf $(NF-1)}/{/{printf " "}/}/{print ""}' ur-file | grep -v " $"[/code:1:52299c1574] |
woodie 回复于:2005-05-18 10:25:27 |
也可以这样:
[code:1:543a1a554f]awk -F"[ ;]" '/lease/{ip=$(NF-1)}/hard/{printf "%s %s\n", ip, $(NF-1)}'[/code:1:543a1a554f] |
云飞舞 回复于:2005-05-18 11:50:36 |
[quote:cb6e458b48="woodie"]
awk -F"[ ;]" '/lease/{ip=$(NF-1)}/hard/{printf "%s %s\n", ip, $(NF-1)}'[/quote:cb6e458b48] woodieGG能讲解一下吗 :em02: |
woodie 回复于:2005-05-18 12:19:59 |
[quote:86c8dcbc7a="云飞舞"]
woodieGG能讲解一下吗 :em02:[/quote:86c8dcbc7a] 非常乐意! 命令行选项-F"[ ;]" 告诉awk空格和分号都是字段分隔符,加入分号是为了后面提取mac地址时的方便。 /lease/{ip=$(NF-1)}表示当前行匹配lease时提取ip地址并暂存,而先不打印。 /hard/{printf "%s %s\n", ip, $(NF-1)}表示当前行匹配hard时打印暂存的ip地址和mac地址。如果没有hard这行的话是不会执行打印动作的。 不知道说清楚了没有? |
云飞舞 回复于:2005-05-18 12:28:25 |
谢谢GG,我明白啦 :em03:
我又演绎了一下,这样也能显示主机的hostname了 :em02: [code:1:924bbcd78d] cat /var/lib/dhcp/dhcpd.leases|awk -F"[ ;]" '/lease/{ip=$(NF-1)}/hard/{mac=$(NF-1)}/host/{printf "%s %s %s\n",ip,mac,$(NF-1)}' [/code:1:924bbcd78d] |
woodie 回复于:2005-05-18 15:05:29 |
举一反三呀,不错!:D
不过这样mac地址取不到的问题又出来了。你可以想想怎么解决,提示一下,可以用个标志变量判断是否要打印。 |
云飞舞 回复于:2005-05-18 15:37:21 |
我现在创建了一个文件,内容是这样的
[quote:d49b61603a] lease 192.168.102.187 { starts 3 2005/05/18 07:11:30; ends 2 2038/01/19 03:14:06; binding state active; next binding state free; hardware ethernet 00:07:95:ce:a0:55; uid "\001\000\007\225\316\240U"; client-hostname "billgates"; } lease 192.168.103.200 { starts 5 2005/04/29 01:37:54; ends 5 2005/04/29 01:37:54; binding state free; client-hostname "lenovo-2f31e43d"; } lease 192.168.103.197 { starts 5 2005/04/29 01:38:27; ends 5 2005/04/29 01:38:27; tstp 5 2005/04/29 01:38:27; binding state free; } lease 192.168.102.197 { starts 5 2005/04/29 01:38:27; ends 5 2005/04/29 01:39:57; tstp 5 2005/04/29 01:39:57; binding state free; hardware ethernet 00:0f:3d:21:8d:30; uid "\001\000\017=!\2150"; } lease 192.168.103.188 { starts 3 2005/05/18 07:11:37; ends 2 2038/01/19 03:14:06; binding state active; next binding state free; hardware ethernet 00:e0:4c:e5:06:6c; uid "\001\000\340L\345\006l"; client-hostname "cq"; } [/quote:d49b61603a] 文件名字是test,里面包含了一些错误的DHCP申请记录信息,为什么执行结果是这样的? [code:1:d49b61603a] # cat test|awk -F"[ ;]" '/lease/{ip=$(NF-1)}/hard/{mac=$(NF-1)}/host/{printf "%s %s %s\n",ip,mac,$(NF-1)}' 192.168.102.187 00:07:95:ce:a0:55 "billgates" 192.168.103.200 00:07:95:ce:a0:55 "lenovo-2f31e43d" 192.168.103.188 00:e0:4c:e5:06:6c "cq" [/code:1:d49b61603a] 难道说,是因为第一次mac已经有了记录,所以第二个的时候就仍然保留了上次的mac信息吗? |
begincwcw 回复于:2005-05-18 17:13:13 |
[quote:352879620e="云飞舞"]难道说,是因为第一次mac已经有了记录,所以第二个的时候就仍然保留了上次的mac信息吗?[/quote:352879620e]
试试这个: awk ' { gsub(/;/,"") if($0~/lease/) f = sprintf("%s ", $2) if($0~/hardware/) s = sprintf("%s", $3) if($0~/}/ && f != "" && s != ""){ printf("%s %s \n", f, s) f = "" s = "" } }' dhcpd.leases 运行结果: 192.168.102.187 00:07:95:ce:a0:55 192.168.102.197 00:0f:3d:21:8d:30 192.168.103.188 00:e0:4c:e5:06:6c |
網中人 回复于:2005-05-18 22:59:03 |
[quote:a09cbf80b8="woodie"]遖wk空格和分号都是字段分隔符,加入分号是为了后面提取mac地址时的方便。
/lease/{ip=$(NF-1)}表示当前行匹配lease时提取ip地址并暂存,而先不打印。 /hard/{printf "%s %s\n", ip, $(NF-1)}表示当前行匹配ha..........[/quote:a09cbf80b8] 呵.... 真妙! 讚一下! ^_^ |
woodie 回复于:2005-05-18 23:13:20 |
网版主和007版主两位都在线啊,看来夜猫子还不止我一个。hehe
网兄的许多大作都拜读过,小弟仰慕已久了呀!还有很多要向您学习呢。 :em09: |