Python Forum
A lot of confusion and I can't seem to solve this issue
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
A lot of confusion and I can't seem to solve this issue
#1
So I have download a list of "open IP" from shodan which was about 1,566,785 open IP address
Now to check all this IP address one by one will take me years or if not it's impossible.. But with the help of python it is possible, for instance we can scan all this ip which will be store in one file called ip.txt and than we can scan all this ip and than output the result in a new file called scan_ip.txt

#Opening the file and reading the file
file_read = open('ip.txt', 'r')
IP_addr = file_read.read()

for line in IP_addr:
    search_with_this_command = (curl -X GET "http://{IP_addr}:8080/")
    #while searching with this command "curl -X GET "http://{ip.txt}:8080/" wait for 5 second and if no respond continue with the next ip address
    #output all the scan result scan_ip.txt
    #The output should be whatever it shows while using this command (curl -X GET "http://{ip.txt}:8080/")
This is how the output looks like this
green  open .geoip_databases 1TF0BMOURMC1EVcgxxL4Nw 1 0        40 40 37.7mb 37.7mb
yellow open service          sVMouTqjQpCuS4v2oOgdRA 1 1         1  0 20.3kb 20.3kb
Can anyone of you help me make this program? This is for my own educational purpose's.
Thank you
Reply
#2
I'll be willing to give anyone some btc if he can do this for me.
Thank you
Reply
#3
(Jul-23-2022, 05:50 PM)Calli Wrote: I'll be willing to give anyone some btc if he can do this for me.
Thank you
If want to pay post in Jobs Thread not here.

Here some help and don't need curl for this.
icmplib is great for this task.
Example with multiping
Get the list for a file can bee done like this.
ip_lst.txt
Output:
python-forum.io 10.0.0.5 127.0.0.1 ::1
I set down count/time it will use to check count=1, interval=0.2, timeout=1,so it will be fast be can be less accurate so short count/times .
Can set parameters to what you want.
from icmplib import multiping

def read_ip() -> list:
    with open('ip_lst.txt') as f:
       return [i.strip() for i in f]

def check_ip(ip_lst : list):
    hosts = multiping(ip_lst, count=1, interval=0.2, timeout=1)
    for host in hosts:
        if host.is_alive:
            print(f'{host.address} is up!')
        else:
            print(f'{host.address} is down!')

if __name__ == '__main__':
    ip_lst = read_ip()
    check_ip(ip_lst)
Output:
172.67.168.227 is up! 10.0.0.5 is down! 127.0.0.1 is up! ::1 is up!
Calli likes this post
Reply
#4
(Jul-23-2022, 07:39 PM)snippsat Wrote:
(Jul-23-2022, 05:50 PM)Calli Wrote: I'll be willing to give anyone some btc if he can do this for me.
Thank you
If want to pay post in Jobs Thread not here.

Here some help and don't need curl for this.
icmplib is great for this task.
Example with multiping
Get the list for a file can bee done like this.
ip_lst.txt
Output:
python-forum.io 10.0.0.5 127.0.0.1 ::1
I set down count/time it will use to check count=1, interval=0.2, timeout=1,so it will be fast be can be less accurate so short count/times .
Can set parameters to what you want.
from icmplib import multiping

def read_ip() -> list:
    with open('ip_lst.txt') as f:
       return [i.strip() for i in f]

def check_ip(ip_lst : list):
    hosts = multiping(ip_lst, count=1, interval=0.2, timeout=1)
    for host in hosts:
        if host.is_alive:
            print(f'{host.address} is up!')
        else:
            print(f'{host.address} is down!')

if __name__ == '__main__':
    ip_lst = read_ip()
    check_ip(ip_lst)
Output:
172.67.168.227 is up! 10.0.0.5 is down! 127.0.0.1 is up! ::1 is up!

Thanks for sharing this but what you have shared is completely the opposite of what i wanted done. For instance if I check this IP
118.31.106.59 is down!

It says that it's down but the fact that this is not down when you check using a specific port which in this case is 9200.
The search should be like this
curl -X GET "http://118.31.106.59:9200/_cat/indices"
Result I get
green open .geoip_databases vtWge-U1SLefV6CuavYxCQ 1 0 40 40 37.7mb 37.7mb

Now I want to safe the scan result like this in a different file scan_output.txt
118.31.106.59
green open .geoip_databases vtWge-U1SLefV6CuavYxCQ 1 0 40 40 37.7mb 37.7mb
Reply
#5
Ok if it like that can use Requests,the curl command would be like this in Requests.
import requests

response = requests.get('http://118.31.106.59:9200/_cat/indices')
print(response.text.strip())

# Parse out ip
ip = response.url.split('/')[2]
print(ip)
Output:
green open .geoip_databases vtWge-U1SLefV6CuavYxCQ 1 0 40 40 37.7mb 37.7mb 118.31.106.59:9200
Reply
#6
(Jul-24-2022, 07:26 AM)snippsat Wrote: Ok if it like that can use Requests,the curl command would be like this in Requests.
import requests

response = requests.get('http://118.31.106.59:9200/_cat/indices')
print(response.text.strip())

# Parse out ip
ip = response.url.split('/')[2]
print(ip)
Output:
green open .geoip_databases vtWge-U1SLefV6CuavYxCQ 1 0 40 40 37.7mb 37.7mb 118.31.106.59:9200

This is working fine but how do you loop through all the ip which is in a file?
import requests

file_read = open('ip.txt', 'r')
ip_list = file_read.read()

response = requests.get("http://",{ip_list},":9200/_cat/indices")
print(response.text.strip())
 
# Parse out ip
ip = response.url.split('/')[2]
print(ip)
Reply
#7
(Jul-24-2022, 04:55 PM)Calli Wrote: This is working fine but how do you loop through all the ip which is in a file?
When you do this it will just be one big string object,that you can not iterate over.
Have to spilt it up for this to work.
ip_list = file_read.read()
Let say this is content.
ip_lst1.txt
Output:
118.31.106.59 118.31.106.59 118.31.106.59
Can just iterated over file object,no need to read()(and split it up).
import requests

with open('ip_lst1.txt') as f:
    for ip in f:
        ip = ip.strip()
        print(ip)
        response = requests.get(f"http://{ip}:9200/_cat/indices")
        print(response.text.strip())
Output:
118.31.106.59 green open .geoip_databases vtWge-U1SLefV6CuavYxCQ 1 0 40 40 37.7mb 37.7mb 118.31.106.59 green open .geoip_databases vtWge-U1SLefV6CuavYxCQ 1 0 40 40 37.7mb 37.7mb 118.31.106.59 green open .geoip_databases vtWge-U1SLefV6CuavYxCQ 1 0 40 40 37.7mb 37.7mb
Doing most task will be will be a struggle if you don't spend time learning📜 basic Python.
Reply
#8
(Jul-24-2022, 05:36 PM)snippsat Wrote:
(Jul-24-2022, 04:55 PM)Calli Wrote: This is working fine but how do you loop through all the ip which is in a file?
When you do this it will just be one big string object,that you can not iterate over.
Have to spilt it up for this to work.
ip_list = file_read.read()
Let say this is content.
ip_lst1.txt
Output:
118.31.106.59 118.31.106.59 118.31.106.59
Can just iterated over file object,no need to read()(and split it up).
import requests

with open('ip_lst1.txt') as f:
    for ip in f:
        ip = ip.strip()
        print(ip)
        response = requests.get(f"http://{ip}:9200/_cat/indices")
        print(response.text.strip())
Output:
118.31.106.59 green open .geoip_databases vtWge-U1SLefV6CuavYxCQ 1 0 40 40 37.7mb 37.7mb 118.31.106.59 green open .geoip_databases vtWge-U1SLefV6CuavYxCQ 1 0 40 40 37.7mb 37.7mb 118.31.106.59 green open .geoip_databases vtWge-U1SLefV6CuavYxCQ 1 0 40 40 37.7mb 37.7mb
Doing most task will be will be a struggle if you don't spend time learning📜 basic Python.

I'm learning python for beginner but it might take some time for be to be professional like you.. But I'm getting this error

124.221.82.115
Traceback (most recent call last):
  File "/usr/local/lib/python3.10/dist-packages/urllib3/connection.py", line 158, in _new_conn
    conn = connection.create_connection(
  File "/usr/local/lib/python3.10/dist-packages/urllib3/util/connection.py", line 80, in create_connection
    raise err
  File "/usr/local/lib/python3.10/dist-packages/urllib3/util/connection.py", line 70, in create_connection
    sock.connect(sa)
ConnectionRefusedError: [Errno 111] Connection refused

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/local/lib/python3.10/dist-packages/urllib3/connectionpool.py", line 597, in urlopen
    httplib_response = self._make_request(conn, method, url,
  File "/usr/local/lib/python3.10/dist-packages/urllib3/connectionpool.py", line 354, in _make_request
    conn.request(method, url, **httplib_request_kw)
  File "/usr/lib/python3.10/http/client.py", line 1282, in request
    self._send_request(method, url, body, headers, encode_chunked)
  File "/usr/lib/python3.10/http/client.py", line 1328, in _send_request
    self.endheaders(body, encode_chunked=encode_chunked)
  File "/usr/lib/python3.10/http/client.py", line 1277, in endheaders
    self._send_output(message_body, encode_chunked=encode_chunked)
  File "/usr/lib/python3.10/http/client.py", line 1037, in _send_output
    self.send(msg)
  File "/usr/lib/python3.10/http/client.py", line 975, in send
    self.connect()
  File "/usr/local/lib/python3.10/dist-packages/urllib3/connection.py", line 181, in connect
    conn = self._new_conn()
  File "/usr/local/lib/python3.10/dist-packages/urllib3/connection.py", line 167, in _new_conn
    raise NewConnectionError(
urllib3.exceptions.NewConnectionError: <urllib3.connection.HTTPConnection object at 0x7f96018eaf50>: Failed to establish a new connection: [Errno 111] Connection refused

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/local/lib/python3.10/dist-packages/requests/adapters.py", line 489, in send
    resp = conn.urlopen(
  File "/usr/local/lib/python3.10/dist-packages/urllib3/connectionpool.py", line 637, in urlopen
    retries = retries.increment(method, url, error=e, _pool=self,
  File "/usr/local/lib/python3.10/dist-packages/urllib3/util/retry.py", line 399, in increment
    raise MaxRetryError(_pool, url, error or ResponseError(cause))
urllib3.exceptions.MaxRetryError: HTTPConnectionPool(host='124.221.82.115', port=9200): Max retries exceeded with url: /_cat/indices (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x7f96018eaf50>: Failed to establish a new connection: [Errno 111] Connection refused'))

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/media/computer/IP/scan.py", line 7, in <module>
    response = requests.get(f"http://{ip}:9200/_cat/indices")
  File "/usr/local/lib/python3.10/dist-packages/requests/api.py", line 73, in get
    return request("get", url, params=params, **kwargs)
  File "/usr/local/lib/python3.10/dist-packages/requests/api.py", line 59, in request
    return session.request(method=method, url=url, **kwargs)
  File "/usr/local/lib/python3.10/dist-packages/requests/sessions.py", line 587, in request
    resp = self.send(prep, **send_kwargs)
  File "/usr/local/lib/python3.10/dist-packages/requests/sessions.py", line 701, in send
    r = adapter.send(request, **kwargs)
  File "/usr/local/lib/python3.10/dist-packages/requests/adapters.py", line 565, in send
    raise ConnectionError(e, request=request)
requests.exceptions.ConnectionError: HTTPConnectionPool(host='124.221.82.115', port=9200): Max retries exceeded with url: /_cat/indices (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x7f96018eaf50>: Failed to establish a new connection: [Errno 111] Connection refused'))
IP
124.221.82.115
type Connection refused
can we add an exception or??
Reply
#9
(Jul-24-2022, 07:24 PM)Calli Wrote: can we add an exception or??
Yes can do that(pass it out so loop will not stop) and can also set shorter time so it goes faster on timeout errors.
import requests

with open('ip_lst1.txt') as f:
    for ip in f:
        try:
            ip = ip.strip()
            print(ip)
            response = requests.get(f"http://{ip}:9200/_cat/indices", timeout=2)
            print(response.text.strip())
        except Exception as error:
            #print(error)
            pass
Output:
118.31.106.59 green open .geoip_databases vtWge-U1SLefV6CuavYxCQ 1 0 40 40 37.7mb 37.7mb 118.31.106.59 green open .geoip_databases vtWge-U1SLefV6CuavYxCQ 1 0 40 40 37.7mb 37.7mb 124.221.82.115 # error still continue 118.31.106.59 green open .geoip_databases vtWge-U1SLefV6CuavYxCQ 1 0 40 40 37.7mb 37.7mb 118.31.106.70 # error
rob101 likes this post
Reply
#10
Hi mate! have you resolved your issue?
Reply


Forum Jump:

User Panel Messages

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