IT이야기

Python에서 서버 ping

cyworld 2022. 4. 3. 21:10
반응형

Python에서 서버 ping

Python에서는 ICMP를 통해 서버를 ping하고 서버가 응답하면 TRUE를 반환하는 방법이 있는가, 응답이 없으면 FALSE를 반환하는 방법이 있는가?

Windows(윈도우)를 지원할 필요가 없는 경우 다음과 같이 간단히 수행할 수 있는 방법을 알아보십시오.

import os
hostname = "google.com" #example
response = os.system("ping -c 1 " + hostname)

#and then check the response...
if response == 0:
  print hostname, 'is up!'
else:
  print hostname, 'is down!'

이는 ping이 연결이 실패하면 0이 아닌 값을 반환하기 때문이다.(실제로 반환 값은 네트워크 오류에 따라 다름)'-t' 옵션을 사용하여 ping 시간 초과(초)를 변경할 수도 있다.콘솔로 텍스트가 출력된다는 점에 유의하십시오.

이 기능은 모든 OS(Unix, Linux, macOS 및 Windows)에서 작동함
파이톤 2와 파이톤 3

편집:
@radato로 os.system로 대체되었다.subprocess.call이렇게 하면 호스트 이름 문자열이 검증되지 않을 수 있는 경우 셸 주입 취약성을 방지할 수 있다.

import platform    # For getting the operating system name
import subprocess  # For executing a shell command

def ping(host):
    """
    Returns True if host (str) responds to a ping request.
    Remember that a host may not respond to a ping (ICMP) request even if the host name is valid.
    """

    # Option for the number of packets as a function of
    param = '-n' if platform.system().lower()=='windows' else '-c'

    # Building the command. Ex: "ping -c 1 google.com"
    command = ['ping', param, '1', host]

    return subprocess.call(command) == 0

이 Windows의 @ikrase를 에 유의하십시오.True만약 당신이 a를 얻게 된다면.Destination Host Unreachable착오

설명

다.pingWindows 및 Unix 유사 시스템 모두에서 사용 가능.
옵션-n (Windows)는-c(Unix)는 이 예에서 1로 설정된 패킷 수를 제어한다.

platform.system() 플랫폼 이름을 반환하십시오.Ex'Darwin'매코스로
subprocess.call() 시스템 호출을 수행한다.Exsubprocess.call(['ls','-l']).

이것을 할 수 있는 pyping이라는 모듈이 있다.pip으로 설치할 수 있다.

pip install pyping

하지만 이 모듈을 사용할 때는 후드 아래에서 원시 패킷을 제작하기 때문에 루트 액세스가 필요하다.

import pyping

r = pyping.ping('google.com')

if r.ret_code == 0:
    print("Success")
else:
    print("Failed with {}".format(r.ret_code))
import subprocess
ping_response = subprocess.Popen(["/bin/ping", "-c1", "-w100", "192.168.0.1"], stdout=subprocess.PIPE).stdout.read()

python3의 경우 매우 간단하고 편리한 python module ping3가 있다. (pip install ping3에는 루트 권한이 필요함).

from ping3 import ping, verbose_ping
ping('example.com')  # Returns delay in seconds.
>>> 0.215697261510079666

이 모듈은 일부 파라미터의 사용자 정의도 허용한다.

버전 2.7과 3.x, 플랫폼 Linux, Mac OS, Windows에서 Python 프로그램을 범용적으로 사용하는 것을 좋아하기 때문에 기존의 예를 수정해야 했다.

# shebang does not work over all platforms
# ping.py  2016-02-25 Rudolf
# subprocess.call() is preferred to os.system()
# works under Python 2.7 and 3.4
# works under Linux, Mac OS, Windows

def ping(host):
    """
    Returns True if host responds to a ping request
    """
    import subprocess, platform

    # Ping parameters as function of OS
    ping_str = "-n 1" if  platform.system().lower()=="windows" else "-c 1"
    args = "ping " + " " + ping_str + " " + host
    need_sh = False if  platform.system().lower()=="windows" else True

    # Ping
    return subprocess.call(args, shell=need_sh) == 0

# test call
print(ping("192.168.17.142"))

은 원시 시시 ICMP를 원을하(하하下)라고 부른다.ping2진법은 못생겼다.서버 모니터링의 경우 TCP ping이라는 기술을 사용하여 동일한 결과를 얻을 수 있다.

# pip3 install tcping
>>> from tcping import Ping
# Ping(host, port, timeout)
>>> ping = Ping('212.69.63.54', 22, 60)
>>> ping.ping(3)
Connected to 212.69.63.54[:22]: seq=1 time=23.71 ms
Connected to 212.69.63.54[:22]: seq=2 time=24.38 ms
Connected to 212.69.63.54[:22]: seq=3 time=24.00 ms

내부적으로 이것은 단순히 대상 서버에 대한 TCP 연결을 설정하고 즉시 삭제하여 경과된 시간을 측정한다.이 특별한 구현은 폐쇄된 포트를 다루지 않는다는 점에서 다소 제한적이지만, 당신 자신의 서버의 경우 그것은 꽤 잘 작동한다.

#!/usr/bin/python3

import subprocess as sp

def ipcheck():
    status,result = sp.getstatusoutput("ping -c1 -w2 " + str(pop))
    if status == 0:
        print("System " + str(pop) + " is UP !")
    else:
        print("System " + str(pop) + " is DOWN !")


pop = input("Enter the ip address: ")
ipcheck()

주위를 둘러본 후, 나는 결국 많은 수의 주소를 감시하도록 고안된 나만의 ping 모듈을 작성하게 되었는데, 이 ping 모듈은 비동기적이며 많은 시스템 자원을 많이 사용하지 않는다.여기서 찾을 수 있다: https://github.com/romana/multi-ping/ 이 아파치는 라이센스를 받았기 때문에 프로젝트에서 어떤 방식으로든 적합하다고 생각되는 방식으로 사용할 수 있다.

나만의 방식을 구현하는 주요 이유는 다른 접근법의 제한 때문이다.

  • 여기에 언급된 많은 솔루션들은 명령줄 유틸리티로 실행되어야 한다.만약 당신이 많은 수의 IP 주소를 감시해야 한다면 이것은 상당히 비효율적이고 자원이 부족하다.
  • 다른 사람들은 몇몇 오래된 파이톤 핑 모듈을 언급한다.나는 그것들을 살펴봤고 결국 그들 모두는 어떤 문제나 다른 문제(패킷 ID를 올바르게 설정하지 않는 것 등)를 가지고 있었고 많은 수의 주소의 핑잉을 처리하지 않았다.

내 버전의 ping 함수:

  • Python 3.5 이상, Windows 및 Linux에서 작동.
  • 윈도우즈에서 ping 명령이 "Destination Host Unable"으로 실패하면 False를 반환한다.
  • 그리고 팝업 창이나 명령줄에 출력을 표시하지 않는다.
import platform, subprocess

def ping(host_or_ip, packets=1, timeout=1000):
    ''' Calls system "ping" command, returns True if ping succeeds.
    Required parameter: host_or_ip (str, address of host to ping)
    Optional parameters: packets (int, number of retries), timeout (int, ms to wait for response)
    Does not show any output, either as popup window or in command line.
    Python 3.5+, Windows and Linux compatible
    '''
    # The ping command is the same for Windows and Linux, except for the "number of packets" flag.
    if platform.system().lower() == 'windows':
        command = ['ping', '-n', str(packets), '-w', str(timeout), host_or_ip]
        # run parameters: capture output, discard error messages, do not show window
        result = subprocess.run(command, stdin=subprocess.DEVNULL, stdout=subprocess.PIPE, stderr=subprocess.DEVNULL, creationflags=0x08000000)
        # 0x0800000 is a windows-only Popen flag to specify that a new process will not create a window.
        # On Python 3.7+, you can use a subprocess constant:
        #   result = subprocess.run(command, capture_output=True, creationflags=subprocess.CREATE_NO_WINDOW)
        # On windows 7+, ping returns 0 (ok) when host is not reachable; to be sure host is responding,
        # we search the text "TTL=" on the command output. If it's there, the ping really had a response.
        return result.returncode == 0 and b'TTL=' in result.stdout
    else:
        command = ['ping', '-c', str(packets), '-w', str(timeout), host_or_ip]
        # run parameters: discard output and error messages
        result = subprocess.run(command, stdin=subprocess.DEVNULL, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
        return result.returncode == 0

마음대로 쓰세요.

pyping이 설치되어 있는지 확인하거나 pip install pyping을 설치하십시오.

#!/usr/bin/python
import pyping

response = pyping.ping('Your IP')

if response.ret_code == 0:
    print("reachable")
else:
    print("unreachable")

python3에서 소켓 패키지 사용:

import socket

def ping_server(server: str, port: int, timeout=3):
    """ping server"""
    try:
        socket.setdefaulttimeout(timeout)
        s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        s.connect((server, port))
    except OSError as error:
        return False
    else:
        s.close()
        return True
#!/usr/bin/python3

import subprocess as sp

ip = "192.168.122.60"
status,result = sp.getstatusoutput("ping -c1 -w2 " + ip)

if status == 0: 
    print("System " + ip + " is UP !")
else:
    print("System " + ip + " is DOWN !")

다음을 통해 이 문제를 해결하십시오.

def ping(self, host):
    res = False

    ping_param = "-n 1" if system_name().lower() == "windows" else "-c 1"

    resultado = os.popen("ping " + ping_param + " " + host).read()

    if "TTL=" in resultado:
        res = True
    return res

"TTL"은 핑이 정확한지 확인하는 방법이다.살루도스

이 게시물의 답변에서 아이디어를 얻었지만 새로운 권장 하위 프로세스 모듈과 python3만 사용했을 때 절감되는 혜택:

import subprocess
import platform

operating_sys = platform.system()
nas = '192.168.0.10'

def ping(ip):
    # ping_command = ['ping', ip, '-n', '1'] instead of ping_command = ['ping', ip, '-n 1'] for Windows
    ping_command = ['ping', ip, '-n', '1'] if operating_sys == 'Windows' else ['ping', ip, '-c 1']
    shell_needed = True if operating_sys == 'Windows' else False

    ping_output = subprocess.run(ping_command,shell=shell_needed,stdout=subprocess.PIPE)
    success = ping_output.returncode
    return True if success == 0 else False

out = ping(nas)
print(out)

이 스크립트는 Windows에서 작동하며 다른 OS에서작동해야 한다:Windows, 데비안 및 Macosx에서 작동하며 Solaris에 대한테스트가 필요하다.

import os
import platform


def isUp(hostname):

    giveFeedback = False

    if platform.system() == "Windows":
        response = os.system("ping "+hostname+" -n 1")
    else:
        response = os.system("ping -c 1 " + hostname)

    isUpBool = False
    if response == 0:
        if giveFeedback:
            print hostname, 'is up!'
        isUpBool = True
    else:
        if giveFeedback:
            print hostname, 'is down!'

    return isUpBool

print(isUp("example.com")) #Example domain
print(isUp("localhost")) #Your computer
print(isUp("invalid.example.com")) #Unresolvable hostname: https://tools.ietf.org/html/rfc6761
print(isUp("192.168.1.1")) #Pings local router
print(isUp("192.168.1.135")) #Pings a local computer - will differ for your network

나는 결국 비슷한 시나리오에 관한 이 질문을 발견하게 되었다.나는 파이핑을 시도해 보았지만, 파이썬 2.7 아래의 윈도우에서 나벤이 준 예는 나에게 맞지 않았다.

나에게 효과가 있었던 예는 다음과 같다.

import pyping

response = pyping.send('Your IP')

if response['ret_code'] == 0:
    print("reachable")
else:
    print("unreachable")

Multi-ping)pip install multiPing) 나는 이 간단한 코드를 만들었다(간단히 복사해서 붙여넣을 것이다!:

from multiping import MultiPing

def ping(host,n = 0):
    if(n>0):
        avg = 0
        for i in range (n):
            avg += ping(host)
        avg = avg/n
    # Create a MultiPing object to test hosts / addresses
    mp = MultiPing([host])

    # Send the pings to those addresses
    mp.send()

    # With a 1 second timout, wait for responses (may return sooner if all
    # results are received).
    responses, no_responses = mp.receive(1)


    for addr, rtt in responses.items():
        RTT = rtt


    if no_responses:
        # Sending pings once more, but just to those addresses that have not
        # responded, yet.
        mp.send()
        responses, no_responses = mp.receive(1)
        RTT = -1

    return RTT

사용량:

#Getting the latency average (in seconds) of host '192.168.0.123' using 10 samples
ping('192.168.0.123',10)

단일 샘플을 원하는 경우 두 번째 파라미터 "10"는 무시할 수 있다!

도움이 되길 바래!

더 빠른 ping 스위프가 필요했고 외부 라이브러리를 사용하고 싶지 않았기 때문에 내장된 기능을 사용하여 동시성을 사용하기로 결정했다.asyncio.

이 코드는 python 3.7+가 필요하며 Linux에서만 만들어 테스트된다.윈도에서는 안 되겠지만 윈도에서는 쉽게 바꿀 수 있을 거야.

난 전문가가 아니다.asyncio하지만 나는 이 훌륭한 기사를 사용했고, "Speed Up Your Python Program with Concurrency"라는 코드를 생각해냈다.최대한 간단하게 만들려고 노력했으니까, 아마 니 필요에 맞게 코드를 더 추가해야 할 거야.

사실인지 거짓인지 되돌아오는 것이 아니라 핑 요청에 응답하는 IP를 인쇄하게 하는 것만으로도 더 편리할 것이라고 생각했다.나는 255개ip를 거의 10초만에 ping하는 것이 꽤 빠르다고 생각한다.

#!/usr/bin/python3

import asyncio

async def ping(host):
    """
    Prints the hosts that respond to ping request
    """
    ping_process = await asyncio.create_subprocess_shell("ping -c 1 " + host + " > /dev/null 2>&1")
    await ping_process.wait()

    if ping_process.returncode == 0:
        print(host)
    return 


async def ping_all():
    tasks = []

    for i in range(1,255):
        ip = "192.168.1.{}".format(i)
        task = asyncio.ensure_future(ping(ip))
        tasks.append(task)

    await asyncio.gather(*tasks, return_exceptions = True)

asyncio.run(ping_all())

샘플 출력:

192.168.1.1
192.168.1.3
192.168.1.102
192.168.1.106
192.168.1.6

IP는 응답하는 즉시 인쇄되기 때문에 먼저 응답하는 IP가 먼저 인쇄되기 때문에 IP는 순서가 맞지 않는다는 점에 유의한다.

간단해 보이긴 하지만 나한테는 잘 맞았어.나는 계속 "icmp open socket operation not permission"을 받았다. 그렇지 않으면 서버가 오프라인 상태일 때 해결책이 중단될 것이다.그러나 서버가 활성 상태이고 해당 서버에서 웹 서버를 실행 중인 경우, curl로 작업을 수행할 수 있다.ssh와 인증서가 있으면 ssh와 간단한 명령으로 충분하다.암호는 다음과 같다.

from easyprocess import EasyProcess # as root: pip install EasyProcess
def ping(ip):
    ping="ssh %s date;exit"%(ip) # test ssh alive or
    ping="curl -IL %s"%(ip)      # test if http alive
    response=len(EasyProcess(ping).call(timeout=2).stdout)
    return response #integer 0 if no response in 2 seconds

python 2.7에서 테스트한 이 기능을 사용하여 ping 시간을 밀리초 단위로 반환하고 실패 시 False를 반환하십시오.

import platform,subproccess,re
def Ping(hostname,timeout):
    if platform.system() == "Windows":
        command="ping "+hostname+" -n 1 -w "+str(timeout*1000)
    else:
        command="ping -i "+str(timeout)+" -c 1 " + hostname
    proccess = subprocess.Popen(command, stdout=subprocess.PIPE)
    matches=re.match('.*time=([0-9]+)ms.*', proccess.stdout.read(),re.DOTALL)
    if matches:
        return matches.group(1)
    else: 
        return False

나는 비슷한 요구사항이 있어서 아래와 같이 실행했다.윈도 64비트와 리눅스에서 테스트한다.

import subprocess
def systemCommand(Command):
    Output = ""
    Error = ""     
    try:
        Output = subprocess.check_output(Command,stderr = subprocess.STDOUT,shell='True')
    except subprocess.CalledProcessError as e:
        #Invalid command raises this exception
        Error =  e.output 

    if Output:
        Stdout = Output.split("\n")
    else:
        Stdout = []
    if Error:
        Stderr = Error.split("\n")
    else:
        Stderr = []

    return (Stdout,Stderr)

#in main
Host = "ip to ping"
NoOfPackets = 2
Timeout = 5000 #in milliseconds
#Command for windows
Command = 'ping -n {0} -w {1} {2}'.format(NoOfPackets,Timeout,Host)
#Command for linux 
#Command = 'ping -c {0} -w {1} {2}'.format(NoOfPackets,Timeout,Host)
Stdout,Stderr = systemCommand(Command)
if Stdout:
   print("Host [{}] is reachable.".format(Host))
else:
   print("Host [{}] is unreachable.".format(Host))

IP에 연결할 수 없는 경우 하위 프로세스.check_output()는 예외를 제기한다.출력 라인 '패킷:보낸 사람 = 2, 받은 사람 = 2, 잃은 사람 = 0 (0% 손실)'이다.

Python's를 사용한 솔루션subprocess모듈 및 기타ping기본 OS에서 제공하는 CLI 도구.Windows 및 Linux에서 테스트됨.네트워크 시간 초과 설정 지원.루트 권한(최소한 윈도우즈 및 리눅스)이 필요하지 않음

import platform
import subprocess

def ping(host, network_timeout=3):
    """Send a ping packet to the specified host, using the system "ping" command."""
    args = [
        'ping'
    ]

    platform_os = platform.system().lower()

    if platform_os == 'windows':
        args.extend(['-n', '1'])
        args.extend(['-w', str(network_timeout * 1000)])
    elif platform_os in ('linux', 'darwin'):
        args.extend(['-c', '1'])
        args.extend(['-W', str(network_timeout)])
    else:
        raise NotImplemented('Unsupported OS: {}'.format(platform_os))

    args.append(host)

    try:
        if platform_os == 'windows':
            output = subprocess.run(args, check=True, universal_newlines=True).stdout

            if output and 'TTL' not in output:
                return False
        else:
            subprocess.run(args, check=True)

        return True
    except (subprocess.CalledProcessError, subprocess.TimeoutExpired):
        return False

WIN32_PingStatus 균열된 원본이 없다는 것을 믿을 수 없음 간단한 WMI 쿼리를 사용하여 정말 상세한 정보가 가득한 개체를 무료로 반환함

import wmi


# new WMI object
c = wmi.WMI()

# here is where the ping actually is triggered
x = c.Win32_PingStatus(Address='google.com')

# how big is this thing? - 1 element
print 'length x: ' ,len(x)


#lets look at the object 'WMI Object:\n'
print x


#print out the whole returned object
# only x[0] element has values in it
print '\nPrint Whole Object - can directly reference the field names:\n'
for i in x:
    print i



#just a single field in the object - Method 1
print 'Method 1 ( i is actually x[0] ) :'
for i in x:
    print 'Response:\t', i.ResponseTime, 'ms'
    print 'TTL:\t', i.TimeToLive


#or better yet directly access the field you want
print '\npinged ', x[0].ProtocolAddress, ' and got reply in ', x[0].ResponseTime, 'ms'

견본 생산량

나는 다른 대답에서 차용한다.쿼리를 단순화하고 최소화해 보십시오.

import platform, os

def ping(host):
    result = os.popen(' '.join(("ping", ping.param, host))).read()
    return 'TTL=' in result

ping.param = "-n 1" if platform.system().lower() == "windows" else "-c 1"

Linux에서는 루트(또는 setuid 또는 setuid) 없이 ICMP 데이터그램(원시 아님) 소켓 생성 가능CAP_NET_RAWhttps://unix.stackexchange.com/a/592914.결국 하게 되었다.

$ id
uid=1000(raylu) gid=1000(raylu) [...]
$ sudo sysctl net.ipv4.ping_group_range='1000 1000'
import socket
import struct
import time

def main():
    ping('192.168.1.10')

def ping(destination):
    sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.getprotobyname('icmp'))
    sock.settimeout(10.0)
    start_time = time.time_ns() # python 3.7+ only

    payload = struct.pack('L', start_time)
    sock.sendto(encode(payload), (destination, 0))
    while (time.time_ns() - start_time) // 1_000_000_000 < 10:
        try:
            data, source = sock.recvfrom(256)
        except socket.timeout:
            print('timed out')
            return
        message_type, message_code, check, identifier, sequence_number = struct.unpack('bbHHh', data[:8])
        if source == (destination, 0) and message_type == ICMP.ECHO_REPLY and data[8:] == payload:
            print((time.time_ns() - start_time) // 1_000_000, 'ms')
            break
        else:
            print('got unexpected packet from %s:' % source[0], message_type, data[8:])
    else:
        print('timed out')

def encode(payload: bytes):
    # calculate checksum with check set to 0
    checksum = calc_checksum(icmp_header(ICMP.ECHO_REQUEST, 0, 0, 1, 1) + payload)
    # craft the packet again with the checksum set
    return icmp_header(ICMP.ECHO_REQUEST, 0, checksum, 1, 1) + payload

def icmp_header(message_type, message_code, check, identifier, sequence_number) -> bytes:
    return struct.pack('bbHHh', message_type, message_code, check, identifier, sequence_number)

def calc_checksum(data: bytes) -> int:
    '''RFC 1071'''
    # code stolen from https://github.com/alessandromaggio/pythonping/blob/a59ce65a/pythonping/icmp.py#L8
    '''
    MIT License

    Copyright (c) 2018 Alessandro Maggio

    Permission is hereby granted, free of charge, to any person obtaining a copy
    of this software and associated documentation files (the "Software"), to deal
    in the Software without restriction, including without limitation the rights
    to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
    copies of the Software, and to permit persons to whom the Software is
    furnished to do so, subject to the following conditions:

    The above copyright notice and this permission notice shall be included in all
    copies or substantial portions of the Software.

    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
    SOFTWARE.
    '''
    subtotal = 0
    for i in range(0, len(data)-1, 2):
        subtotal += (data[i] << 8) + data[i+1]
    if len(data) % 2:
        subtotal += (data[len(data)-1] << 8)
    while subtotal >> 16:
        subtotal = (subtotal & 0xFFFF) + (subtotal >> 16)
    check = ~subtotal
    return ((check << 8) & 0xFF00) | ((check >> 8) & 0x00FF)

class ICMP:
    ECHO_REPLY = 0
    ECHO_REQUEST = 8

비록 다른 답변들이 여기서 제시한 많은 패키지도 효과가 있을 것이다.

많은 답변이 빗나가고 있는 한 가지는 (적어도 Windows에서는)ping명령어는 "대상 호스트에 연결할 수 없음"이라는 응답을 수신하면 0(성공 표시)을 반환한다.

여기 내 코드가 있어b'TTL='pinge.pinge osts에 에 그 참고: 이 코드의 대부분은 여기의 다른 답변에 기초한다.

import platform
import subprocess

def ping(ipAddr, timeout=100):
    '''
    Send a ping packet to the specified host, using the system ping command.
    Accepts ipAddr as string for the ping destination.
    Accepts timeout in ms for the ping timeout.
    Returns True if ping succeeds otherwise Returns False.
        Ping succeeds if it returns 0 and the output includes b'TTL='
    '''
    if platform.system().lower() == 'windows':
        numFlag = '-n'
    else:
        numFlag = '-c'
    completedPing = subprocess.run(['ping', numFlag, '1', '-w', str(timeout), ipAddr],
                                   stdout=subprocess.PIPE,    # Capture standard out
                                   stderr=subprocess.STDOUT)  # Capture standard error
    # print(completedPing.stdout)
    return (completedPing.returncode == 0) and (b'TTL=' in completedPing.stdout)

print(ping('google.com'))

참고: 출력물을 인쇄하는 대신 캡처하므로 출력물을 보려면ping, 인쇄를 해야 할 것이다.completedPing.stdout돌아오기 전에

창이나 Linux에서 모두 ping하고 정렬된 목록을 반환하십시오.이것은 반응 @Ahmed Essam과 @Arno에서 나온 하이브리드/픽스다.

import asyncio
import re

import platform
isWindows = platform.system()


async def ping(host):
    cmd = 'ping {} {} 1'.format(host, '-n' if isWindows else '-c')
    ping_proc = \
        await asyncio.create_subprocess_shell(cmd, stdout=asyncio.subprocess.PIPE,
                                                      stderr=asyncio.subprocess.PIPE)
    stdout, stderr = await ping_proc.communicate()
    outstr = stdout.decode()

    if ping_proc.returncode == 0:
        delay = int(re.search(r'(?:time=)([\d]*)', outstr).group(1)) if 'time=' in outstr else -1
        if delay >= 0:
            # print('{} {}ms'.format(host, delay))
            return [host, delay]

    return [host, None]


async def ping_all():
    tasks = []

    for i in range(1, 256):
        ip = "192.168.1.{}".format(i)
        task = asyncio.ensure_future(ping(ip))
        tasks.append(task)

    retList = await asyncio.gather(*tasks, return_exceptions=True)
    retList = [x for x in retList if x[1] is not None]
    retList.sort(key=lambda x: int(x[0].split('.')[-1]))

    return retList


loop = asyncio.ProactorEventLoop()
asyncio.set_event_loop(loop)
pingRet = loop.run_until_complete(ping_all())

for ip, d in pingRet:
    print('{:<16s} {}ms'.format(ip, d))
  1 #!/usr/bin/python
  2
  3 import os
  4 import sys
  5 import time
  6
  7 os.system("clear")
  8 home_network = "172.16.23."
  9 mine = []
 10
 11 for i in range(1, 256):
 12         z =  home_network + str(i)
 13         result = os.system("ping -c 1 "+ str(z))
 14         os.system("clear")
 15         if result == 0:
 16                 mine.append(z)
 17
 18 for j in mine:
 19         print "host ", j ," is up"

금방 익힌 간단한 거..icmplib를 사용하는 것은 아래 루트 권한들을 필요로 한다. 꽤 효과가 있다!HTH

참조URL: https://stackoverflow.com/questions/2953462/pinging-servers-in-python

반응형