I'm writing a shell script that I want to be able to run some command, like arp -a
, to get the IPs of everyone on the network and then try SSHing to each of them in turn. Problem is, I have no idea how to send the IPs from arp -a
to the SSH command without typing the IP in manually (not going to work, as I'm writing an executable file.) Is this even possible?
Short answer: You can write a small script to extract the IP in from arp
and process each one. You can use bash
loop, or other tools (xargs
) to process multiple IP.
Bash solution
#! /bin/bash
# Read arp line like: '_gateway (192.168.215.3) at 00:52:58:e7:cf:5f [ether] on ens33`
while read tag ip x ; do
# Strip leading and trailing characters from the IP
ip=${ip:1:-1}
# Execute ssh
ssh ... "$ip"
done <<< "$(arp -a)"
Update bash version BEFORE 4.2:
Based on input in comment from @cyrus.
Using negative length on substring was added in bash 4.2. For older older versions, use the following instead of ip=${ip:1:-1}
to remove leading '(' and trailing ')'
ip=${ip#(} ; ip=${ip%)}