Hướng dẫn python ping ip address from text file - địa chỉ ip ping python từ tệp văn bản

Mã trhe lấy adress IP từ TXT. Khi máy chủ trực tuyến viết trong tệp TXT mới. Nhưng mã này chỉ viết tệp cuối cùng trong tệp TXT. Và không bao giờ thời gian khác. tôi cần giúp đỡ

Tahnks

import os


file = open["IPs.txt","r+"]

with open["IPs.txt","r"] as file:

  for line in file:
     response =  os.system["ping   " + line]

     if response == 0:
        with open["IPsCheck.txt","w"] as file:
            print[line]
            file.write[line]




     else:
        print["server not available "]

HTSHAME

5.9354 Huy hiệu vàng30 Huy hiệu bạc52 Huy hiệu Đồng4 gold badges30 silver badges52 bronze badges

Đã hỏi ngày 7 tháng 3 năm 2019 lúc 10:51Mar 7, 2019 at 10:51

0

Bạn cần mở tệp đầu ra [ipscheck.txt] ở chế độ phụ lục: "A+"

Xem ở đây để biết thêm //www.guru99.com/reading-and-writing-files-in-python.html#2

Mã dưới đây dường như hoạt động. Thay đổi gỡ lỗi thành sai nếu bạn muốn đọc IPS từ tệp IPS.

import os

DEBUG = True

DEBUG_IPS = ['1.1.1.1', '8.8.8.8']

if DEBUG:
    ips = DEBUG_IPS
else:
    with open["IPs.txt", "r+"] as ips_file:
        ips = [ip.strip[] for ip in ips_file.readlines[]]

with open["IPsCheck.txt", "w"] as available_ips_file:
    for ip in ips:
        response = os.system['ping {}'.format[ip]]
        if response == 0:
            available_ips_file.write[ip]
        else:
            print['server {} not available'.format[ip]]

Đã trả lời ngày 7 tháng 3 năm 2019 lúc 10:55Mar 7, 2019 at 10:55

Baldermanbalermanbalderman

21.9k6 Huy hiệu vàng32 Huy hiệu bạc47 Huy hiệu đồng6 gold badges32 silver badges47 bronze badges

3

Xem thảo luận

Cải thiện bài viết

Lưu bài viết

  • Đọc
  • Bàn luận
  • Xem thảo luận

    Cải thiện bài viết

    Lưu bài viết

    Đọc
     

    Bàn luận  

    • Hãy cho chúng tôi xem cách trích xuất địa chỉ IP từ một tệp bằng Python. & NBSP;
    • Thuật toán: & nbsp; & nbsp;
    • Nhập mô -đun lại cho biểu thức chính quy.
    • Mở tệp bằng hàm Open [].
       
    r'[\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}]'
    • Đọc tất cả các dòng trong tập tin và lưu trữ chúng trong một danh sách.
    • Tuyên bố mẫu cho địa chỉ IP. Mẫu regex là: & nbsp; & nbsp;

    Đối với mọi yếu tố của tìm kiếm danh sách cho mẫu bằng hàm search [], hãy lưu trữ địa chỉ IP trong danh sách.
     

    test.txt

    python3

    import re

    with open['C:/Users/user/Desktop/New Text Document.txt'] as fh:

    Hiển thị danh sách chứa địa chỉ IP.

    Tệp được xử lý là Test.txt: & nbsp; & nbsp;

    import os
    
    DEBUG = True
    
    DEBUG_IPS = ['1.1.1.1', '8.8.8.8']
    
    if DEBUG:
        ips = DEBUG_IPS
    else:
        with open["IPs.txt", "r+"] as ips_file:
            ips = [ip.strip[] for ip in ips_file.readlines[]]
    
    with open["IPsCheck.txt", "w"] as available_ips_file:
        for ip in ips:
            response = os.system['ping {}'.format[ip]]
            if response == 0:
                available_ips_file.write[ip]
            else:
                print['server {} not available'.format[ip]]
    
    0
    import os
    
    DEBUG = True
    
    DEBUG_IPS = ['1.1.1.1', '8.8.8.8']
    
    if DEBUG:
        ips = DEBUG_IPS
    else:
        with open["IPs.txt", "r+"] as ips_file:
            ips = [ip.strip[] for ip in ips_file.readlines[]]
    
    with open["IPsCheck.txt", "w"] as available_ips_file:
        for ip in ips:
            response = os.system['ping {}'.format[ip]]
            if response == 0:
                available_ips_file.write[ip]
            else:
                print['server {} not available'.format[ip]]
    
    1
    import os
    
    DEBUG = True
    
    DEBUG_IPS = ['1.1.1.1', '8.8.8.8']
    
    if DEBUG:
        ips = DEBUG_IPS
    else:
        with open["IPs.txt", "r+"] as ips_file:
            ips = [ip.strip[] for ip in ips_file.readlines[]]
    
    with open["IPsCheck.txt", "w"] as available_ips_file:
        for ip in ips:
            response = os.system['ping {}'.format[ip]]
            if response == 0:
                available_ips_file.write[ip]
            else:
                print['server {} not available'.format[ip]]
    
    2
    import os
    
    DEBUG = True
    
    DEBUG_IPS = ['1.1.1.1', '8.8.8.8']
    
    if DEBUG:
        ips = DEBUG_IPS
    else:
        with open["IPs.txt", "r+"] as ips_file:
            ips = [ip.strip[] for ip in ips_file.readlines[]]
    
    with open["IPsCheck.txt", "w"] as available_ips_file:
        for ip in ips:
            response = os.system['ping {}'.format[ip]]
            if response == 0:
                available_ips_file.write[ip]
            else:
                print['server {} not available'.format[ip]]
    
    3

    import os
    
    DEBUG = True
    
    DEBUG_IPS = ['1.1.1.1', '8.8.8.8']
    
    if DEBUG:
        ips = DEBUG_IPS
    else:
        with open["IPs.txt", "r+"] as ips_file:
            ips = [ip.strip[] for ip in ips_file.readlines[]]
    
    with open["IPsCheck.txt", "w"] as available_ips_file:
        for ip in ips:
            response = os.system['ping {}'.format[ip]]
            if response == 0:
                available_ips_file.write[ip]
            else:
                print['server {} not available'.format[ip]]
    
    4
    import os
    
    DEBUG = True
    
    DEBUG_IPS = ['1.1.1.1', '8.8.8.8']
    
    if DEBUG:
        ips = DEBUG_IPS
    else:
        with open["IPs.txt", "r+"] as ips_file:
            ips = [ip.strip[] for ip in ips_file.readlines[]]
    
    with open["IPsCheck.txt", "w"] as available_ips_file:
        for ip in ips:
            response = os.system['ping {}'.format[ip]]
            if response == 0:
                available_ips_file.write[ip]
            else:
                print['server {} not available'.format[ip]]
    
    2
    import os
    
    DEBUG = True
    
    DEBUG_IPS = ['1.1.1.1', '8.8.8.8']
    
    if DEBUG:
        ips = DEBUG_IPS
    else:
        with open["IPs.txt", "r+"] as ips_file:
            ips = [ip.strip[] for ip in ips_file.readlines[]]
    
    with open["IPsCheck.txt", "w"] as available_ips_file:
        for ip in ips:
            response = os.system['ping {}'.format[ip]]
            if response == 0:
                available_ips_file.write[ip]
            else:
                print['server {} not available'.format[ip]]
    
    6
    import os
    
    DEBUG = True
    
    DEBUG_IPS = ['1.1.1.1', '8.8.8.8']
    
    if DEBUG:
        ips = DEBUG_IPS
    else:
        with open["IPs.txt", "r+"] as ips_file:
            ips = [ip.strip[] for ip in ips_file.readlines[]]
    
    with open["IPsCheck.txt", "w"] as available_ips_file:
        for ip in ips:
            response = os.system['ping {}'.format[ip]]
            if response == 0:
                available_ips_file.write[ip]
            else:
                print['server {} not available'.format[ip]]
    
    7
    import os
    
    DEBUG = True
    
    DEBUG_IPS = ['1.1.1.1', '8.8.8.8']
    
    if DEBUG:
        ips = DEBUG_IPS
    else:
        with open["IPs.txt", "r+"] as ips_file:
            ips = [ip.strip[] for ip in ips_file.readlines[]]
    
    with open["IPsCheck.txt", "w"] as available_ips_file:
        for ip in ips:
            response = os.system['ping {}'.format[ip]]
            if response == 0:
                available_ips_file.write[ip]
            else:
                print['server {} not available'.format[ip]]
    
    8
    import os
    
    DEBUG = True
    
    DEBUG_IPS = ['1.1.1.1', '8.8.8.8']
    
    if DEBUG:
        ips = DEBUG_IPS
    else:
        with open["IPs.txt", "r+"] as ips_file:
            ips = [ip.strip[] for ip in ips_file.readlines[]]
    
    with open["IPsCheck.txt", "w"] as available_ips_file:
        for ip in ips:
            response = os.system['ping {}'.format[ip]]
            if response == 0:
                available_ips_file.write[ip]
            else:
                print['server {} not available'.format[ip]]
    
    9
    r'[\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}]'
    0

    import os
    
    DEBUG = True
    
    DEBUG_IPS = ['1.1.1.1', '8.8.8.8']
    
    if DEBUG:
        ips = DEBUG_IPS
    else:
        with open["IPs.txt", "r+"] as ips_file:
            ips = [ip.strip[] for ip in ips_file.readlines[]]
    
    with open["IPsCheck.txt", "w"] as available_ips_file:
        for ip in ips:
            response = os.system['ping {}'.format[ip]]
            if response == 0:
                available_ips_file.write[ip]
            else:
                print['server {} not available'.format[ip]]
    
    0
    r'[\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}]'
    9import0import1

    import2import3

    Đầu ra:

    Làm cách nào để trích xuất một địa chỉ IP từ một tệp văn bản trong Python?valid IP addresses.

    Hãy cho chúng tôi xem cách trích xuất địa chỉ IP từ một tệp bằng Python ..

    • Nhập mô -đun lại cho biểu thức chính quy ..
    • Mở tệp bằng hàm Open [] ..

    Đọc tất cả các dòng trong tệp và lưu trữ chúng trong danh sách ..
     

    Tuyên bố mẫu cho địa chỉ IP. Mẫu regex là:.

    Làm cách nào để trích xuất một địa chỉ IP từ một tệp văn bản?

    Làm cách nào để trích xuất IP từ văn bản ?.

    • 25 [0-5]-biểu thị số từ 250 đến 255
    • 2 [0-4] [0-9]-đại diện cho số từ 200 đến 249
    • [01]? [0-9] [0-9]?-đại diện cho các số từ 0 đến 199

    Tệp sẽ được xử lý là test2.txt: & nbsp; & nbsp;
     

    test2.txt 

    python3

    import re

    with open[import9] as fh:

    re1re2

    import os
    
    DEBUG = True
    
    DEBUG_IPS = ['1.1.1.1', '8.8.8.8']
    
    if DEBUG:
        ips = DEBUG_IPS
    else:
        with open["IPs.txt", "r+"] as ips_file:
            ips = [ip.strip[] for ip in ips_file.readlines[]]
    
    with open["IPsCheck.txt", "w"] as available_ips_file:
        for ip in ips:
            response = os.system['ping {}'.format[ip]]
            if response == 0:
                available_ips_file.write[ip]
            else:
                print['server {} not available'.format[ip]]
    
    2
    import os
    
    DEBUG = True
    
    DEBUG_IPS = ['1.1.1.1', '8.8.8.8']
    
    if DEBUG:
        ips = DEBUG_IPS
    else:
        with open["IPs.txt", "r+"] as ips_file:
            ips = [ip.strip[] for ip in ips_file.readlines[]]
    
    with open["IPsCheck.txt", "w"] as available_ips_file:
        for ip in ips:
            response = os.system['ping {}'.format[ip]]
            if response == 0:
                available_ips_file.write[ip]
            else:
                print['server {} not available'.format[ip]]
    
    3

    import os
    
    DEBUG = True
    
    DEBUG_IPS = ['1.1.1.1', '8.8.8.8']
    
    if DEBUG:
        ips = DEBUG_IPS
    else:
        with open["IPs.txt", "r+"] as ips_file:
            ips = [ip.strip[] for ip in ips_file.readlines[]]
    
    with open["IPsCheck.txt", "w"] as available_ips_file:
        for ip in ips:
            response = os.system['ping {}'.format[ip]]
            if response == 0:
                available_ips_file.write[ip]
            else:
                print['server {} not available'.format[ip]]
    
    4
    import os
    
    DEBUG = True
    
    DEBUG_IPS = ['1.1.1.1', '8.8.8.8']
    
    if DEBUG:
        ips = DEBUG_IPS
    else:
        with open["IPs.txt", "r+"] as ips_file:
            ips = [ip.strip[] for ip in ips_file.readlines[]]
    
    with open["IPsCheck.txt", "w"] as available_ips_file:
        for ip in ips:
            response = os.system['ping {}'.format[ip]]
            if response == 0:
                available_ips_file.write[ip]
            else:
                print['server {} not available'.format[ip]]
    
    2
    import os
    
    DEBUG = True
    
    DEBUG_IPS = ['1.1.1.1', '8.8.8.8']
    
    if DEBUG:
        ips = DEBUG_IPS
    else:
        with open["IPs.txt", "r+"] as ips_file:
            ips = [ip.strip[] for ip in ips_file.readlines[]]
    
    with open["IPsCheck.txt", "w"] as available_ips_file:
        for ip in ips:
            response = os.system['ping {}'.format[ip]]
            if response == 0:
                available_ips_file.write[ip]
            else:
                print['server {} not available'.format[ip]]
    
    6
    import os
    
    DEBUG = True
    
    DEBUG_IPS = ['1.1.1.1', '8.8.8.8']
    
    if DEBUG:
        ips = DEBUG_IPS
    else:
        with open["IPs.txt", "r+"] as ips_file:
            ips = [ip.strip[] for ip in ips_file.readlines[]]
    
    with open["IPsCheck.txt", "w"] as available_ips_file:
        for ip in ips:
            response = os.system['ping {}'.format[ip]]
            if response == 0:
                available_ips_file.write[ip]
            else:
                print['server {} not available'.format[ip]]
    
    7[

    r'[\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}]'
    0

    with 1

    import os
    
    DEBUG = True
    
    DEBUG_IPS = ['1.1.1.1', '8.8.8.8']
    
    if DEBUG:
        ips = DEBUG_IPS
    else:
        with open["IPs.txt", "r+"] as ips_file:
            ips = [ip.strip[] for ip in ips_file.readlines[]]
    
    with open["IPsCheck.txt", "w"] as available_ips_file:
        for ip in ips:
            response = os.system['ping {}'.format[ip]]
            if response == 0:
                available_ips_file.write[ip]
            else:
                print['server {} not available'.format[ip]]
    
    2
    r'[\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}]'
    3

    with 4

    import os
    
    DEBUG = True
    
    DEBUG_IPS = ['1.1.1.1', '8.8.8.8']
    
    if DEBUG:
        ips = DEBUG_IPS
    else:
        with open["IPs.txt", "r+"] as ips_file:
            ips = [ip.strip[] for ip in ips_file.readlines[]]
    
    with open["IPsCheck.txt", "w"] as available_ips_file:
        for ip in ips:
            response = os.system['ping {}'.format[ip]]
            if response == 0:
                available_ips_file.write[ip]
            else:
                print['server {} not available'.format[ip]]
    
    2
    r'[\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}]'
    3

    r'[\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}]'
    4
    r'[\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}]'
    5
    r'[\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}]'
    6 open0

    open1

    r'[\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}]'
    5
    import os
    
    DEBUG = True
    
    DEBUG_IPS = ['1.1.1.1', '8.8.8.8']
    
    if DEBUG:
        ips = DEBUG_IPS
    else:
        with open["IPs.txt", "r+"] as ips_file:
            ips = [ip.strip[] for ip in ips_file.readlines[]]
    
    with open["IPsCheck.txt", "w"] as available_ips_file:
        for ip in ips:
            response = os.system['ping {}'.format[ip]]
            if response == 0:
                available_ips_file.write[ip]
            else:
                print['server {} not available'.format[ip]]
    
    2 open4

    open1open6

    import os
    
    DEBUG = True
    
    DEBUG_IPS = ['1.1.1.1', '8.8.8.8']
    
    if DEBUG:
        ips = DEBUG_IPS
    else:
        with open["IPs.txt", "r+"] as ips_file:
            ips = [ip.strip[] for ip in ips_file.readlines[]]
    
    with open["IPsCheck.txt", "w"] as available_ips_file:
        for ip in ips:
            response = os.system['ping {}'.format[ip]]
            if response == 0:
                available_ips_file.write[ip]
            else:
                print['server {} not available'.format[ip]]
    
    2 open8

    open1[0 [1

    [2[3

    open1[5[6

    [2[8

    import2['C:/Users/user/Desktop/New Text Document.txt'1

    r'[\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}]'
    0

    import2'C:/Users/user/Desktop/New Text Document.txt'4

    import2['C:/Users/user/Desktop/New Text Document.txt'7

    r'[\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}]'
    0

    import2] as fh:0

    Đầu ra:


    Làm cách nào để trích xuất một địa chỉ IP từ một tệp văn bản trong Python?

    Hãy cho chúng tôi xem cách trích xuất địa chỉ IP từ một tệp bằng Python ...
    Nhập mô -đun lại cho biểu thức chính quy ..
    Mở tệp bằng hàm Open [] ..
    Đọc tất cả các dòng trong tệp và lưu trữ chúng trong danh sách ..
    Tuyên bố mẫu cho địa chỉ IP. Mẫu regex là:.

    Làm cách nào để trích xuất một địa chỉ IP từ một tệp văn bản?

    Làm cách nào để trích xuất IP từ văn bản ?..
    Mở công cụ trích xuất IP trực tuyến miễn phí ..
    Nhập văn bản chứa IPS.Công cụ hỗ trợ cả địa chỉ IPv4 và IPv6 ..
    Sau khi nhập văn bản, nhấp vào nút "Trích xuất IPS" ..
    Công cụ sẽ trích xuất tất cả các trường hợp của địa chỉ IP từ văn bản và cung cấp cho bạn kết quả mong muốn ..

    Làm cách nào để ping một địa chỉ IP trong Python?

    Làm cách nào để ping một địa chỉ IP trong Python ?..
    Nhập hệ điều hành ..
    ip_list = ['8.8.8.8'].
    Đối với IP trong IP_LIST:.
    Trả lời = HĐH.popen [f "ping {ip}"].đọc[].
    Nếu "nhận được = 4" để đáp ứng:.
    in [f "up {ip} ping thành công"].

    Làm cách nào để ping đến một địa chỉ IP?

    Sử dụng ping trên thiết bị Windows trong cửa sổ nhắc lệnh, nhập 'ping' theo sau là đích, địa chỉ IP hoặc tên miền và nhấn Enter.Lệnh sẽ bắt đầu in kết quả của ping vào dấu nhắc lệnh.In the Command Prompt window, type 'ping' followed by the destination, either an IP Address or a Domain Name, and press Enter. The command will begin printing the results of the ping into the Command Prompt.

    Bài Viết Liên Quan

    Chủ Đề