[英]python unpack buffer data
我在半径中使用rlm_python
模块,它以十六进制或二进制格式从coovachilli获取DHCP位置option82
。
将其作为字符串捕获显示为此值\\001\\027\\002\\025\\001+\\001\\024
,但查看python显示的值被截断,因为option82
包含以TLVs-type,length,values
编码的suboptions
TLVs-type,length,values
表示字段以0x01(circuit ID, per RFC 3046)
类型0x01(circuit ID, per RFC 3046)
开头,后跟一个字节长度。
知道如何抓住这个并正确解开选项吗?
我已经使用struct.unpack
解压缩了字符串但是没有意义struct.unpack
因为它没有告诉option82
字段中的压缩suboptions
option82
。
Python 2.4.3 (#1, May 5 2011, 16:39:10)
[GCC 4.1.2 20080704 (Red Hat 4.1.2-50)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from struct import *
>>> unpack('=hhl',"\001\027\002\025\001+\001\024" )
(5889, 5378, 335620865)
有任何想法吗?
更新:
Coovachilli是用--locationopt82
编译的,它将位置作为属性发送,类似这样......
rad_recv: Accounting-Request packet from host 10.66.53.49 port 53178, id=101, length=342
ChilliSpot-Version = "1.3.0"
ChilliSpot-Acct-View-Point = ChilliSpot-Client-View-Point
Event-Timestamp = "Apr 18 2013 11:59:16 BST"
User-Name = "3C-D0-F8-4A-05-68"
Acct-Input-Octets = 0
Acct-Output-Octets = 22851
Acct-Input-Gigawords = 0
Acct-Output-Gigawords = 0
Acct-Input-Packets = 0
Acct-Output-Packets = 370
Acct-Session-Time = 5401
ChilliSpot-Session-State = Authorized
Acct-Status-Type = Interim-Update
Acct-Session-Id = "516fbceb00000002"
Framed-IP-Address = 10.75.33.46
NAS-Port-Type = Wireless-802.11
NAS-Port = 2
NAS-Port-Id = "00000002"
Calling-Station-Id = "3C-D0-F8-4A-05-68"
Called-Station-Id = "00-50-56-B7-66-00"
NAS-IP-Address = 10.75.32.7
ChilliSpot-Location = "\001\030\002\026\001+\001\024"
ChilliSpot-Location-Change-Count = 15
NAS-Identifier = "VLAN299-REGENT"
WISPr-Location-ID = "isocc=GR,cc=44,ac=01200,network=mywifi,my_Network_regent"
WISPr-Location-Name = "REGENT"
freeradius有rlm_python
模块,用于查找记帐请求
def accounting(p):
file = open("/tmp/test.log","a")
username = None
chillilocation = None
output = StringIO.StringIO()
for t in p:
if t[0] == 'User-Name':
username = t[1]
elif t[0] == 'ChilliSpot-Location':
chillilocation = t[1]a
output.write(t[1])
content = output.getvalue()
file.write("I am being called in radius accouting section as %s and location is %s \n" % (username,content))
file.close()
print "---Accounting---"
radiusd.radlog(radiusd.L_INFO, '*** radlog call in accounting (0) ***')
print
print p
return radiusd.RLM_MODULE_OK
我已经尝试将ChilliSpot-Location存储在string
和stringIO
,使用struct解压缩但无效,它看起来像TLV格式...
任何想法如何剥离它?
解包不会解包为“有意义”的值。 它将字符串解压缩为一系列数字字段,以便每个字段的大小由格式字符串的每个字符指定(字母前面的数字是乘数)。
unpack不应该使用八位字节大小的字段吗?
>>> unpack('= 8B',“\\ 001 \\ 027 \\ 002 \\ 025 \\ 001 + \\ 001 \\ 024”)
(1,23,2,21,1,43,1,20)
#或者'+'可能是分隔符(43):
>>> unpack('= = 6Bh',“\\ 001 \\ 027 \\ 002 \\ 025 \\ 001 + \\ 001 \\ 024”)
(1,23,2,21,1,43,5121)
第一个字节可能是电路子选项代码(unpack()[0] == 1),其大小为23,这意味着我们没有得到整个子选项值。 但是我们也可能只包含大小== 10或8字节内容的子选项的包含值。
我不太确定选项82应该包含可读字符串,尽管“ChilliSpot-Location”可以。 RFC3046表示电路子选项应包含“路由器接口号”,端口号和其他数值等内容。 但rad_recv的属性真的来自82选项吗?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.