def run_netstat(self): buf = StringIO() netstat(u'-su', _out=buf) buf.seek(0) data = [] donot_parse = True for line in buf: line = line.rstrip('\n') if line.startswith('Udp:'): donot_parse = False continue if line.startswith('UdpLite:'): donot_parse = True if donot_parse: continue m = match(r'^\s*([0-9]+)\b([^$]+)$', line) if not m: continue data.append({ 'key': m.group(2).strip(' '), 'value': m.group(1).strip(' ') }) return data
def get_node_ip(): if arch in ('armv6l', 'armv7l'): active_interfaces = interfaces() if 'eth0' in active_interfaces: interface = 'eth0' elif 'wlan0' in active_interfaces: interface = 'wlan0' else: raise Exception("No active network interface found.") try: my_ip = ifaddresses(interface)[2][0]['addr'] return my_ip except KeyError: raise Exception("Unable to retrieve an IP.") else: """Returns the node's IP, for the interface that is being used as the default gateway. This shuld work on both MacOS X and Linux.""" try: default_interface = grep(netstat('-nr'), '-e', '^default', '-e' '^0.0.0.0').split()[-1] my_ip = ifaddresses(default_interface)[2][0]['addr'] return my_ip except: raise Exception("'Unable to resolve local IP address.")
def get_local_socket(): # try to determine the mysql socket path local_socket = "" if "linux" in sys.platform: local_socket = str( awk(netstat('-ln'), '/mysql(.*)?\.sock/ { print $9 }')).strip() elif sys.platform == "darwin": local_socket = str( awk(netstat('-an'), '/mysql(.*)?\.sock/ { print $5 }')).strip() # if we don't find a file, make it a required parameter if not os.path.exists(local_socket): local_socket = None return local_socket
def get_interface_data(self, interface): if OS == OS_type.darwin: # OS X does not have /sys/class/net/... # This is only for testing on OS X. Running netstat takes # way to much time to be a practical method for reading # the byte counters of an interface. netstat_output = netstat("-i", "-b", "-I", interface) for line in netstat_output: if line.startswith(interface): words = line.split() rx_bytes = int(words[6]) tx_bytes = int(words[9]) return time.time(), rx_bytes, tx_bytes return time.time(), 0, 0 else: # FIXME: Perhaps open the tx_file and the rx_file in the # __init__ method instead. Is there really a good # reason for doing it this way? tx_fn = "/sys/class/net/%s/statistics/tx_bytes" % interface rx_fn = "/sys/class/net/%s/statistics/rx_bytes" % interface if self.tx_file is None: self.tx_file = open(tx_fn) tx_bytes = int(self.tx_file.read()) else: self.tx_file.seek(0) tx_bytes = int(self.tx_file.read()) if self.rx_file is None: self.rx_file = open(rx_fn) rx_bytes = int(self.rx_file.read()) else: self.rx_file.seek(0) rx_bytes = int(self.rx_file.read()) # return time.time(), tx_bytes, rx_bytes return [time.time(), tx_bytes, rx_bytes]
def get_node_ip(): """Returns the node's IP, for the interface that is being used as the default gateway. This should work on both MacOS X and Linux.""" try: default_interface = grep(netstat('-nr'), '-e', '^default', '-e' '^0.0.0.0').split()[-1] my_ip = ifaddresses(default_interface)[2][0]['addr'] return my_ip except: raise Exception("Unable to resolve local IP address.")
def parse_pull_args(desc=None): parser = argparse.ArgumentParser( description=desc, formatter_class=argparse.ArgumentDefaultsHelpFormatter) # some defaults for the local database dl_user = '******' dl_database = 'things_downstream' dl_password = '******' # try to determine the mysql socket path dl_socket = "" if "linux" in sys.platform: dl_socket = str(awk(netstat('-ln'), '/mysql(.*)?\.sock/ { print $9 }')).strip() elif sys.platform == "darwin": dl_socket = str(awk(netstat('-an'), '/mysql(.*)?\.sock/ { print $5 }')).strip() # if we don't find a file, make it a required parameter if not os.path.exists(dl_socket): dl_socket = None # some defaults for the remote database # typically this would be a remote server--using localhost for testing dr_database = 'things_upstream' dr_user = '******' dr_password = '******' dr_host = '127.0.0.1' parser.add_argument('--local-user', default=dl_user) parser.add_argument('--local-password', default=dl_password) parser.add_argument('--local-database', default=dl_database) parser.add_argument('--local-socket', default=dl_socket) parser.add_argument('-u', '--remote-user', default=dr_user) parser.add_argument('-p', '--remote-password', default=dr_password) parser.add_argument('-o', '--remote-host', default=dr_host) parser.add_argument('-d', '--remote-database', default=dr_database) parser.add_argument('-c', '--cipher') return parser.parse_args()
def scan_ports(self): self.listening_ports = {} listening_ports_raw = sh.netstat('-ln') for listener in listening_ports_raw: match = re.match( "tcp\\s+\\d+\\s+\\d+\\s+127\\.0\\.0\\.1:(\\d+)\\s+0\\.0\\.0\\.0:\\*\\s*LISTEN", listener) if match is not None: self.listening_ports[int(match.group(1))] = 1 return self.listening_ports
def get_node_ip(): """Returns the node's IP, for the interface that is being used as the default gateway. This shuld work on both MacOS X and Linux.""" try: default_interface = grep(netstat("-nr"), "-e", "^default", "-e" "^0.0.0.0").split()[-1] my_ip = ifaddresses(default_interface)[2][0]["addr"] return my_ip except: pass return None
def get_node_ip(): if arch in ('armv6l', 'armv7l') and getenv("RESIN_UUID") is None: interface = None for n in range(10): iface = 'eth{}'.format(n) try: file_carrier = open('/sys/class/net/' + iface + '/carrier') file_operstate = open('/sys/class/net/' + iface + '/operstate') if "1" in file_carrier.read() and "up" in file_operstate.read(): interface = iface break except IOError: continue if not interface: file_interfaces = open('/etc/network/interfaces') for n in range(10): iface = 'wlan{}'.format(n) if iface in file_interfaces.read(): interface = iface break if not interface: """Check to see if we're running in USB gadget mode.""" file_interfaces = open('/etc/network/interfaces') iface = 'usb0' if iface in file_interfaces.read(): interface = iface if not interface: raise Exception("No active network connection found.") try: my_ip = ifaddresses(interface)[2][0]['addr'] return my_ip except KeyError: raise Exception("Unable to retrieve an IP.") else: """Returns the node's IP, for the interface that is being used as the default gateway. This should work on both MacOS X and Linux.""" try: default_interface = grep(netstat('-nr'), '-e', '^default', '-e' '^0.0.0.0').split()[-1] my_ip = ifaddresses(default_interface)[2][0]['addr'] return my_ip except: raise Exception("Unable to resolve local IP address.")
def __trafficPerSecond(): self.__flow_new = sh.awk( sh.netstat("-I", self.__network_interface, "-bn"), 'NR==2 {print $7,$10}').strip().split(" ") self.__flow_new = [int(i) for i in self.__flow_new] self.__stamp_new = time.time() d = self.__stamp_new - self.__stamp_old rRate = (self.__flow_new[0] - self.__flow_old[0]) / 1000 / d tRate = (self.__flow_new[1] - self.__flow_old[1]) / 1000 / d self.__flow_old[0] = self.__flow_new[0] self.__flow_old[1] = self.__flow_new[1] self.__stamp_old = self.__stamp_new return (rRate, tRate)
def getUsedPorts(logger=None): try: ports = [] result = sh.netstat("-plntu") if result is not None and len(result) > 0: for line in result.splitlines(): try: line = line.split() line = line[3].split(":") port = line[-1] port = int(port) ports.append(port) except Exception as e: if logger is not None: logger.error(str(e)) return ports except Exception as e: if logger is not None: logger.error(str(e)) return []
#!/usr/bin/env python from sh import grep, netstat print(grep(netstat('-an'), 'LISTEN')) # <1>
def _dockerHost(self, config): """ Function to check if container/external IP have connections open to our main container. This container is running in "network=host" like "hass" and "mosquitto". """ # Check configuration for conf in [CONF_NAME, CONF_HOST, CONF_PORT, CONF_CLIENTS]: if conf not in config: LOGGER.error( "%s: Invalid config, missing '%s' in config=%s", ATTR_DOCKERHOST, conf, str(config), ) return if not config[CONF_ENABLED]: LOGGER.debug("%s: %s is not enabled", ATTR_DOCKERHOST, config[CONF_NAME]) return # Just report it in debug mode LOGGER.debug("%s: %s is enabled", ATTR_DOCKERHOST, config[CONF_NAME]) LOGGER.debug("%s: config=%s", ATTR_DOCKERHOST, str(config)) # Get our docker client client = docker.from_env() # Check if main docker container exist and is running try: container = client.containers.get(config[CONF_CONTAINER]) except docker.errors.NotFound: # Container doesn't exit, so we shouldn't continue LOGGER.error( "%s: %s primary container %s does not exist", ATTR_DOCKERHOST, config[CONF_NAME], config[CONF_CONTAINER], ) # Add to error list msg = "Container {} does not exist".format(config[CONF_CONTAINER]) self._handleMsg( ATTR_ALARM, ATTR_DOCKERHOST, ATTR_CONTAINERS, config[CONF_NAME], config[CONF_CONTAINER], msg, ) return # The container needs to be running, otherwise no connectivity can be there if container.status != "running": LOGGER.error( "%s: %s primary container %s not running", ATTR_DOCKERHOST, config[CONF_NAME], config[CONF_CONTAINER], ) # Add to error list msg = "Container {} not running".format(config[CONF_CONTAINER]) self._handleMsg( ATTR_ALARM, ATTR_DOCKERHOST, ATTR_CONTAINERS, config[CONF_NAME], config[CONF_CONTAINER], msg, ) return pid = container.attrs["State"]["Pid"] LOGGER.debug( "%s: %s is running with pid=%d", ATTR_DOCKERHOST, config[CONF_CONTAINER], pid, ) # Clear possible error with primary container msg = "Container {} alarm cleared".format(config[CONF_CONTAINER]) self._handleMsg( ATTR_CLEAR, ATTR_DOCKERHOST, ATTR_CONTAINERS, config[CONF_NAME], config[CONF_CONTAINER], msg, ) # Configure errorfound to False errorfound = False # Go through list of containers connected to primary if CONF_CONTAINERS in config[CONF_CLIENTS]: host = config[CONF_HOST] if self.isIPValid(config[CONF_HOST]): host = config[CONF_HOST].replace(".", "\.") # We support multiple port(s) checklist = [] if type(config[CONF_PORT]).__name__ == "list": for port in config[CONF_PORT]: checklist.append((".*:.*\s*" + host + ":" + str(port) + "\s*ESTABLISHED$")) else: checklist.append((".*:.*\s*" + host + ":" + str(config[CONF_PORT]) + "\s*ESTABLISHED$")) checkfor = "|".join(checklist) LOGGER.debug("%s: Connection string '%s'", ATTR_DOCKERHOST, checkfor) for name in config[CONF_CLIENTS][CONF_CONTAINERS]: # Check if client container exist and is running try: container = client.containers.get(name) except docker.errors.NotFound: # Container doesn't exit, so we shouldn't continue LOGGER.error( "%s: %s client container %s does not exist", ATTR_DOCKERHOST, config[CONF_NAME], name, ) # Add to error list msg = "Container {} does not exist".format(name) self._handleMsg( ATTR_ALARM, ATTR_DOCKERHOST, ATTR_CONTAINERS, config[CONF_NAME], name, msg, ) errorfound = True continue # The container needs to be running, otherwise no connectivity can be there if container.status != "running": LOGGER.error( "%s: %s client container %s not running", ATTR_DOCKERHOST, config[CONF_NAME], name, ) # Add to error list msg = "Container {} not running".format(name) self._handleMsg( ATTR_ALARM, ATTR_DOCKERHOST, ATTR_CONTAINERS, config[CONF_NAME], name, msg, ) errorfound = True continue pid = container.attrs["State"]["Pid"] LOGGER.debug("%s: %s is running with pid=%d", ATTR_DOCKERHOST, name, pid) # Check if we have connectivity, we go in their namespace # With docker this is *only* possible through namespace and shell, # there doesn't seem to be a simple python option with Namespace(pid, "net"): try: netstatparam = "-a" if config[CONF_DNS] else "-na" outp = sh.egrep( sh.netstat(netstatparam, _tty_out=False), checkfor) except sh.ErrorReturnCode_1: # Not found, so no connection LOGGER.error( "%s: container %s not connected %s", ATTR_DOCKERHOST, name, config[CONF_NAME], ) msg = "Container {} not connected to {}".format( config[CONF_CONTAINER], config[CONF_NAME]) self._handleMsg( ATTR_ALARM, ATTR_DOCKERHOST, ATTR_CONTAINERS, config[CONF_NAME], name, msg, ) errorfound = True continue except sh.ErrorReturnCode as e: # Not good, shouldn't happen LOGGER.error( "%s: container %s returned an error with checkfor='%s'. msg='%s'", ATTR_DOCKERHOST, name, checkfor, str(e), ) msg = "Container {} not connected to {} (RC>1)".format( config[CONF_CONTAINER], config[CONF_NAME]) self._handleMsg( ATTR_ALARM, ATTR_DOCKERHOST, ATTR_CONTAINERS, config[CONF_NAME], name, msg, ) errorfound = True continue # RC=0, should be good #if outp.count("\n") > 1: # LOGGER.error( # "%s: container %s returned more then 1 line '%s'", # ATTR_DOCKERHOST, # name, # outp, # ) LOGGER.debug( "%s: container %s connected to %s", ATTR_DOCKERHOST, name, config[CONF_NAME], ) # Clear possible error with primary container msg = "Container {} alarm cleared".format(name) self._handleMsg( ATTR_CLEAR, ATTR_DOCKERHOST, ATTR_CONTAINERS, config[CONF_NAME], name, msg, ) # Check the hosts (external IPs) if CONF_HOSTS in config[CONF_CLIENTS]: host = config[CONF_HOST] if self.isIPValid(config[CONF_HOST]): host = config[CONF_HOST].replace(".", "\.") # We support multiple port(s) checklist = [] if type(config[CONF_PORT]).__name__ == "list": for port in config[CONF_PORT]: checklist.append("\s*" + host + ":" + str(port) + "\s*{}:.*\s*ESTABLISHED$") else: checklist.append("\s*" + host + ":" + str(config[CONF_PORT]) + "\s*{}:.*\s*ESTABLISHED$") checkfor = "|".join(checklist) checkfor = ("\s*" + host + ":" + str(config[CONF_PORT]) + "\s*{}:.*\s*ESTABLISHED$") for name in config[CONF_CLIENTS][CONF_HOSTS]: try: netstatparam = "-a" if config[CONF_DNS] else "-na" host = name if self.isIPValid(name): host = name.replace(".", "\.") LOGGER.debug( "%s: Connection string '%s'", ATTR_DOCKERHOST, checkfor.format(host), ) outp = sh.egrep(sh.netstat(netstatparam, _tty_out=False), checkfor.format(host)) except sh.ErrorReturnCode_1: # Not found, so no connection LOGGER.error( "%s: host %s not connected %s", ATTR_DOCKERHOST, name, config[CONF_NAME], ) msg = "Host {} not connected to {}".format( name, config[CONF_NAME]) self._handleMsg( ATTR_ALARM, ATTR_DOCKERHOST, ATTR_HOSTS, config[CONF_NAME], name, msg, ) errorfound = True continue except sh.ErrorReturnCode as e: # Not good, shouldn't happen LOGGER.error( "%s: host %s returned an error with checkfor='%s'. msg='%s'", ATTR_DOCKERHOST, name, checkfor.format(host), str(e), ) msg = "Host {} not connected to {} (RC>1)".format( name, config[CONF_NAME]) self._handleMsg( ATTR_ALARM, ATTR_DOCKERHOST, ATTR_HOSTS, config[CONF_NAME], name, msg, ) errorfound = True continue # RC=0, should be good if outp.count("\n") > 1: LOGGER.error( "%s: host %s returned more then 1 line '%s'", ATTR_DOCKERHOST, name, outp, ) LOGGER.debug( "%s: host %s connected to %s", ATTR_DOCKERHOST, name, config[CONF_NAME], ) # Clear possible error with primary container msg = "Host {} alarm cleared".format(name) self._handleMsg( ATTR_CLEAR, ATTR_DOCKERHOST, ATTR_HOSTS, config[CONF_NAME], name, msg, ) # Configure errorfound to False if not errorfound: LOGGER.debug("%s: OK", ATTR_DOCKERHOST)
def __update_network_interface(self): self.__network_interface = sh.awk( sh.head(sh.netstat("-nr"), '-n5'), '/default/ {print $6}').strip()