for i in 192.168.1.{61..71};do ping -c 1 $i &> /dev/null && echo $i;fi;done

Echo the local IP addresses of the machines on your local network

If you need to ssh into a computer on the local network but you're unsure of the ip to use, then ping them and see if you get a response. If you do, print out the address you got it from. Adjust the range to suit your network.

2009-08-26 06:04:24

These Might Interest You

  • 1
    xhost +local:
    peppet · 2010-02-17 09:20:39 0
  • This also works on non-Linux machines. If you have GNU sed you can do it more elegantly: ifconfig | sed -n 's/^\s*inet \(addr:\)\?\([^\s]*\) .*/\2/;T;/^127\./d;p'

    ifconfig | sed -ne 's/^.*inet \(addr:\)*\([^ ]*\).*/\2/;te' -e 'd;:e' -e '/^127\./d;p'
    dramaturg · 2010-12-05 17:14:57 0
  • Mount a Windows share. Usually the IP is needed for the $ip_or_host option. Getting hostnames working on a local network never seems to work.

    sudo mount -t cifs //$ip_or_host/$sharename /mnt
    sb · 2009-11-23 14:24:02 0
  • David thanks for that grep inside! here is mine version: psgrep() { case ${1} in ( -E | -e ) local EXTENDED_REGEXP=1 shift 1 ;; *) local EXTENDED_REGEXP=0 ;; esac if [[ -z ${*} ]] then echo "psgrep - grep for process(es) by keyword" >&2 echo "Usage: psgrep [-E|-e] ... " >&2 echo "" >&2 echo "option [-E|-e] enables full extended regexp support" >&2 echo "without [-E|-e] plain strings are looked for" >&2 return 1 fi \ps -eo 'user,pid,pcpu,command' w | head -n1 local ARG='' if (( ${EXTENDED_REGEXP} == 0 )) then while (( ${#} > 0 )) do ARG="${1}" shift 1 local STRING=${ARG} local LENGTH=$(expr length ${STRING}) local FIRSCHAR=$(echo $(expr substr ${STRING} 1 1)) local REST=$(echo $(expr substr ${STRING} 2 ${LENGTH})) \ps -eo 'user,pid,pcpu,command' w | grep "[${FIRSCHAR}]${REST}" done else \ps -eo 'user,pid,pcpu,command' w | grep -iE "(${*})" fi }

    psgrep() ... func to long, please look under "description"
    Xk2c · 2015-01-01 02:58:48 0
  • Display a list of local shell scripts soft-linked to /usr/local/bin Put local shell scripts to local ~/bin/ directory and soft-link them to /usr/local/bin/ which is in the $PATH variable to run them from anywhere. Show Sample Output

    for AAA in $(find /usr/local/bin -type l); do ls -gG "${AAA}"; done
    rgregor · 2013-10-01 10:49:12 0
  • This improves on #9892 by compressing the directory on the remote machine so that the amount of data transferred over the network is much smaller. The command uses ssh(1) to get to a remote host, uses tar(1) to archive and compress a remote directory, prints the result to STDOUT, which is written to a local file. In other words, we are archiving and compressing a remote directory to our local box.

    ssh user@host "tar -zcf - /path/to/dir" > dir.tar.gz
    __ · 2011-12-16 05:48:38 2

What Others Think

Check out nmap: nmap -sT -p 22 192.168.1.* Also if the host that is running ssh has a host name, you can just use the host name in the LAN. i.e. ssh user@hostname Thanks zeroconf. :-)
Abiden · 455 weeks and 4 days ago
@ AlecSchueler: uneccessary test (aka [): for i in 192.168.1.{61..71}; if ping -c 1 $i &> /dev/null; then echo $i;fi;done If you don't have nmap, you can try if the ssh port is open with nc: nc -w1 -z 22 @Abiden: To replicate what AlecSchueler did, your command should be: nmap -sT -p 22
0x89 · 455 weeks and 3 days ago
@0x89 You missed a 'do' for i in 192.168.1.{61..71}; do if ping -c 1 $i &> /dev/null; then echo $i;fi;done Or for i in 192.168.1.{61..71}; do ping -c 1 "$i" &> /dev/null && then echo "$i" ;done
DaveQB · 455 weeks and 3 days ago
Rather for i in 192.168.1.{61..71}; do ping -c 1 "$i" &> /dev/null && echo "$i" ;done
DaveQB · 455 weeks and 3 days ago
@0x89 " Echo the local IP addresses of the machines on your local network If you need to ssh into a computer on the local network but you're unsure of the ip to use" The user is not sure what host is running ssh on it. So why not scan the whole range? I guess AlecSchueler may have to be a bit more specific. Maybe I put too much focus into, "you're unsure of the ip to use " Also "192.168.1.{61..71}", does not even work. Here's a solution if you do not have nmap but you have nc at hand. for i in $(seq 61 71); do nc -vv -z -w1 192.168.1.${i} 22; done Also: @0x89 For netcat to produce any output, you must specify at least one '-v'.
Abiden · 455 weeks and 3 days ago
@Abiden Yes, this was intended for use when you didn't know the IP at all. 192.168.1.{61..71} works fine for me in bash 4.0.28 though. @0x89 What if you have neither nmap or nc installed?
AlecSchueler · 455 weeks and 3 days ago
@0x89 Yeah, I don't know how I forgot about &&. Silly mistake.
AlecSchueler · 455 weeks and 3 days ago
@Alec I was mistaken, about '192.168.1.{61..67}'. When I had tested it, I had used an additional '.', so that's why it didn't work for me. lol Also Alec, it seems that most distros are including netcat as of recently from my experience.
Abiden · 455 weeks and 2 days ago

What do you think?

Any thoughts on this command? Does it work on your machine? Can you do the same thing with only 14 characters?

You must be signed in to comment.

What's this? is the place to record those command-line gems that you return to again and again. That way others can gain from your CLI wisdom and you from theirs too. All commands can be commented on, discussed and voted up or down.

Share Your Commands

Stay in the loop…

Follow the Tweets.

Every new command is wrapped in a tweet and posted to Twitter. Following the stream is a great way of staying abreast of the latest commands. For the more discerning, there are Twitter accounts for commands that get a minimum of 3 and 10 votes - that way only the great commands get tweeted.


Subscribe to the feeds.

Use your favourite RSS aggregator to stay in touch with the latest commands. There are feeds mirroring the 3 Twitter streams as well as for virtually every other subset (users, tags, functions,…):

Subscribe to the feed for: