-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathpyping.py
More file actions
executable file
·162 lines (142 loc) · 5.52 KB
/
pyping.py
File metadata and controls
executable file
·162 lines (142 loc) · 5.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#!/usr/bin/env python
########################################################################
# pyping.py: Ping a Range of IP Addresses in a Subnet
#
# Description:
# This script pings a range of IP addresses within a specified subnet.
# It can display results either immediately (faster) or in ascending
# order of IP addresses for better readability.
#
# Author: id774 (More info: http://id774.net)
# Source Code: https://github.com/id774/scripts
# License: The GPL version 3, or LGPL version 3 (Dual License).
# Contact: idnanashi@gmail.com
#
# Usage:
# pyping.py [-h] [-o] subnet start_ip end_ip
#
# Ping a range of IP addresses in a subnet.
#
# positional arguments:
# subnet Subnet part of the IP. Example: "192.168.11."
# start_ip Start of the IP range. Example: 1
# end_ip End of the IP range. Example: 32
#
# options:
# -h, --help show this help message and exit
# -o, --ordered Display results in ascending order of IP addresses.
#
# Requirements:
# - Python Version: 3.2 or later
#
# Notes:
# - Ensure you have permissions to send pings to the target IPs.
# - This script may take time to complete based on the range specified.
#
# Version History:
# v1.4 2025-07-01
# Standardized termination behavior for consistent script execution.
# v1.3 2025-06-23
# Unified usage output to display full script header and support common help/version options.
# v1.2 2025-01-06
# Added `--ordered` option with shorthand `-o` for sorted output.
# v1.1 2024-01-15
# Suppressed standard error output from ping command.
# Improved command line argument interface using argparse.
# v1.0 2024-01-12
# Initial release. Python version of the rubyping.rb script.
#
########################################################################
import argparse
import os
import subprocess
import sys
import threading
def usage():
""" Display the script header as usage information and exit. """
script_path = os.path.abspath(__file__)
in_header = False
try:
with open(script_path, 'r', encoding='utf-8') as f:
for line in f:
if line.strip().startswith('#' * 10):
if not in_header:
in_header = True
continue
else:
break
if in_header and line.startswith('#'):
if line.startswith('# '):
print(line[2:], end='')
else:
print(line[1:], end='')
except Exception as e:
print("Error reading usage information: %s" % str(e), file=sys.stderr)
sys.exit(1)
sys.exit(0)
def ping(ip, results):
"""
Send a ping request to the specified IP address.
Args:
ip (str): The target IP address to ping.
results (dict): A shared dictionary to store ping results.
"""
try:
with open(os.devnull, 'w') as DEVNULL:
# Execute the ping command and suppress standard error output
subprocess.check_output(
["ping", "-c", "1", "-i", "1", ip], stderr=DEVNULL)
results[ip] = "alive"
except subprocess.CalledProcessError:
results[ip] = "-----"
def main(subnet, start_ip, end_ip, ordered):
"""
Ping a range of IP addresses in the given subnet.
Args:
subnet (str): Subnet prefix, e.g., "192.168.1."
start_ip (int): Start of the IP address range.
end_ip (int): End of the IP address range.
ordered (bool): Whether to sort the results before displaying.
"""
threads = [] # List to store threading.Thread objects
results = {} # Dictionary to store ping results
# Create and start threads for each IP in the range
for n in range(start_ip, end_ip + 1):
ip = subnet + str(n)
thread = threading.Thread(target=ping, args=(ip, results))
threads.append(thread)
thread.start()
# Wait for all threads to complete
for thread in threads:
thread.join()
# Display results based on the `ordered` flag
if ordered:
# Sort IP addresses in ascending order before displaying
for ip in sorted(results.keys(), key=lambda x: tuple(map(int, x.split('.')))):
print("{} --> {}".format(ip, results[ip]))
else:
# Display results as they are collected
for ip, status in results.items():
print("{} --> {}".format(ip, status))
return 0
if __name__ == "__main__":
if len(sys.argv) < 3 or sys.argv[1] in ('-h', '--help', '-v', '--version'):
usage()
if sys.version_info < (3, 2):
print("[ERROR] This script requires Python 3.2 or later.", file=sys.stderr)
sys.exit(9)
# Setup argparse for command-line arguments
parser = argparse.ArgumentParser(
description='Ping a range of IP addresses in a subnet.')
parser.add_argument('subnet', type=str,
help='Subnet part of the IP. Example: "192.168.11."')
parser.add_argument('start_ip', type=int,
help='Start of the IP range. Example: 1')
parser.add_argument('end_ip', type=int,
help='End of the IP range. Example: 32')
parser.add_argument('-o', '--ordered', action='store_true',
help='Display results in ascending order of IP addresses.')
# Parse command-line arguments
args = parser.parse_args()
# Call the main function with parsed arguments
sys.exit(main(args.subnet, args.start_ip, args.end_ip, args.ordered))