def _parse_routing_table(self, line): """ Check if -r paramter is in use. If yes then parse corresponding values in line. Append those values to GROUP list. :param line: Line to process. :return: None but raises ParsingDone if line has the information to handle by this method. """ if "r" in self.options and self._regex_helper.search_compiled( Netstat._re_routing_table, line): if "ROUTING_TABLE" not in self.current_ret: self.current_ret['ROUTING_TABLE'] = list() _ret_dict = { "destination": self._regex_helper.group("DESTINATION"), "gateway": self._regex_helper.group("GATEWAY"), "genmask": self._regex_helper.group("GENMASK"), "flags": self._regex_helper.group("FLAGS"), "mss": convert_to_number(self._regex_helper.group("MSS")), "window": convert_to_number(self._regex_helper.group("WINDOW")), "irtt": convert_to_number(self._regex_helper.group("IRTT")), "iface": self._regex_helper.group("INTERFACE") } self.current_ret['ROUTING_TABLE'].append(_ret_dict) raise ParsingDone
def _parse_headers_unix(self, line): """ Check if parsed type of connection is a UNIX domain socket. If yes then parse corresponding values in line. Append those values to UNIX_SOCKETS list. :param line: Line to process. :return: None but raises ParsingDone if line has the information to handle by this method. """ if self._active == "UNIX domain sockets": if "UNIX_SOCKETS" not in self.current_ret: self.current_ret['UNIX_SOCKETS'] = list() if self._regex_helper.search_compiled(Netstat._re_header_unix, line): _ret_dict = { "proto": self._regex_helper.group("PROTO"), "refcnt": convert_to_number(self._regex_helper.group("REFCNT")), "flags": self._regex_helper.group("FLAGS"), "type": self._regex_helper.group("TYPE"), "state": self._regex_helper.group("STATE"), "i-node": convert_to_number(self._regex_helper.group("INODE")), "path": self._regex_helper.group("PATH") } if self._regex_helper.group("PID"): _ret_dict.update( {"pid/program name": self._regex_helper.group("PID")}) self.current_ret['UNIX_SOCKETS'].append(_ret_dict) raise ParsingDone
def _parse_headers_internet(self, line): """ Check if parsed type of connection is an internet type. If yes then parse corresponding values in line. Append those values to INTERNET_CONNECTIONS list. :param line: Line to process. :return: None but raises ParsingDone if line has the information to handle by this method. """ if self._active == "Internet connections": if "INTERNET_CONNECTIONS" not in self.current_ret: self.current_ret['INTERNET_CONNECTIONS'] = list() if self._regex_helper.search_compiled(Netstat._re_header_internet, line): _ret_dict = { "proto": self._regex_helper.group("PROTO"), "recv-q": convert_to_number(self._regex_helper.group("RECVQ")), "send-q": convert_to_number(self._regex_helper.group("SENDQ")), "local address": self._regex_helper.group("LADDRESS"), "foreign address": self._regex_helper.group("FADDRESS"), "state": self._regex_helper.group("STATE") } if self._regex_helper.group("PID"): _ret_dict.update( {"pid/program name": self._regex_helper.group("PID")}) self.current_ret['INTERNET_CONNECTIONS'].append(_ret_dict) raise ParsingDone
def _parse_interface(self, line): """ Check if -i paramter is in use. If yes then parse corresponding values in line. Append those values to INTERFACE list. :param line: Line to process. :return: None but raises ParsingDone if line has the information to handle by this method. """ if "i" in self.options and self._regex_helper.search_compiled( Netstat._re_interface, line): if "INTERFACE" not in self.current_ret: self.current_ret['INTERFACE'] = list() _ret_dict = { "iface": self._regex_helper.group("INTERFACE"), "mtu": convert_to_number(self._regex_helper.group("MTU")), "met": convert_to_number(self._regex_helper.group("MET")), "rx-ok": convert_to_number(self._regex_helper.group("RXOK")), "rx-err": convert_to_number(self._regex_helper.group("RXERR")), "rx-drp": convert_to_number(self._regex_helper.group("RXDRP")), "rx-ovr": convert_to_number(self._regex_helper.group("RXOVR")), "tx-ok": convert_to_number(self._regex_helper.group("TXOK")), "tx-err": convert_to_number(self._regex_helper.group("TXERR")), "tx-drp": convert_to_number(self._regex_helper.group("TXDRP")), "tx-ovr": convert_to_number(self._regex_helper.group("TXOVR")), "flg": self._regex_helper.group("FLG") } self.current_ret['INTERFACE'].append(_ret_dict) raise ParsingDone
def _parse_two_values_with_type(self, line): if self._regex_helper.search_compiled(self._re_two_values_with_type, line): kind = self._regex_helper.group("KIND") if kind not in self.current_ret[self._outer][self._inner]: self.current_ret[self._outer][self._inner][kind] = dict() self.current_ret[self._outer][self._inner][kind][self._regex_helper.group("KEY1")] = \ convert_to_number(self._regex_helper.group("VALUE1")) self.current_ret[self._outer][self._inner][kind][self._regex_helper.group("KEY2")] = \ convert_to_number(self._regex_helper.group("VALUE2")) raise ParsingDone()
def _parse_headers(self, line): if self._regex_helper.search_compiled(Ss._re_parse_groups, line): socket_dict = dict() socket_dict['netid'] = self._regex_helper.group("NETID") socket_dict['state'] = self._regex_helper.group("STATE") socket_dict['recv-Q'] = convert_to_number( self._regex_helper.group("RECV_Q")) socket_dict['send-Q'] = convert_to_number( self._regex_helper.group("SEND_Q")) socket_dict['local_address'] = self._regex_helper.group( "LOCAL_ADDR") socket_dict['local_port'] = self._regex_helper.group("LOCAL_PORT") socket_dict['peer_address'] = self._regex_helper.group("PEER_ADDR") socket_dict['peer_port'] = self._regex_helper.group("PEER_PORT") self.current_ret['NETWORK_CONNECTIONS'].append(socket_dict) self.connection_index = self.current_ret[ 'NETWORK_CONNECTIONS'].index(socket_dict) raise ParsingDone
def _parse_sctp_streams(self, line): if self._regex_helper.search_compiled(Ss._re_parse_sctp_stream, line): self.current_ret['NETWORK_CONNECTIONS'][ self.connection_index]['streams'] = [] sctp_stream = dict() sctp_stream['state'] = self._regex_helper.group("STATE") sctp_stream['recv-Q'] = convert_to_number( self._regex_helper.group("RECV_Q")) sctp_stream['send-Q'] = convert_to_number( self._regex_helper.group("SEND_Q")) sctp_stream['local_address'] = self._regex_helper.group( "LOCAL_ADDR") sctp_stream['local_interface'] = self._regex_helper.group( "INTERFACE") sctp_stream['local_port'] = self._regex_helper.group("LOCAL_PORT") sctp_stream['peer_address'] = self._regex_helper.group("PEER_ADDR") sctp_stream['peer_port'] = self._regex_helper.group("PEER_PORT") self.current_ret['NETWORK_CONNECTIONS'][ self.connection_index]['streams'].append(sctp_stream) raise ParsingDone
def _parse_groups(self, line): """ Check if -g paramter is in use. If yes then parse corresponding values in line. Append those values to GROUP list. :param line: Line to process. :return: None but raises ParsingDone if line has the information to handle by this method. """ if "g" in self.options and self._regex_helper.search_compiled( Netstat._re_groups, line): if "GROUP" not in self.current_ret: self.current_ret['GROUP'] = list() _ret_dict = { "interface": self._regex_helper.group("INTERFACE"), "refcnt": convert_to_number(self._regex_helper.group("REFCNT")), "group": self._regex_helper.group("GROUP") } self.current_ret['GROUP'].append(_ret_dict) raise ParsingDone
def _parse_history(self, line): if self._regex_helper.search_compiled(History._re_history, line): self.current_ret[convert_to_number(self._regex_helper.group('NUMBER'))] = self._regex_helper.group('CMD') raise ParsingDone
def _convert_string_to_number(self, value): if self.convert_string_to_number: value = convert_to_number(value) return value
def test_convert_to_number_str(): from moler.helpers import convert_to_number expected = "not a number" result = convert_to_number(expected) assert expected == result
def test_convert_to_number_float(): from moler.helpers import convert_to_number expected = 3.2 result = convert_to_number("{}".format(expected)) assert expected == result
def _parse_one_value(self, line): if self._regex_helper.search_compiled(self._re_one_value, line): self.current_ret[self._outer][self._inner][self._regex_helper.group("KEY")] = \ convert_to_number(self._regex_helper.group("VALUE")) raise ParsingDone()