Created
January 16, 2013 10:33
-
-
Save stevommmm/4546232 to your computer and use it in GitHub Desktop.
v6 lovebots
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import array | |
| import socket | |
| import fcntl | |
| import struct | |
| import re | |
| def getArchitecture(): | |
| return __import__("platform").architecture()[0] | |
| def __get_ip_address(ifname): | |
| try: | |
| if getArchitecture() == '64bit': | |
| s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) | |
| return socket.inet_ntoa(fcntl.ioctl( | |
| s.fileno(), | |
| 0x8915, # SIOCGIFADDR | |
| struct.pack('256s', ifname[:15]) | |
| )[20:24]) | |
| else: | |
| s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) | |
| return socket.inet_ntoa(fcntl.ioctl( | |
| s.fileno(), | |
| 0x8915, # SIOCGIFADDR | |
| struct.pack('256s', ifname[:15]) | |
| )[20:24]) | |
| except IOError: | |
| return None | |
| def getIpv4(): | |
| addrs = [] | |
| with open('/proc/net/dev', 'r') as fileHandle: | |
| fileLine = fileHandle.readlines() | |
| for line in fileLine: | |
| if ":" in line: | |
| addrs.append(line.split(":")[0].strip()) | |
| return localFilter([__get_ip_address(x) for x in addrs]) | |
| def getIpv6(): | |
| addrs = [] | |
| addrFix = lambda x:re.sub("(.{4})", "\\1:", x, re.DOTALL)[:-1] | |
| with open('/proc/net/if_inet6', 'r') as fileHandle: | |
| fileLine = fileHandle.readlines() | |
| for line in fileLine: | |
| addrs.append(line.split(" ")[0].strip()) | |
| return localFilter([addrFix(x) for x in addrs]) | |
| def localFilter(ipList): | |
| retr = [] | |
| for ip in ipList: | |
| if ip and not ip.startswith('127.0.') and ip != '0000:0000:0000:0000:0000:0000:0000:0001': | |
| retr.append(ip) | |
| return retr | |
| if __name__ == '__main__': | |
| print getIpv4() | |
| print getIpv6() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import socket | |
| import addrs | |
| import random | |
| import threading | |
| import logging | |
| import time | |
| logging.basicConfig(level=logging.DEBUG, | |
| format='(%(threadName)-10s) %(message)s',) | |
| class bottimus(threading.Thread): | |
| def __init__(self, addr, port=random.randrange(3000, 9000)): | |
| logging.debug('Init thread for addr %s' % addr) | |
| # self.__sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | |
| self.__sock = socket.socket(family=socket.AF_INET6) | |
| self.__sock.bind((addr, port)) | |
| self.__fd = self.__sock.makefile("r") | |
| self.__nick = "aaw%d" % random.randrange(122, 400) | |
| self.__chan = "#BOT" | |
| self.__running = True | |
| logging.debug("Nick set: %s" % self.__nick) | |
| threading.Thread.__init__(self) | |
| def stop(self): | |
| self.__running = False | |
| self.__sock.close() | |
| def run(self): | |
| logging.debug('Run thread') | |
| self.__sock.connect(('irc.rozznet.net', 6667)) | |
| self.__sock.send("NICK %s\n" % self.__nick) | |
| self.__sock.send("USER %s 0 * :%s\n" % (self.__nick, self.__nick)) | |
| self.__sock.send("JOIN %s\n" % self.__chan) | |
| while self.__running: | |
| msg = self.__fd.readline() | |
| print str(msg.split(" ")) | |
| if msg != "": | |
| if msg.find('PING') != -1: | |
| self.__sock.send('PONG %s\n' % msg.split()[1]) | |
| if msg.find('MODE %s :+iw' % self.__nick) != -1: | |
| self.__sock.send('JOIN %s\n' % self.__chan) | |
| if msg.find('asdf__') != -1: | |
| self.__sock.send('QUIT :a\n') | |
| break | |
| if msg.find('!lovetime') != -1: | |
| self.__sock.send('PRIVMSG %s :I love zimsky\r\n' % self.__chan) | |
| self.__sock.close() | |
| if __name__ == "__main__": | |
| ipAddrs = addrs.getIpv6() | |
| threadPool = [] | |
| for ip in ipAddrs: | |
| threadPool.append(bottimus(ip)) | |
| threadPool[-1].start() | |
| break | |
| while True: | |
| try: | |
| time.sleep(0.6) | |
| except KeyboardInterrupt: | |
| logging.info("Keyboard Interrupt, stopping....") | |
| break | |
| for localThread in threadPool: | |
| localThread.stop() | |
| localThread.join() | |
| logging.debug('Joined %s' % localThread.getName()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment