Skip to content

Instantly share code, notes, and snippets.

@mrtc0
Created November 5, 2015 11:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mrtc0/75e1c3c1eb5504275ddb to your computer and use it in GitHub Desktop.
Save mrtc0/75e1c3c1eb5504275ddb to your computer and use it in GitHub Desktop.
ネットワークのグラフを作成するスクリプト
import networkx as nx
from scapy.all import *
import matplotlib.pyplot as plt
# 無向グラフを作成
g = nx.Graph()
# 自分のIPアドレスを取得
myip = [x[4] for x in conf.route.routes if x[2] != '0.0.0.0'][0]
print "Press CTRL+C to stop capture"
# パケットキャプチャの開始,CTRL+Cでキャプチャストップ
pkt = sniff(filter="ip")
print "Capture stopped"
# IPアドレスを抽出,自分のIPアドレスとエッジを張る
for i in range(len(pkt)):
ip = pkt[i][IP].src
if ip != myip:
print ip
print myip
g.add_edge(myip,ip)
# グラフにIPアドレスを表示する
for v in g.nodes():
g.node[v]['state']=v
# 自身のノードカラーを赤に
# 以下カラーリング処理部分は未完成
val_map = {myip: 1.0}
values = [val_map.get(node,0.25) for node in g.nodes()]
pos = nx.spring_layout(g)
colors=range(g.number_of_edges())
# グラフの描画
nx.draw(g, pos, node_color=values, cmap=plt.get_cmap('jet'),edge_color=colors,width=4,edge_cmap=plt.cm.Blues)
node_labels = nx.get_node_attributes(g,'state')
nx.draw_networkx_labels(g, pos, labels = node_labels)
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment