把Perl代码改成python

PowerPython 2012-02-29 10:27:57
在网上找扫描MAC的PYTHON代码没找到(不用SENDARP的),但找到了PERL代码,怎样改成PYTHON的,请教高手。
use Socket;
$host=$ARGV[0];
$packhost=inet_aton($host);
$port=137;
$str="\x01\x7A\x00\x10\x00\x01\x00\x00\x00\x00\x00\x00\x20\x43\x4B\x41\x41\x41\x41\x41\x41\x41\x41\x41\x41\x41\x41\x41\x41\x41\x41\x41\x41\x41\x41\x41\x41\x41\x41\x41\x41\x41\x41\x41\x41\x00\x00\x21\x00\x01";
$sin=sockaddr_in($port,$packhost);
socket(TARGET,PF_INET,SOCK_DGRAM,getprotobyname('UDP'))||die 'sock error';
send(TARGET,$str,0,$sin) ||die('send fail');
recv(TARGET,$buffer,10000,undef);
$head=substr($buffer,50,8);
@char=unpack('C*',$head);
#print "head:";
#print @char;
#print "\n";
$body=substr($buffer,57);
open LOG,">>137.log";
print LOG $body;
$body=~s/\x84\x00/\x84\n/g;
$body=~s/\x04\x00/\x04\n/g;
my @result=split(/\n/,$body);
my @type;
for($i=0;$i<@result;$i++)
{
$type[$i]=substr($result[$i],length($result[$i])-1,1)
}
#print @type;
chop(@result);

my @id;
for($i=0;$i<@result;$i++)
{
$id[$i]=substr($result[$i],length($result[$i])-1,1)
}
#print @id;
chop(@result);

$mac=pop(@result);
#print join("\n",@result);
print "IP---------->$host\n";
print " Name Type Status\n";
print "---------------------------------------------\n";
for($i=0;$i<@result;$i++)
{

@tempid=unpack('C*',$id[$i]);
@tempType=unpack('C*',$type[$i]);
if($tempType[0]==4)
{
printf(" %15s<%2X> UNIQUE Registered",$result[$i],$tempid[0]);
}
else
{
printf(" %15s<%2X> GROUP Registered",$result[$i],$tempid[0]);
}
print "\n";


}
$mac=substr($mac,0,6);
@mac=unpack('C*',$mac);
foreach $m (@mac)
{
$m=sprintf("%X",$m);
}
print "\n MAC Address =".join("-",@mac);

...全文
236 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
kingwmj78 2012-03-03
  • 打赏
  • 举报
回复
竞然短了这么多,是不是功能不全啊.
PowerPython 2012-03-02
  • 打赏
  • 举报
回复
自己努力了一下,也能用。
def getmac(ip):
import socket

port = 137
ns = "\x01\x7A\x00\x10\x00\x01\x00\x00\x00\x00\x00\x00\x20\x43\x4B"
ns += "\x41\x41\x41\x41\x41\x41\x41\x41\x41\x41\x41\x41\x41\x41\x41"
ns += "\x41\x41\x41\x41\x41\x41\x41\x41\x41\x41\x41\x41\x41\x41\x41"
ns += "\x00\x00\x21\x00\x01"

s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)

s.connect((ip, port))

s.sendall(ns)
s.settimeout(0.01)
try:
buf = s.recv(1024)
n = ord(buf[56])
macbuf = buf[57+18*n:57+18*n+6]

mac = ["%02X" % ord(macbuf[i]) for i in range(6)]
mac = "-".join(mac)
except:
mac = None
return mac
iambic 2012-02-29
  • 打赏
  • 举报
回复
自己努力。

37,719

社区成员

发帖
与我相关
我的任务
社区描述
JavaScript,VBScript,AngleScript,ActionScript,Shell,Perl,Ruby,Lua,Tcl,Scala,MaxScript 等脚本语言交流。
社区管理员
  • 脚本语言(Perl/Python)社区
  • IT.BOB
加入社区
  • 近7日
  • 近30日
  • 至今

试试用AI创作助手写篇文章吧