107 lines
5.6 KiB
HTML
107 lines
5.6 KiB
HTML
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
|
|
<html xmlns="http://www.w3.org/1999/xhtml"><head>
|
|
<meta http-equiv="Content-type" content="text/html;charset=UTF-8" />
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
|
<link rel="stylesheet" href="main.css" type="text/css" />
|
|
<link rel="stylesheet" href="blog.css" type="text/css" />
|
|
<link rel="alternate" type="application/rss+xml" title="Subscribe to this page..." href="feed.rss" />
|
|
<title>Ip addresses on your subnet with xargs</title>
|
|
</head><body>
|
|
<div id="divbodyholder">
|
|
<div class="headerholder"><div class="header">
|
|
<div id="title">
|
|
<h1 class="nomargin"><a class="ablack" href="http://zigford.org/index.html">zigford.org</a></h1>
|
|
<div id="description"><a href="about.html">About</a><a href="links.html"> | Links</a><a href="scripts.html"> | Scripts</a><br>Sharing linux/windows scripts and tips</br></div>
|
|
</div></div></div>
|
|
<div id="divbody"><div class="content">
|
|
<!-- entry begin -->
|
|
<h3><a class="ablack" href="ip-addresses-on-your-subnet-with-xargs.html">
|
|
Ip addresses on your subnet with xargs
|
|
</a></h3>
|
|
<!-- bashblog_timestamp: #201809012300.09# -->
|
|
<div class="subtitle">September 01, 2018 —
|
|
Jesse Harris
|
|
</div>
|
|
<!-- text begin -->
|
|
<p><a href="tag_xargs.html">xargs</a> strikes again. This time I'm going to use it's parallel
|
|
feature to ping 255 machines very quickly. xargs has a parem <code>-Px</code> where x is
|
|
the number of parallel processes to spawn of the subsequent process. </p>
|
|
<hr />
|
|
<h2>Update - 11/04/2019 Added MacOS Xargs syntax</h2>
|
|
<p>Together with the bash <code>{1..255}</code> expression that expands to output 1 to 255, we
|
|
can do something like this</p>
|
|
<pre><code> $ s=192.168.11.
|
|
$ echo ${s}{1..254} | xargs -P254 -d' ' -L1 ping -c1 -w1 | grep 'bytes from'
|
|
</code></pre>
|
|
<p>In this example, <code>-P254</code> tells xargs to spawn 254 procceses, <code>-d' '</code> says to
|
|
split commands with a space instead of the normal newline, and <code>-L1</code> says to
|
|
finish building the command after <code>1</code> amount of input.</p>
|
|
<p>To achieve the same result in powershell is not quite as simple. Out of the box
|
|
you could use a <code>ForeEach-Object</code> loop like so:</p>
|
|
<pre><code> PS> $s='192.168.11.'
|
|
PS> echo $s[1..254] | %{ Test-Connection $_ -Count 1 -TTL 1 -EA SilentlyContinue }
|
|
</code></pre>
|
|
<p>But it's just a sequential loop. Instead we are going to have to leverage
|
|
the .Net <a href="docs.microsoft.com/dotnet/api/system.net.networkinformation.ping">ping</a>
|
|
class.</p>
|
|
<p>Lee Holmes <a href="https://twitter.com/Lee_Holmes/status/646890380995067904">shared</a>
|
|
this on twitter back in 2015:</p>
|
|
<blockquote>
|
|
<p>Sweep a /16 in 30s: </p>
|
|
<pre><code> $t=$ips|%{
|
|
(New-Object Net.NetworkInformation.Ping).SendPingAsync($_,250)};
|
|
[Threading.Tasks.Task]::WaitAll($t);
|
|
$t.Result
|
|
</code></pre>
|
|
</blockquote>
|
|
<p>Before you run this, build out an array of ips, like so:</p>
|
|
<pre><code>PS> $ips=[1..254] | %{"192.168.11.$_"}
|
|
</code></pre>
|
|
<p>This use of powershell seems a bit too obtuse for me, and not something likely
|
|
to stick in my head. </p>
|
|
<p>I did find other ways to do it in powershell. A collegue of mine Darryl,
|
|
<a href="https://github.com/dardie">dardie</a> on github added <a href="https://github.com/zigford/USC-SCCM/commit/d9813d5626cbb52201541a574a0adf51e99466d8#diff-73c8dfd0435ecc73e3fc408a851841c2">this</a>
|
|
commit to a module I maintain which has pretty good results. Again however not
|
|
something you could commit to memory as you need to write a function to use it.</p>
|
|
<h2>Final thoughts</h2>
|
|
<p>In this case it looks to me like bash wins, if not in performance, but by being
|
|
simpler to implement and commit to memory, or recreate from memory. But if I
|
|
ever need to do this in powershell again, I can just come back to my site for
|
|
reference.</p>
|
|
<p>If you have any comments or feedback, please <a href="mailto:jesse@zigford.org">email</a> me
|
|
and let me know if you will allow your feedback to be posted here.</p>
|
|
<h2>MacOS</h2>
|
|
<p>For completeness and because I tried to run this and failed on MacOS today I'm
|
|
adding it's syntax:</p>
|
|
<p>Xargs on MacOS is a BSD variant, seemlingly pulled from FreeBSD going by the
|
|
man page. It still supports the <code>-Px</code>, however <code>-d</code> is not required as it
|
|
splits on newline and spaces by default. The other difference is that instead
|
|
of <code>-L1</code> we use <code>-n1</code> with essentially the same effect.</p>
|
|
<p>Also of note is the differences with ping. Rather than <code>-w1</code> to reduce time
|
|
waiting for a ping reply, <code>-t1</code> is used.</p>
|
|
<pre><code> echo "192.168.11."{1..254} | xargs -P254 -n1 ping -c1 -t1 |
|
|
awk '/bytes from/ {
|
|
match($0,"[0-9]+.[0-9]+.[0-9]+.[0-9]+");
|
|
n=substr($0,RSTART,RLENGTH);
|
|
print n
|
|
}'
|
|
</code></pre>
|
|
<p><em>In this example, I'm using an awk script to only print hosts that respond</em></p>
|
|
<p>Tags: <a href='tag_bash-v-powershell.html'>bash-v-powershell</a>, <a href='tag_powershell.html'>powershell</a>, <a href='tag_xargs.html'>xargs</a>, <a href='tag_bash.html'>bash</a>, <a href='tag_ping.html'>ping</a>, <a href='tag_awk.html'>awk</a></p>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<!-- text end -->
|
|
<!-- entry end -->
|
|
</div>
|
|
<div id="footer">© <a href="http://twitter.com/zigford_org">Jesse Harris</a> — <a href="mailto:jesse@zigford.org">jesse@zigford.org</a><br/>
|
|
Generated with <a href="https://github.com/cfenollosa/bashblog">bashblog</a>, a single bash script to easily create blogs like this one</div>
|
|
</div></div>
|
|
</body></html>
|