diff options
author | Lassulus <lassulus@googlemail.com> | 2012-12-09 03:06:58 +0100 |
---|---|---|
committer | Lassulus <lassulus@googlemail.com> | 2012-12-09 03:06:58 +0100 |
commit | 85af484348ac6ad8ca6b9bf992ecf8b63b4ec587 (patch) | |
tree | 507466a4e172e033bb2e13e0083f875200ed6bd3 /retiolum/bin | |
parent | 8c11e39a58e69de9b1912756082609f5ffb0dcb1 (diff) | |
parent | d263d0e9e7e6c37f793eb3ec1908d188849f5d02 (diff) |
Merge branch 'master' of github.com:krebscode/painload
Diffstat (limited to 'retiolum/bin')
-rwxr-xr-x | retiolum/bin/announce_pubkey | 35 | ||||
-rwxr-xr-x | retiolum/bin/tinc_stats2json | 123 |
2 files changed, 158 insertions, 0 deletions
diff --git a/retiolum/bin/announce_pubkey b/retiolum/bin/announce_pubkey new file mode 100755 index 00000000..ce5aed19 --- /dev/null +++ b/retiolum/bin/announce_pubkey @@ -0,0 +1,35 @@ +#!/bin/sh +set -euf +HOST="${1:-}" + +usage (){ +cat <<EOF +usage: $0 HOSTNAME + +also, you are made of stupid +EOF +exit 23 +} + +[ "x$HOST" = "x" ] && usage + +CHANNEL="#krebsco" +IRCHOST="irc.freenode.net" +PORT=6667 +RETIOLUM="/etc/tinc/retiolum" +PUBFILE="$RETIOLUM/hosts/$HOST" + +if [ ! -e $PUBFILE ] ;then + echo "cannot find $PUBFILE - host $HOST wrong?" ; + echo + usage +fi + +NICK="${HOST}_$((RANDOM%666))" + +( echo "NICK $NICK"; + echo "USER $NICK $IRCHOST bla : $NICK"; + echo "JOIN $CHANNEL"; + sleep 23; + sed "s/^\(.*\)/PRIVMSG $CHANNEL : \1/" $PUBFILE; + sleep 5; ) | telnet $IRCHOST $PORT diff --git a/retiolum/bin/tinc_stats2json b/retiolum/bin/tinc_stats2json new file mode 100755 index 00000000..acadb306 --- /dev/null +++ b/retiolum/bin/tinc_stats2json @@ -0,0 +1,123 @@ +#!/usr/bin/python +import subprocess +import os +import re +import sys +import json + + + +TINC_NETWORK =os.environ.get("TINC_NETWORK","retiolum") + +# is_legacy is the parameter which defines if the tinc config files are handled old fashioned (parse from syslog), +# or if the new and hip tincctl should be used +is_legacy= os.environ.get("TINC_LEGACY",False) +SYSLOG_FILE = os.environ.get("LOG_FILE","/var/log/everything.log") + + +# Tags and Delimiters +TINC_TAG="tinc.%s" % TINC_NETWORK +BEGIN_NODES = "Nodes:" +END_NODES = "End of nodes." +BEGIN_SUBNET = "Subnet list:" +END_SUBNET = "End of subnet list" +BEGIN_EDGES = "Edges:" +END_EDGES = "End of edges." + +def get_tinc_block(log_file): + """ returns an iterateable block from the given log file (syslog) + This function became obsolete with the introduction of tincctl + """ + from BackwardsReader import BackwardsReader + tinc_block = [] + in_block = False + bf = BackwardsReader(log_file) + BOL = re.compile(".*tinc.%s\[[0-9]+\]: " % TINC_NETWORK) + while True: + line = bf.readline() + if not line: + raise Exception("end of file at log file? This should not happen!") + line = BOL.sub('',line).strip() + + if END_SUBNET in line: + in_block = True + + if not in_block: + continue + + tinc_block.append(line) + + if BEGIN_NODES in line: + break + return reversed(tinc_block) + +def parse_new_input(): + nodes = {} + pnodes = subprocess.check_output(["tincctl","-n",TINC_NETWORK,"dump","reachable","nodes"]) + for line in pnodes.split('\n'): + if not line: continue + l = line.split() + nodes[l[0]]= { 'external-ip': l[2], 'external-port' : l[4] } + psubnets = subprocess.check_output(["tincctl","-n",TINC_NETWORK,"dump","subnets"]) + for line in psubnets.split('\n'): + if not line: continue + l = line.split() + try: + if not nodes[l[2]].get('internal-ip',False): + nodes[l[2]]['internal-ip'] = [] + nodes[l[2]]['internal-ip'].append(l[0].split('#')[0]) + except KeyError: + pass # node does not exist (presumably) + pedges = subprocess.check_output(["tincctl","-n",TINC_NETWORK,"dump","edges"]) + for line in pedges.split('\n'): + if not line: continue + l = line.split() + try: + if not nodes[l[0]].has_key('to') : + nodes[l[0]]['to'] = [] + nodes[l[0]]['to'].append( + {'name':l[2],'addr':l[4],'port':l[6],'weight' : l[10] }) + except KeyError: + pass #node does not exist + return nodes + +def parse_input(log_data): + nodes={} + for line in log_data: + if BEGIN_NODES in line : + nodes={} + for line in log_data: + if END_NODES in line : + break + l = line.replace('\n','').split() #TODO unhack me + nodes[l[0]]= { 'external-ip': l[2], 'external-port' : l[4] } + if BEGIN_SUBNET in line : + for line in log_data: + if END_SUBNET in line : + break + l = line.replace('\n','').split() + if not nodes[l[2]].get('internal-ip',False): + nodes[l[2]]['internal-ip'] = [] + nodes[l[2]]['internal-ip'].append(l[0].split('#')[0]) + if BEGIN_EDGES in line : + edges = {} + for line in log_data: + if END_EDGES in line : + break + l = line.replace('\n','').split() + if not nodes[l[0]].has_key('to') : + nodes[l[0]]['to'] = [] + nodes[l[0]]['to'].append( + {'name':l[2],'addr':l[4],'port':l[6],'weight' : l[10] }) + return nodes + + +if __name__ == '__main__': + import subprocess,time + if is_legacy: + subprocess.call(["pkill","-SIGUSR2", "tincd"]) + time.sleep(1) + print json.dumps(parse_input((get_tinc_block(SYSLOG_FILE)))) + else: + print json.dumps(parse_new_input()) + |