トップ 一覧 検索 ヘルプ RSS ログイン

Programming_python_pingtomacの変更点

  • 追加された行はこのように表示されます。
  • 削除された行はこのように表示されます。
!!!Macアドレス宛にpingを送信するpythonスクリプト
!動作フロー
+arpコマンドを送信し、周囲の端末の情報(ホストネーム、ipアドレス、macアドレス)を取得
+arpを送信し、周囲の端末の情報(ホストネーム、ipアドレス、macアドレス)を取得
+取得した結果をリスト化
+macアドレスを入力
+入力されたmacアドレスを持つ端末を先に生成したリストから検索し、その端末のipアドレスを取得
+取得したipアドレスにpingを送信

!ソースコード
*Ubuntu10.04でのみ動作を確認。
*macは恐らく問題ないが、windowsではarpの出力結果の文字列の構成が異なるため、処理を分ける必要がある。

 #!usr/bin/python
 
 from subprocess import Popen,PIPE
 
 NEW_LINE = "\n"
 
 class ArpData:
   def __init__(self,name,ip,mac):
     self.name = name
     self.ip = ip
     self.mac = mac
 
 class PingMac:
 
   #constructor
   def __init__(self):
     self.listData = []
     self.sendArp()
     self.analyzeArpData()
     self.printArpData()
  
   #send arp
   def sendArp(self):
     proc = Popen(["arp", "-a"], stdout=PIPE, stderr=PIPE)
     self.result = proc.communicate()[0]
    
   #analyze arp data ( put hostname,ipaddress,macaddress )
   def analyzeArpData(self):
     table = self.result.split(NEW_LINE)
     index = 0 
     while index < len(table):
       line = table[index].split(" ")
       #analyze
       #[type]
       #hostname (Ipaddress) at Macaddress on interface ifscope [ethernet]
       #[ex(mac)]
       #? (172.30.10.85) at 0:1f:3b:71:cc:97 [ethernet] on en1
       #[ex(ubuntu)]
       #ywlan010085.sras.sic.shibaura-it.ac.jp (172.30.10.85) at 0:1f:3b:71:cc:97 on en1 ifscope [ethernet]
       if(len(line)>3):
         name = line[0]
         ip = line[1].replace("(","").replace(")","")
         mac = line[3]
         #add
         self.listData.append(ArpData(name,ip,mac))
       index= index + 1
  
   #display arp data table
   def printArpData(self):
     index = 0 
     while index < len(self.listData):
       print "[node] : "+ str(index)
       print "name : "+self.listData[index].name
       print "ip address  : "+self.listData[index].ip
       print "mac address :"+self.listData[index].mac
       index= index + 1
 
   def searchMacIpFromMac(self,macaddr):
     result =""
     index = 0
     while index < len(self.listData):
       if self.listData[index].mac == macaddr:
         result = self.listData[index].ip
         break
       index = index + 1
     return result
 
   #ping to ipaddress
   def pingToIpAddress(self,ipaddr):
     proc = Popen(["ping", ipaddr,"-c 1"], stdout=PIPE, stderr=PIPE)
     print proc.communicate()[0]
  
   #ping to mac address
   def pingToMacAddress(self,macaddr):
     #get ip 
     ipaddr = self.searchMacIpFromMac(macaddr)
     print "send to "+ ipaddr
     #send ping to ip
     self.pingToIpAddress(ipaddr)
 
 #main
 if __name__ == '__main__':
   print "-------------------------------------------------------------"
   print "[get arp table]"
   ping = PingMac()
   print "-------------------------------------------------------------"
   print "[send ping to mac address]"
   print "input destination mac : ",
   mac = raw_input()
   ping.pingToMacAddress(mac)
   print "-------------------------------------------------------------"

!todo
*細かいオプションの設定
*arp情報のキャッシュによる高速手軽化
*gui化(pygameかgtkか)