Python socket.getprotobyname() Examples
The following are 30
code examples of socket.getprotobyname().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
socket
, or try the search function
.
Example #1
Source File: checks.py From ansible-pfsense with GNU General Public License v3.0 | 6 votes |
def check_name(self, name, objtype): """ check name validy """ msg = None if len(name) >= 32 or len(re.findall(r'(^_*$|^\d*$|[^a-zA-Z0-9_])', name)) > 0: msg = "The {0} name must be less than 32 characters long, may not consist of only numbers, may not consist of only underscores, ".format(objtype) msg += "and may only contain the following characters: a-z, A-Z, 0-9, _" elif name in ["port", "pass"]: msg = "The {0} name must not be either of the reserved words 'port' or 'pass'".format(objtype) else: try: socket.getprotobyname(name) msg = 'The {0} name must not be an IP protocol name such as TCP, UDP, ICMP etc.'.format(objtype) except socket.error: pass try: socket.getservbyname(name) msg = 'The {0} name must not be a well-known or registered TCP or UDP port name such as ssh, smtp, pop3, tftp, http, openvpn etc.'.format(objtype) except socket.error: pass if msg is not None: self.module.fail_json(msg=msg)
Example #2
Source File: ping.py From NodePingManage with Apache License 2.0 | 6 votes |
def do_one(dest_addr, timeout): """ Returns either the delay (in seconds) or none on timeout. """ icmp = socket.getprotobyname("icmp") try: my_socket = socket.socket(socket.AF_INET, socket.SOCK_RAW, icmp) except socket.error, (errno, msg): if errno == 1: # Operation not permitted msg = msg + ( " - Note that ICMP messages can only be sent from processes" " running as root." ) raise socket.error(msg) raise # raise the original error
Example #3
Source File: ping.py From g3ar with BSD 2-Clause "Simplified" License | 6 votes |
def do_one(dest_addr, timeout): """ Returns either the delay (in seconds) or none on timeout. """ icmp = socket.getprotobyname("icmp") try: my_socket = socket.socket(socket.AF_INET, socket.SOCK_RAW, icmp) except socket.error as xxx_todo_changeme: (errno, msg) = xxx_todo_changeme.args if errno == 1: # Operation not permitted msg = msg + ( " - Note that ICMP messages can only be sent from processes" " running as root." ) raise socket.error(msg) raise # raise the original error my_ID = os.getpid() & 0xFFFF send_one_ping(my_socket, dest_addr, my_ID) delay = receive_one_ping(my_socket, my_ID, timeout) my_socket.close() return delay
Example #4
Source File: ping.py From g3ar with BSD 2-Clause "Simplified" License | 6 votes |
def do_one(dest_addr, timeout): """ Returns either the delay (in seconds) or none on timeout. """ icmp = socket.getprotobyname("icmp") try: my_socket = socket.socket(socket.AF_INET, socket.SOCK_RAW, icmp) except socket.error as xxx_todo_changeme: (errno, msg) = xxx_todo_changeme.args if errno == 1: # Operation not permitted msg = msg + ( " - Note that ICMP messages can only be sent from processes" " running as root." ) raise socket.error(msg) raise # raise the original error my_ID = os.getpid() & 0xFFFF send_one_ping(my_socket, dest_addr, my_ID) delay = receive_one_ping(my_socket, my_ID, timeout) my_socket.close() return delay
Example #5
Source File: 13_2_ping_remote_host.py From Python-Network-Programming with MIT License | 6 votes |
def ping_once(self): """ Returns the delay (in seconds) or none on timeout. """ icmp = socket.getprotobyname("icmp") try: sock = socket.socket(socket.AF_INET, socket.SOCK_RAW, icmp) except socket.error as e: if e.errno == 1: # Not superuser, so operation not permitted e.msg += "ICMP messages can only be sent from root user processes" raise socket.error(e.msg) except Exception as e: print ("Exception: %s" %(e)) my_ID = os.getpid() & 0xFFFF self.send_ping(sock, my_ID) delay = self.receive_pong(sock, my_ID, self.timeout) sock.close() return delay
Example #6
Source File: socket_handler.py From traceflow with BSD 3-Clause "New" or "Revised" License | 6 votes |
def __init__(self, ip_daddr): # We're only interested in ICMP, so happy to have this hard coded. try: self.icmp_listener = socket.socket( socket.AF_INET, socket.SOCK_RAW, socket.getprotobyname("icmp") ) except PermissionError as e: print(e) print("Please run as root!") exit(1) # TODO: Test Timestamps correctly try: SO_TIMESTAMPNS = 35 self.icmp_listener.setsockopt(socket.SOL_SOCKET, SO_TIMESTAMPNS, 1) except OSError as e: logging.debug("Timestamps not available, continuing without them for now") self.ip_daddr = ip_daddr self.mutex = threading.Lock() logging.debug("Starting") self.icmp_packets = dict() t = threading.Thread(target=self.listener) t.setDaemon(True) t.start()
Example #7
Source File: __init__.py From BitTorrent with GNU General Public License v3.0 | 6 votes |
def __init__(self, unix_socket_fname = ""): Thread.__init__(self) self.id = os.getpid() if not unix_socket_fname: unix_socket_fname = os.path.join(platform.get_dot_dir(), "xicmp-unix-socket" ) self.ux_fname = unix_socket_fname self.files = {} # id -> IcmpFile self.dfs = {} # id -> deferreds. A given id may have multiple deferreds. #self.sock = socket.socket(socket.AF_INET,socket.SOCK_RAW, # socket.getprotobyname('icmp')) self.sock = open( unix_socket_fname, "rw" ) os.setuid(os.getuid()) # give up root access as quickly as possible. self.sock.settimeout(2.0) # times out to periodically check done flag. # Separate from time outs on icmp messages. #self.lock = Condition() # simliar to a semaphore, but has only two # # states: locked and unlocked.
Example #8
Source File: network.py From pythonping with MIT License | 6 votes |
def __init__(self, destination, protocol, source=None, options=(), buffer_size=2048): """Creates a network socket to exchange messages :param destination: Destination IP address :type destination: str :param protocol: Name of the protocol to use :type protocol: str :param options: Options to set on the socket :type options: tuple :param source: Source IP to use - implemented in future releases :type source: Union[None, str] :param buffer_size: Size in bytes of the listening buffer for incoming packets (replies) :type buffer_size: int""" try: self.destination = socket.gethostbyname(destination) except socket.gaierror as e: raise RuntimeError('Cannot resolve address "' + destination + '", try verify your DNS or host file') self.protocol = socket.getprotobyname(protocol) self.buffer_size = buffer_size if source is not None: raise NotImplementedError('PythonPing currently does not support specification of source IP') self.socket = socket.socket(socket.AF_INET, socket.SOCK_RAW, self.protocol) if options: self.socket.setsockopt(*options)
Example #9
Source File: 3_2_ping_remote_host.py From Python-Network-Programming-Cookbook-Second-Edition with MIT License | 6 votes |
def ping_once(self): """ Returns the delay (in seconds) or none on timeout. """ icmp = socket.getprotobyname("icmp") try: sock = socket.socket(socket.AF_INET, socket.SOCK_RAW, icmp) except socket.error as e: if e.errno == 1: # Not superuser, so operation not permitted e.msg += "ICMP messages can only be sent from root user processes" raise socket.error(e.msg) except Exception as e: print ("Exception: %s" %(e)) my_ID = os.getpid() & 0xFFFF self.send_ping(sock, my_ID) delay = self.receive_pong(sock, my_ID, self.timeout) sock.close() return delay
Example #10
Source File: nmap.py From PythonMiniProbe with BSD 3-Clause "New" or "Revised" License | 6 votes |
def do_one_ping(self, dest_addr, timeout): """ Returns either the delay (in seconds) or none on timeout. """ icmp = socket.getprotobyname("icmp") try: my_socket = socket.socket(socket.AF_INET, socket.SOCK_RAW, icmp) except socket.error as serr: if serr.errno == 1: # Operation not permitted serr.msg += ( " - Note that ICMP messages can only be sent from processes" " running as root." ) raise socket.error(serr.msg) raise # raise the original error my_ID = os.getpid() & 0xFFFF self.send_one_ping(my_socket, dest_addr, my_ID) delay = self.receive_one_ping(my_socket, my_ID, timeout) my_socket.close() return delay
Example #11
Source File: WKS.py From arissploit with GNU General Public License v3.0 | 5 votes |
def from_text(cls, rdclass, rdtype, tok, origin=None, relativize=True): address = tok.get_string() protocol = tok.get_string() if protocol.isdigit(): protocol = int(protocol) else: protocol = socket.getprotobyname(protocol) bitmap = bytearray() while 1: token = tok.get().unescape() if token.is_eol_or_eof(): break if token.value.isdigit(): serv = int(token.value) else: if protocol != _proto_udp and protocol != _proto_tcp: raise NotImplementedError("protocol must be TCP or UDP") if protocol == _proto_udp: protocol_text = "udp" else: protocol_text = "tcp" serv = socket.getservbyname(token.value, protocol_text) i = serv // 8 l = len(bitmap) if l < i + 1: for j in xrange(l, i + 1): bitmap.append(0) bitmap[i] = bitmap[i] | (0x80 >> (serv % 8)) bitmap = dns.rdata._truncate_bitmap(bitmap) return cls(rdclass, rdtype, address, protocol, bitmap)
Example #12
Source File: test_socket.py From CTFCrackTools with GNU General Public License v3.0 | 5 votes |
def testGetProtoByName(self): self.failUnlessEqual(socket.IPPROTO_TCP, socket.getprotobyname("tcp")) self.failUnlessEqual(socket.IPPROTO_UDP, socket.getprotobyname("udp")) try: result = socket.getprotobyname("nosuchproto") except socket.error: pass except Exception, x: self.fail("getprotobyname raised wrong exception for unknown protocol: %s" % str(x))
Example #13
Source File: test_socket.py From medicare-demo with Apache License 2.0 | 5 votes |
def testGetProtoByName(self): self.failUnlessEqual(socket.IPPROTO_TCP, socket.getprotobyname("tcp")) self.failUnlessEqual(socket.IPPROTO_UDP, socket.getprotobyname("udp")) try: result = socket.getprotobyname("nosuchproto") except socket.error: pass except Exception, x: self.fail("getprotobyname raised wrong exception for unknown protocol: %s" % str(x))
Example #14
Source File: WKS.py From Cloudmare with GNU General Public License v3.0 | 5 votes |
def from_text(cls, rdclass, rdtype, tok, origin=None, relativize=True): address = tok.get_string() protocol = tok.get_string() if protocol.isdigit(): protocol = int(protocol) else: protocol = socket.getprotobyname(protocol) bitmap = bytearray() while 1: token = tok.get().unescape() if token.is_eol_or_eof(): break if token.value.isdigit(): serv = int(token.value) else: if protocol != _proto_udp and protocol != _proto_tcp: raise NotImplementedError("protocol must be TCP or UDP") if protocol == _proto_udp: protocol_text = "udp" else: protocol_text = "tcp" serv = socket.getservbyname(token.value, protocol_text) i = serv // 8 l = len(bitmap) if l < i + 1: for j in xrange(l, i + 1): bitmap.append(0) bitmap[i] = bitmap[i] | (0x80 >> (serv % 8)) bitmap = thirdparty.dns.rdata._truncate_bitmap(bitmap) return cls(rdclass, rdtype, address, protocol, bitmap)
Example #15
Source File: WKS.py From elasticintel with GNU General Public License v3.0 | 5 votes |
def from_text(cls, rdclass, rdtype, tok, origin=None, relativize=True): address = tok.get_string() protocol = tok.get_string() if protocol.isdigit(): protocol = int(protocol) else: protocol = socket.getprotobyname(protocol) bitmap = bytearray() while 1: token = tok.get().unescape() if token.is_eol_or_eof(): break if token.value.isdigit(): serv = int(token.value) else: if protocol != _proto_udp and protocol != _proto_tcp: raise NotImplementedError("protocol must be TCP or UDP") if protocol == _proto_udp: protocol_text = "udp" else: protocol_text = "tcp" serv = socket.getservbyname(token.value, protocol_text) i = serv // 8 l = len(bitmap) if l < i + 1: for j in xrange(l, i + 1): bitmap.append(0) bitmap[i] = bitmap[i] | (0x80 >> (serv % 8)) bitmap = dns.rdata._truncate_bitmap(bitmap) return cls(rdclass, rdtype, address, protocol, bitmap)
Example #16
Source File: portscan.py From Scanver with Apache License 2.0 | 5 votes |
def __icmpSocket(self): '''创建ICMP Socket''' if not self.IPv6: Sock = socket.socket(socket.AF_INET, socket.SOCK_RAW, socket.getprotobyname("icmp")) else: Sock = socket.socket(socket.AF_INET6, socket.SOCK_RAW, socket.getprotobyname("ipv6-icmp")) return Sock
Example #17
Source File: F-Scrack.py From F-Scrack with GNU General Public License v3.0 | 5 votes |
def __icmpSocket(self): Sock = socket.socket(socket.AF_INET, socket.SOCK_RAW, socket.getprotobyname("icmp")) return Sock
Example #18
Source File: test_socket.py From CTFCrackTools-V2 with GNU General Public License v3.0 | 5 votes |
def testGetProtoByName(self): self.failUnlessEqual(socket.IPPROTO_TCP, socket.getprotobyname("tcp")) self.failUnlessEqual(socket.IPPROTO_UDP, socket.getprotobyname("udp")) try: result = socket.getprotobyname("nosuchproto") except socket.error: pass except Exception, x: self.fail("getprotobyname raised wrong exception for unknown protocol: %s" % str(x))
Example #19
Source File: WKS.py From bazarr with GNU General Public License v3.0 | 5 votes |
def from_text(cls, rdclass, rdtype, tok, origin=None, relativize=True): address = tok.get_string() protocol = tok.get_string() if protocol.isdigit(): protocol = int(protocol) else: protocol = socket.getprotobyname(protocol) bitmap = bytearray() while 1: token = tok.get().unescape() if token.is_eol_or_eof(): break if token.value.isdigit(): serv = int(token.value) else: if protocol != _proto_udp and protocol != _proto_tcp: raise NotImplementedError("protocol must be TCP or UDP") if protocol == _proto_udp: protocol_text = "udp" else: protocol_text = "tcp" serv = socket.getservbyname(token.value, protocol_text) i = serv // 8 l = len(bitmap) if l < i + 1: for j in xrange(l, i + 1): bitmap.append(0) bitmap[i] = bitmap[i] | (0x80 >> (serv % 8)) bitmap = dns.rdata._truncate_bitmap(bitmap) return cls(rdclass, rdtype, address, protocol, bitmap)
Example #20
Source File: F-Scrack.py From F-Scrack with GNU General Public License v3.0 | 5 votes |
def __icmpSocket(self): Sock = socket.socket(socket.AF_INET, socket.SOCK_RAW, socket.getprotobyname("icmp")) return Sock
Example #21
Source File: Ping.py From Computer-Networking-A-Top-Down-Approach-NOTES with MIT License | 5 votes |
def doOnePing(destAddr, ID, sequence, timeout): icmp = socket.getprotobyname("icmp") # SOCK_RAW is a powerful socket type. For more details see: http://sock-raw.org/papers/sock_raw # Fill in start mySocket = socket.socket(socket.AF_INET, socket.SOCK_RAW, icmp) # Fill in end sendOnePing(mySocket, ID, sequence, destAddr) delay = receiveOnePing(mySocket, ID, sequence, destAddr, timeout) mySocket.close() return delay
Example #22
Source File: WKS.py From Tautulli with GNU General Public License v3.0 | 5 votes |
def from_text(cls, rdclass, rdtype, tok, origin=None, relativize=True): address = tok.get_string() protocol = tok.get_string() if protocol.isdigit(): protocol = int(protocol) else: protocol = socket.getprotobyname(protocol) bitmap = bytearray() while 1: token = tok.get().unescape() if token.is_eol_or_eof(): break if token.value.isdigit(): serv = int(token.value) else: if protocol != _proto_udp and protocol != _proto_tcp: raise NotImplementedError("protocol must be TCP or UDP") if protocol == _proto_udp: protocol_text = "udp" else: protocol_text = "tcp" serv = socket.getservbyname(token.value, protocol_text) i = serv // 8 l = len(bitmap) if l < i + 1: for j in xrange(l, i + 1): bitmap.append(0) bitmap[i] = bitmap[i] | (0x80 >> (serv % 8)) bitmap = dns.rdata._truncate_bitmap(bitmap) return cls(rdclass, rdtype, address, protocol, bitmap)
Example #23
Source File: icmp.py From dizzy with BSD 3-Clause "New" or "Revised" License | 5 votes |
def __init__(self, section_proxy): check_root("use the ICMP probe") self.target_host = section_proxy.get('target_host') self.timeout = section_proxy.getfloat('timeout', 1) self.pkg_size = section_proxy.getint('pkg_size', 64) self.retry = section_proxy.getint('retry', 2) self.socket = None self.is_open = False try: inet_aton(self.target_host) self.af = AF_INET self.proto = getprotobyname("icmp") echo = self.ICMP_ECHO except Exception as e: try: inet_pton(AF_INET6, self.target_host) self.af = AF_INET6 self.proto = getprotobyname("ipv6-icmp") echo = self.ICMP6_ECHO except Exception as f: raise ProbeParseException("probe/icmp: unknown address family: %s: %s, %s" % (self.target_host, e, f)) self.pid = getpid() & 0xFFFF self.header = pack("!BBHHH", echo, 0, 0, self.pid, 0) pad = list() for i in range(0x41, 0x41 + self.pkg_size): pad += [(i & 0xff)] self.data = bytearray(pad) checksum = csum_inet(self.header + self.data) self.header = self.header[0:2] + checksum + self.header[4:]
Example #24
Source File: icmp.py From peach with Mozilla Public License 2.0 | 5 votes |
def connect(self): if self._socket is not None: # Close out old socket first self._socket.close() self._socket = socket.socket(socket.AF_INET, socket.SOCK_RAW, socket.getprotobyname('icmp')) self._socket.connect((self._host, 22))
Example #25
Source File: WKS.py From luscan-devel with GNU General Public License v2.0 | 5 votes |
def from_text(cls, rdclass, rdtype, tok, origin = None, relativize = True): address = tok.get_string() protocol = tok.get_string() if protocol.isdigit(): protocol = int(protocol) else: protocol = socket.getprotobyname(protocol) bitmap = [] while 1: token = tok.get().unescape() if token.is_eol_or_eof(): break if token.value.isdigit(): serv = int(token.value) else: if protocol != _proto_udp and protocol != _proto_tcp: raise NotImplementedError("protocol must be TCP or UDP") if protocol == _proto_udp: protocol_text = "udp" else: protocol_text = "tcp" serv = socket.getservbyname(token.value, protocol_text) i = serv // 8 l = len(bitmap) if l < i + 1: for j in xrange(l, i + 1): bitmap.append('\x00') bitmap[i] = chr(ord(bitmap[i]) | (0x80 >> (serv % 8))) bitmap = dns.rdata._truncate_bitmap(bitmap) return cls(rdclass, rdtype, address, protocol, bitmap)
Example #26
Source File: recipe-409689.py From code with MIT License | 5 votes |
def ping(addr): print "PING (%s): %d data bytes" % (addr,datalen) ## create socket s = socket.socket(socket.AF_INET,socket.SOCK_RAW, socket.getprotobyname('icmp')) s.connect((addr,22)) ## setuid back to normal user os.setuid(os.getuid()) seq_num = 0 packet_count = 0 process_id = os.getpid() base_packet = Packet((8,0)) while 1: ## create ping packet seq_num += 1 pdata = struct.pack("!HHd",process_id,seq_num,time.time()) ## send initial packet base_packet.data = pdata s.send(base_packet.packet) ## recv packet buf = s.recv(BUFSIZE) current_time = time.time() ## parse packet; remove IP header first r = Packet.parse(buf[20:]) ## parse ping data (ident,seq,timestamp) = struct.unpack("!HHd",r.data) ## calculate rounttrip time rtt = current_time - timestamp rtt *= 1000 print "%d bytes from %s: id=%s, seq=%u, rtt=%.3f ms" % (len(buf), addr, ident, seq, rtt) time.sleep(1)
Example #27
Source File: test__socket.py From ironpython3 with Apache License 2.0 | 5 votes |
def test_getprotobyname(self): '''Tests _socket.getprotobyname''' #IP and CPython proto_map = { "icmp": _socket.IPPROTO_ICMP, "ip": _socket.IPPROTO_IP, "tcp": _socket.IPPROTO_TCP, "udp": _socket.IPPROTO_UDP, } #supported only by IP if is_cli: proto_map.update( {"dstopts": _socket.IPPROTO_DSTOPTS, "none": _socket.IPPROTO_NONE, "raw": _socket.IPPROTO_RAW, "ipv4": _socket.IPPROTO_IPV4, "ipv6": _socket.IPPROTO_IPV6, "esp": _socket.IPPROTO_ESP, "fragment": _socket.IPPROTO_FRAGMENT, "nd": _socket.IPPROTO_ND, "icmpv6": _socket.IPPROTO_ICMPV6, "routing": _socket.IPPROTO_ROUTING, "pup": _socket.IPPROTO_PUP, #http://ironpython.codeplex.com/WorkItem/View.aspx?WorkItemId=21918 "ggp": _socket.IPPROTO_GGP, #http://ironpython.codeplex.com/WorkItem/View.aspx?WorkItemId=21918 }) for proto_name, good_val in proto_map.items(): temp_val = _socket.getprotobyname(proto_name) self.assertEqual(temp_val, good_val) #negative cases bad_list = ["", "blah", "i"] for name in bad_list: self.assertRaises(_socket.error, _socket.getprotobyname, name)
Example #28
Source File: WKS.py From script.elementum.burst with Do What The F*ck You Want To Public License | 5 votes |
def from_text(cls, rdclass, rdtype, tok, origin=None, relativize=True): address = tok.get_string() protocol = tok.get_string() if protocol.isdigit(): protocol = int(protocol) else: protocol = socket.getprotobyname(protocol) bitmap = bytearray() while 1: token = tok.get().unescape() if token.is_eol_or_eof(): break if token.value.isdigit(): serv = int(token.value) else: if protocol != _proto_udp and protocol != _proto_tcp: raise NotImplementedError("protocol must be TCP or UDP") if protocol == _proto_udp: protocol_text = "udp" else: protocol_text = "tcp" serv = socket.getservbyname(token.value, protocol_text) i = serv // 8 l = len(bitmap) if l < i + 1: for j in xrange(l, i + 1): bitmap.append(0) bitmap[i] = bitmap[i] | (0x80 >> (serv % 8)) bitmap = dns.rdata._truncate_bitmap(bitmap) return cls(rdclass, rdtype, address, protocol, bitmap)
Example #29
Source File: ping.py From stash with MIT License | 5 votes |
def do_one(dest_addr, timeout): """ Returns either the delay (in seconds) or none on timeout. """ icmp = socket.getprotobyname("icmp") my_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, icmp) my_ID = os.getpid() & 0xFFFF send_one_ping(my_socket, dest_addr, my_ID) delay = receive_one_ping(my_socket, my_ID, timeout) my_socket.close() return delay
Example #30
Source File: plain_coap_client.py From py-air-control with MIT License | 5 votes |
def _send_over_socket(self, destination, packet): protocol = socket.getprotobyname("icmp") if os.geteuid() == 0: s = socket.socket(socket.AF_INET, socket.SOCK_RAW, protocol) else: s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, protocol) try: s.sendto(packet, (destination, 0)) except OSError: # That fixes a mac os bug for me: OSError: [Errno 22] Invalid argument pass finally: s.close()