Editing
Scripting Basics
(section)
Jump to navigation
Jump to search
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
==== Bash ==== This script scans a given subnet for live hosts by sending ICMP echo requests (ping) and prints responding IP addresses. <syntaxhighlight lang="bash"> #!/bin/bash # Loop through IP addresses 1 to 254 in a subnet for ip in $(seq 1 254); do # Send 1 ping (-c 1) to each IP in the given subnet (passed as $1) # Example: if $1 = 192.168.1, it pings 192.168.1.1 to 192.168.1.254 ping -c 1 $1.$ip | # Look for lines in the ping output that contain "64 bytes" grep "64 bytes" | # Extract the 4th field (which contains the IP with a colon at the end) cut -d " " -f 4 | # Remove the trailing colon from the IP tr -d ":" & done # Notes: # - $(...) is used instead of backticks \`...\` (modern syntax) # - '&' runs each ping in the background for speed # - This script prints a list of live hosts in the subnet # - Usage: ./script.sh 192.168.1 </syntaxhighlight> This script performs a fast parallel ping sweep across a /24 subnet and logs all responsive IPs to a CSV file. <syntaxhighlight lang="bash"> # Scan a subnet (10.10.10.1β254) and log live hosts to a CSV file # Loop over all IPs in the subnet for ip in 10.10.10.{1..254}; do # Ping each IP once (-c1), wait max 1 second for reply (-W1) # Suppress output (&>/dev/null), and if host responds, log to file ping -c1 -W1 $ip &>/dev/null && echo "$ip,up" >> live.csv & done # Wait for all background pings to finish wait </syntaxhighlight>
Summary:
Please note that all contributions to HackOps may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see
HackOps:Copyrights
for details).
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)
Navigation menu
Personal tools
Not logged in
Talk
Contributions
Create account
Log in
Namespaces
Page
Discussion
English
Views
Read
Edit
Edit source
View history
More
Search
Navigation
Tools
What links here
Related changes
Special pages
Page information