जवाबों:
यह फ़ंक्शन किसी भी ओएस (यूनिक्स, लिनक्स, मैकओएस, और विंडोज)
पायथन 2 और पायथन 3 में काम करता है
EDITS :
By @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
ध्यान दें कि, विंडोज पर @ikrase के अनुसार, यह फ़ंक्शन अभी भी लौटेगा True
यदि आपको कोई Destination Host Unreachable
त्रुटि मिलती है ।
व्याख्या
कमांड ping
विंडोज और यूनिक्स जैसी दोनों प्रणालियों में है।
विकल्प -n
(विंडोज) या -c
(यूनिक्स) पैकेट की संख्या को नियंत्रित करता है जो इस उदाहरण में 1 पर सेट किया गया था।
platform.system()
मंच का नाम देता है। पूर्व। 'Darwin'
macOS पर।
subprocess.call()
एक सिस्टम कॉल करता है। पूर्व। subprocess.call(['ls','-l'])
।
ping 8.8.8.8 -n 1
3) echo %ERRORLEVEL%
। कोड: पायथन कोड की अंतिम पंक्ति को संशोधित करें return system_call(command)
। उचित कनेक्टिविटी के साथ आपको 0 (शून्य) मिलेगा। अपने मॉडेम को बंद करने के साथ, आपको कुछ त्रुटि कोड प्राप्त करना होगा। बेशक, दोनों तरीकों को समान शर्तों के तहत एक ही त्रुटि कोड वापस करना होगा।
यदि आपको विंडोज का समर्थन करने की आवश्यकता नहीं है, तो यह करने के लिए वास्तव में संक्षिप्त तरीका है:
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!'
यह काम करता है क्योंकि यदि कनेक्शन विफल रहता है तो पिंग एक गैर-शून्य मान लौटाता है। (वापसी का मूल्य वास्तव में नेटवर्क त्रुटि के आधार पर भिन्न होता है।) आप '-t' विकल्प का उपयोग करके पिंग टाइमआउट (सेकंड में) भी बदल सकते हैं। ध्यान दें, यह टेक्स्ट को कंसोल में आउटपुट करेगा।
response = os.system("ping -c 1 -w2 " + hostname + " > /dev/null 2>&1")
man ping
सुनिश्चित करने के लिए जाँच करें।
hostname
उपयोगकर्ता से स्ट्रिंग मिलती है , तो वे आपको "url" जैसी सुविधा देकर आसानी से आपके सर्वर को हैक कर सकते हैं 'google.com; rm -rf /*'
। subprocess.run(["ping", "-c", "1", hostname]).returncode
इसके बजाय उपयोग करें ।
वहाँ एक मॉड्यूल है जिसे pyping कहा जाता है जो ऐसा कर सकता है। इसे पाइप के साथ स्थापित किया जा सकता है
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))
os.system('ping -c 1 -t 1 hostname')
समाधान का उपयोग करते हुए 255 सेकंड के बजाय 1 सेकंड में निष्पादित करता है । प्लस pyping
टीसीपी / आईपी सॉकेट लाइब्रेरी का उपयोग करने की तुलना में उपयोग करने के लिए बहुत आसान है। मैंने अपने पिंग प्रोग्राम को दोनों का उपयोग करके लिखा है, और pyping
बहुत तेज़ और उपयोग में आसान है, मेरी राय में, खासकर अगर कोई टीसीपी / आईपी सॉकेट लाइब्रेरी का उपयोग करने से परिचित नहीं है।
pip install ping3
import subprocess
ping_response = subprocess.Popen(["/bin/ping", "-c1", "-w100", "192.168.0.1"], stdout=subprocess.PIPE).stdout.read()
whereis ping
सही रास्ता पाने के लिए उपयोग करें ।
ping_response = subprocess.Popen(["ping", hostname, "-n", '1'], stdout=subprocess.PIPE).stdout.read()
Python3 के लिए एक बहुत ही सरल और सुविधाजनक अजगर मॉड्यूल ping3 है : ( pip install ping3
, रूट विशेषाधिकारों की आवश्यकता है )।
from ping3 import ping, verbose_ping
ping('example.com') # Returns delay in seconds.
>>> 0.215697261510079666
यह मॉड्यूल कुछ मापदंडों के अनुकूलन के लिए भी अनुमति देता है।
चूँकि मुझे संस्करण २. 3. और ३.३ पर अपने पायथन प्रोग्राम को सार्वभौमिक बनाना पसंद है और प्लेटफ़ॉर्म लिनक्स, मैक ओएस और विंडोज पर मुझे मौजूदा उदाहरणों को संशोधित करना पड़ा।
# 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"))
False if platform.system().lower()=="windows" else True
आप के बजाय बेशक सिर्फ इस्तेमाल भी कर सकते हैं platform.system().lower() != "windows"
।
os.name!="nt"
काम भी नहीं करता है ? निश्चित रूप से मैंने इसे सभी ver / platform combos पर आज़माया नहीं है!
def ping(host): process = subprocess.Popen(["ping", "-n", "1",host], stdout=subprocess.PIPE, stderr=subprocess.PIPE) streamdata = process.communicate()[0] if 'unreachable' in str(streamdata): return 1 return process.returncode
unreachable
पाइप में पाया जाता है तो आप 0 नहीं लौटाएँगे , नहीं।
चारों ओर देखने के बाद, मैंने अपना स्वयं का पिंग मॉड्यूल लिखना समाप्त कर दिया, जो बड़ी संख्या में पतों की निगरानी करने के लिए डिज़ाइन किया गया है, अतुल्यकालिक है और बहुत सारे सिस्टम संसाधनों का उपयोग नहीं करता है। आप इसे यहां पा सकते हैं: https://github.com/romana/multi-ping/ यह Apache लाइसेंस प्राप्त है, इसलिए आप इसे अपने प्रोजेक्ट में किसी भी तरह से उपयोग कर सकते हैं जो आपको फिट दिखाई दे।
मेरे खुद को लागू करने के मुख्य कारणों में अन्य दृष्टिकोणों के प्रतिबंध हैं:
#!/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()
Make Sure Pyping स्थापित है या इसे स्थापित करें पाइप स्थापित pyping
#!/usr/bin/python
import pyping
response = pyping.ping('Your IP')
if response.ret_code == 0:
print("reachable")
else:
print("unreachable")
कच्चे आईसीएमपी पैकेट भेजने के लिए आवश्यक विशेषाधिकारों के कारण प्रोग्रामेटिक आईसीएमपी पिंग जटिल है, और 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
आंतरिक रूप से, यह बस लक्ष्य सर्वर के लिए एक टीसीपी कनेक्शन स्थापित करता है और इसे तुरंत छोड़ देता है, समय व्यतीत करते हुए। यह विशेष रूप से कार्यान्वयन थोड़ा सीमित है जिसमें यह बंद बंदरगाहों को संभालता नहीं है लेकिन आपके अपने सर्वर के लिए यह बहुत अच्छा काम करता है।
मैं इसे हल करता हूं:
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
"टीटीएल" यह जानने का तरीका है कि क्या पिंग सही ढंग से है। Saludos
इस पोस्ट में उत्तरों से विचारों का उपयोग करते हुए मेरी कमी लेकिन केवल नए अनुशंसित उपप्रक्रम मॉड्यूल और 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)
True if condition else False
किसी शर्त के आधार पर True या False लौटाने के लिए उपयोग करने की आवश्यकता नहीं है । बस का उपयोग करें जैसे shell_needed = operating_sys == 'Windows'
औरreturn success == 0
यह स्क्रिप्ट विंडोज पर काम करती है, और अन्य ओएस पर काम करना चाहिए: यह विंडोज, डेबियन और मैकोक्स पर काम करता है, सोलारिस पर एक परीक्षण की आवश्यकता है।
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")
pyping
एक मानक मॉड्यूल प्रतीत नहीं होता है। शायद आप एक लिंक प्रदान कर सकते हैं?
मल्टी-पिंग ( 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
को अनदेखा किया जा सकता है!
आशा करता हूँ की ये काम करेगा!
पिंग फ़ंक्शन का मेरा संस्करण:
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 (Mac not tested, should work)
'''
# 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
अपनी इच्छानुसार इसका उपयोग करने के लिए स्वतंत्र महसूस करें।
काफी सरल लगता है, लेकिन मुझे फिट बैठता है। मैं "icmp ओपन सॉकेट ऑपरेशन की अनुमति नहीं देता" रख रहा था अन्यथा सर्वर लाइन बंद होने पर समाधान लटक जाता। यदि, हालांकि, आप जो जानना चाहते हैं वह यह है कि सर्वर जीवित है और आप उस सर्वर पर एक वेब सर्वर चला रहे हैं, तो कर्ल काम करेगा। यदि आपके पास 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
मुझे भी ऐसी ही आवश्यकता थी इसलिए मैंने इसे नीचे दिखाए अनुसार लागू किया। यह विंडोज 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 उपलब्ध नहीं है, तो subprocess.check_output () एक अपवाद उठाता है। आउटपुट लाइन 'पैकेट: भेजा गया = 2, प्राप्त = 2, खोया = 0 (0% हानि)' से जानकारी निकालकर अतिरिक्त सत्यापन किया जा सकता है।
यहां पायथन के subprocess
मॉड्यूल और ping
अंतर्निहित ओएस द्वारा प्रदान किए गए सीएलआई टूल का उपयोग करके समाधान है । विंडोज और लिनक्स पर परीक्षण किया गया। नेटवर्क टाइमआउट सेट करने में सहायता करें। रूट विशेषाधिकार (विंडोज और लिनक्स पर कम से कम) की जरूरत नहीं है।
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
इसका उपयोग अजगर 2.7 पर परीक्षण किया गया है और यह ठीक है अगर सफलता में असफल होने पर मिलीसेकेंड में पिंग समय देता है।
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
एक बात बहुत सारे उत्तर याद आती है कि (कम से कम विंडोज में) बहुत कुछ ping
कमांड 0 देता है (सफलता का संकेत देता है) यदि यह उत्तर प्राप्त करता है "गंतव्य होस्ट अप्राप्य है।"
यहाँ मेरा कोड है जो जांच करता है कि क्या b'TTL='
प्रतिक्रिया में है, क्योंकि यह केवल उस समय मौजूद है जब पिंग मेजबान तक पहुंचा था। नोट: इस कोड के अधिकांश यहाँ अन्य उत्तरों पर आधारित है।
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
लौटने से पहले प्रिंट करना होगा ।
WINDOWS ONLY - विश्वास नहीं हो सकता है कि कोई भी खुली खुली 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"
मुझे एक तेज़ पिंग स्वीप की आवश्यकता थी और मैं किसी भी बाहरी लाइब्रेरी का उपयोग नहीं करना चाहता था, इसलिए मैंने बिल्ट-इन का उपयोग करके संगामिति का उपयोग करने का संकल्प लिया asyncio
।
इस कोड को अजगर 3.7+ की आवश्यकता होती है और इसे लिनक्स पर ही बनाया और परखा जाता है । यह विंडोज पर काम नहीं करेगा लेकिन मुझे यकीन है कि आप इसे विंडोज पर काम करने के लिए आसानी से बदल सकते हैं।
मैं एक विशेषज्ञ के साथ asyncio
नहीं हूँ, लेकिन मैंने इस बेहतरीन लेख का उपयोग स्पीड अप योर पायथन प्रोग्राम विथ कंसीमरी में किया है और मैं कोड की इन पंक्तियों के साथ आया हूं। मैंने इसे जितना संभव हो उतना सरल बनाने की कोशिश की, इसलिए सबसे अधिक संभावना है कि आपको अपनी आवश्यकताओं के अनुरूप इसमें अधिक कोड जोड़ने की आवश्यकता होगी।
यह सही या गलत नहीं है, मुझे लगा कि यह सिर्फ आईपी को प्रिंट करने के लिए अधिक सुविधाजनक होगा जो पिंग अनुरोध का जवाब देता है। मुझे लगता है कि यह बहुत तेज़ है, लगभग 10 सेकंड में 255 ips पिंग करता है ।
#!/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 का उत्तर दिया जाता है, इसलिए वह जो पहले उत्तर देता है वह पहले मुद्रित हो जाता है।
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"
एक साधारण एक मैं बस एक मिनट में पकाया जाता है..इकंप्लिब की जड़ें निजी होने की जरूरत है नीचे काम करता है बहुत अच्छा! HTH