البرمجة

كيفية فحص الشبكة باستخدام Python

The code you provided has an issue with indentation. In Python, indentation is crucial for defining the structure of your code, such as loops and conditional statements. The error message indicates that there is an indentation problem near the line where you define the test variable:

python
test = "ping -n 1 -w 500 %s.%d" % (y,i)

To fix this error, ensure that the line above test = ... and any other lines inside the for loop are indented properly. Here’s the corrected version of your code:

python
from os import popen from string import split, join from re import match import subprocess import os # execute the code and pipe the result to a string rtr_table = [elem.strip().split() for elem in popen("route print").read().split("Metric\n")[1].split("\n") if match("^[0-9]", elem.strip())] #print "Active default gateway:", rtr_table[0][2] x=rtr_table[0][2] #x has a.a.a.a y = x[:x.rfind(".")] # y has a.a.a # in order to test in range of 1 to 255 for the default gateway for i in range(1,255): #ping by sending 1 packet and wait for 500 milli seconds test = "ping -n 1 -w 500 %s.%d" % (y,i) process = subprocess.Popen(test, shell=True, stdout=subprocess.PIPE) # give it time to respond process.wait() # optional check (0 --> success) #print process.returncode # read the result to a string result_str = process.stdout.read() # test it ... # print result_str #from arp we get mac address and corresponding ip address and state as we use '-a' lines=os.popen('arp -a') for line in lines: print line

In this corrected version, the test = ... line and the subsequent lines inside the for loop are properly indented to be inside the loop. This should resolve the IndentationError you were experiencing.

المزيد من المعلومات

بالتأكيد! يبدو أن الكود الذي قدمته يقوم بفحص الشبكة المحلية باستخدام أدوات النظام والشبكة في نظام التشغيل. إليك شرح لبعض الجزئيات في الكود:

  1. popen("route print"): يقوم بتنفيذ أمر route print في نظام التشغيل وقراءة النتائج من الإخراج القياسي.

  2. rtr_table: يقوم بتحليل النتائج المطبوعة من route print للحصول على جدول التوجيه (Routing Table)، ويخزنها في قائمة.

  3. for i in range(1,255):: يقوم بتكرار عملية فحص الشبكة لعناوين IP في نطاق 1 إلى 254، باستخدام أمر ping لاختبار ما إذا كانت العناوين متاحة.

  4. subprocess.Popen(test, shell=True, stdout=subprocess.PIPE): يقوم بتنفيذ أمر ping في عملية فرعية ويقرأ الإخراج الناتج.

  5. os.popen('arp -a'): يقوم بتنفيذ أمر arp -a لعرض جدول ARP ويقرأ النتائج.

  6. print line: يطبع كل سطر من نتائج جدول ARP.

يركز الكود على فحص الشبكة المحلية واسترداد معلومات حول العناوين IP وجدول التوجيه وجدول ARP.

مقالات ذات صلة

زر الذهاب إلى الأعلى
إغلاق

أنت تستخدم إضافة Adblock

يرجى تعطيل مانع الإعلانات حيث أن موقعنا غير مزعج ولا بأس من عرض الأعلانات لك فهي تعتبر كمصدر دخل لنا و دعم مقدم منك لنا لنستمر في تقديم المحتوى المناسب و المفيد لك فلا تبخل بدعمنا عزيزي الزائر