Skip to content

Instantly share code, notes, and snippets.

@aojea
Created January 22, 2015 19:45
Show Gist options
  • Save aojea/36d0b5648608c9ed1260 to your computer and use it in GitHub Desktop.
Save aojea/36d0b5648608c9ed1260 to your computer and use it in GitHub Desktop.
Internet visualization
library(data.table)
library(bit64)
library(igraph)
library(BioNet)
# Modify with the country
country<-"ES"
# You need the file with the edges genetad with the scrip bpgdump2edge.pl
# Read edge file SRC,DST,IP Prefix, nº ASes AS PATH
edges.df<-fread("bgp/edges2.txt",sep=",",colClasses=c("character","character","character","integer"),header=FALSE)
# Remove IP Prefix fom edges
edges.df[,V3:=NULL]
# And the file with the country code to AS assignment
# Read RIR Assignmentthe 2nd (Country Code ) and 4th (AS number) columns
asn2country.df<-fread("bgp/asn2country.txt",select=c(2,4), na.strings=NULL,colClasses="character",sep="|",header=FALSE)
setkey(asn2country.df,V2)
# Obtain all AS from the chosen Country
CountryNodes<-asn2country.df[.(country)]
setcolorder(CountryNodes, c("V4", "V2"))
setnames(CountryNodes,c("name","group"))
# Obtain all links in the chosen country
setkeyv(edges.df,c("V1","V2"))
MisLinks<-unique(edges.df)
MisLinks<-MisLinks[ V1 %in% CountryNodes$name | V2 %in% CountryNodes$name ]
# Prepare for graph
MisLinks<-as.data.frame(MisLinks)
colnames(MisLinks) = c("src","target","value")
# Obtain all nodes related to that country (inside and connected by 1 hop)
MisNodes<-as.data.frame(unique(c(MisLinks$src,MisLinks$target)),stringsAsFactors = FALSE)
colnames(MisNodes) = c("name")
MisNodes$name<-as.character(MisNodes$name)
setnames(asn2country.df,c("group","name"))
MisNodes<-merge(MisNodes,asn2country.df,by="name", all.x = TRUE)
# Problems with NA country code
MisNodes[is.na(MisNodes[,2]),2] <- "NA"
# IGRAPH
bsk<-graph.data.frame(MisLinks[,1:2],directed=F,MisNodes)
simplify(bsk,remove.multiple = TRUE, remove.loops = FALSE)
bsk<-rmSelfLoops(bsk)
# Size of the node proportional to connections
V(bsk)$size<-igraph::degree(bsk)*10/max(igraph::degree(bsk))
# Color red the country ASes and blue the other
V(bsk)[V(bsk)$group == country]$membership = "0"
V(bsk)[V(bsk)$group != country]$membership = "1"
V(bsk) [ V(bsk)$membership == 0 ]$color <- "red"
V(bsk) [ V(bsk)$membership == 1 ]$color <- "blue"
# Label size proportional degree
V(bsk)$label.cex <- igraph::degree(bsk)/max(igraph::degree(bsk))
# Choose the layout you want
layout <- layout.fruchterman.reingold(bsk)
plot(bsk,layout=layout,
vertex.label=V(bsk)$name,
edge.arrow.size=.1,
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment