class Ping(Sender): name = _("ping tool") def _setup_args(self, parser): super(Ping, self)._setup_args(parser) parser.add_argument('destip', type=str, help=_("Specifies the dest ip send to")) parser.add_argument('--data', type=str, help=_("Specifies the optional data to be sent")) def _before_send(self): super(Ping, self)._before_send() self._sock = IpSocket(socket.IPPROTO_ICMP) self._icmp_echo = IcmpEcho() self._icmp_echo.header.type = ICMP_ECHO print('PING {0} {1} bytes of data.'.format(self.args.destip, self._icmp_echo.length)) def _send(self): if self._icmp_echo.sequence > 0: time.sleep(1) self._icmp_echo.sequence += 1 addr = (self.args.destip, 0) self._icmp_echo.timestamp = int(time.time()*1000) & 0xffffffff self._icmp_echo.header.checksum = 0 self._icmp_echo.header.checksum = socket.ntohs(checksum(self._icmp_echo.dump())) self._sock.sendto(self._icmp_echo.dump(), addr) try: timeout = 2 start_timestamp = time.time() while timeout > 0: self._sock.settimeout(timeout) data, raddr = self._sock.recvfrom(1024) recv_timestamp= int(time.time()*1000) & 0xffffffff if raddr == addr: iphdr = IpHeader() iphdr.load(data) icmp_data = data[iphdr.ihl*4:] icmp_echo_reply = IcmpEcho() icmp_echo_reply.load(icmp_data) if icmp_echo_reply.header.type == ICMP_ECHOREPLY \ and icmp_echo_reply.id == self._icmp_echo.id \ and icmp_echo_reply.sequence == self._icmp_echo.sequence: print('{0} bytes from {1}: icmp_req={2} ttl={3} time={4} ms'.format( len(icmp_data), addr[0], self._icmp_echo.sequence, iphdr.ttl, abs(recv_timestamp-self._icmp_echo.timestamp))) break timeout -= time.time() - start_timestamp if timeout < 0: raise socket.timeout() except socket.timeout: print('timeout') def _get_iphdr_len(self, data): ver_len = struct.unpack('!B', data[0]) return (ver_len & 0x0f)*4 def _after_send(self): # super(Ping, self)._after_send() self._sock.close()
class IpSender(MultiSender): def _setup_args(self, parser, need_data=True): super(IpSender, self)._setup_args(parser) parser.add_argument('protocol', type=str, help=_("Specifies the data protocol to be sent")) parser.add_argument('destip', type=str, help=_("Specifies the dest ip send to")) if need_data: parser.add_argument('--data', type=str, help=_("Specifies the data to be sent")) parser.add_argument('--srcip', type=str, help=_("Specifies the src ip send from")) def _after_setup_args(self): if 'data' in self.args and not self.args.data and self.args.processes: raise NettestError( _("must specifies --data option when use multiprocess")) proto_id = ipproto_dict.get(self.args.protocol.upper()) if proto_id is None: proto_id = int(self.args.protocol) self._protocol = proto_id def _before_send(self): if self.args.srcip: self._sock = IpSocket(socket.IPPROTO_RAW) self._sock.setsockopt(socket.SOL_IP, socket.IP_HDRINCL, 1) self._data = self._get_raw_data() else: self._sock = IpSocket(self._protocol) self._data = self._get_data() def _send(self): addr = (self.args.destip, 0) self._sock.sendto(self._data, addr) def _after_send(self): self._sock.close() def _get_data(self): if self.args.data: return self.args.data.encode() else: return sys.stdin.read(65535).encode() def _get_raw_data(self): iphdr = IpHeader() iphdr.protocol = self._protocol if self.args.srcip: iphdr.saddr = self.args.srcip iphdr.daddr = self.args.destip data = self._get_data() iphdr.ihl = iphdr.length >> 2 return iphdr.dump() + data
class IpSender(MultiSender): def _setup_args(self, parser, need_data=True): super(IpSender, self)._setup_args(parser) parser.add_argument('protocol', type=str, help=_("Specifies the data protocol to be sent")) parser.add_argument('destip', type=str, help=_("Specifies the dest ip send to")) if need_data: parser.add_argument('--data', type=str, help=_("Specifies the data to be sent")) parser.add_argument('--srcip', type=str, help=_("Specifies the src ip send from")) def _after_setup_args(self): if 'data' in self.args and not self.args.data and self.args.processes: raise NettestError(_("must specifies --data option when use multiprocess")) proto_id = ipproto_dict.get(self.args.protocol.upper()) if proto_id is None: proto_id = int(self.args.protocol) self._protocol = proto_id def _before_send(self): if self.args.srcip: self._sock = IpSocket(socket.IPPROTO_RAW) self._sock.setsockopt(socket.SOL_IP, socket.IP_HDRINCL, 1) self._data = self._get_raw_data() else: self._sock = IpSocket(self._protocol) self._data = self._get_data() def _send(self): addr = (self.args.destip, 0) self._sock.sendto(self._data, addr) def _after_send(self): self._sock.close() def _get_data(self): if self.args.data: return self.args.data.encode() else: return sys.stdin.read(65535).encode() def _get_raw_data(self): iphdr = IpHeader() iphdr.protocol = self._protocol if self.args.srcip: iphdr.saddr = self.args.srcip iphdr.daddr = self.args.destip data = self._get_data() iphdr.ihl = iphdr.length >> 2 return iphdr.dump() + data
class Ping(Sender): name = _("ping tool") def _setup_args(self, parser): super(Ping, self)._setup_args(parser) parser.add_argument('destip', type=str, help=_("Specifies the dest ip send to")) parser.add_argument('--data', type=str, help=_("Specifies the optional data to be sent")) def _before_send(self): super(Ping, self)._before_send() self._sock = IpSocket(socket.IPPROTO_ICMP) self._icmp_echo = IcmpEcho() self._icmp_echo.header.type = ICMP_ECHO print('PING {0} {1} bytes of data.'.format(self.args.destip, self._icmp_echo.length)) def _send(self): if self._icmp_echo.sequence > 0: time.sleep(1) self._icmp_echo.sequence += 1 addr = (self.args.destip, 0) self._icmp_echo.timestamp = int(time.time() * 1000) & 0xffffffff self._icmp_echo.header.checksum = 0 self._icmp_echo.header.checksum = socket.ntohs( checksum(self._icmp_echo.dump())) self._sock.sendto(self._icmp_echo.dump(), addr) try: timeout = 2 start_timestamp = time.time() while timeout > 0: self._sock.settimeout(timeout) data, raddr = self._sock.recvfrom(1024) recv_timestamp = int(time.time() * 1000) & 0xffffffff if raddr == addr: iphdr = IpHeader() iphdr.load(data) icmp_data = data[iphdr.ihl * 4:] icmp_echo_reply = IcmpEcho() icmp_echo_reply.load(icmp_data) if icmp_echo_reply.header.type == ICMP_ECHOREPLY \ and icmp_echo_reply.id == self._icmp_echo.id \ and icmp_echo_reply.sequence == self._icmp_echo.sequence: print( '{0} bytes from {1}: icmp_req={2} ttl={3} time={4} ms' .format( len(icmp_data), addr[0], self._icmp_echo.sequence, iphdr.ttl, abs(recv_timestamp - self._icmp_echo.timestamp))) break timeout -= time.time() - start_timestamp if timeout < 0: raise socket.timeout() except socket.timeout: print('timeout') def _get_iphdr_len(self, data): ver_len = struct.unpack('!B', data[0]) return (ver_len & 0x0f) * 4 def _after_send(self): # super(Ping, self)._after_send() self._sock.close()