The UNIX command ping is used to test if specific machines are active on a network
$ ping 10.0.1.9
PING 10.0.1.9 (10.0.1.9): 56 data bytes
64 bytes from 10.0.1.9: icmp_seq=0 ttl=255 time=0.619 ms
[...]
I have been using ping for years but last week I found out that you can ping the broadcast address of a network (x.x.x.255) and see all the machines on that network that are configured to respond to ping:
$ ping 10.0.1.255
PING 10.0.1.255 (10.0.1.255): 56 data bytes
64 bytes from 10.0.1.10: icmp_seq=0 ttl=64 time=0.071 ms
64 bytes from 10.0.1.9: icmp_seq=0 ttl=255 time=0.534 ms
64 bytes from 10.0.1.1: icmp_seq=0 ttl=255 time=0.544 ms
[...]
Really useful if you need the IP address for a machine that used DHCP to assign its address
A complementary command is arp -a
This displays information of all the network interfaces for a machine and reports the interface, the IP address and the MAC address of each entry in the address resolution tables.
$ arp -a
? (10.0.1.1) at b8:8d:12:5a:ad:77 on en0 ifscope [ethernet]
? (10.0.1.2) at b8:8d:12:5a:ad:77 on en0 ifscope [ethernet]
? (10.0.1.6) at 70:56:81:ad:b6:d1 on en0 ifscope [ethernet]
? (10.0.1.7) at b8:8d:12:5a:ad:77 on en0 ifscope [ethernet]
? (10.0.1.9) at 70:56:81:c5:fb:2d on en0 ifscope [ethernet]
[...]
Note that these addresses are not necessarily active. You can see link reachability information using arp -al
A collection of computer systems and programming tips that you may find useful.
Brought to you by Craic Computing LLC, a bioinformatics consulting company.
Showing posts with label unix. Show all posts
Showing posts with label unix. Show all posts
Monday, February 10, 2014
Friday, June 7, 2013
Unix command 'comm' for comparing files
I needed a simple way to compare two similar files and only output lines that were unique to the second file.
Sounded like a job for 'diff' but I was not finding the right options to give me what I needed. And then I stumbled across 'comm' - a standard UNIX command that I don't think I have ever used.
That does exactly what I needed. My two files look like this
File A
File A
A B D EFile B
A B C DI want the command to just output 'C' By default comm compares two files and produces 3 columns of text - lines that are only in file A, lines that are only in file B and lines that are in both. So with these two files I get:
$ comm tmp_A tmp_B
A
B
C
D
E
Ugly, and not what I want... But then you can suppress the output of one or more of these columns using -1, -2, -3 options and combinations of those. I want to suppress lines that are only in file A and those in common:
$ comm -13 tmp_A tmp_B
C
Simple - does exactly what I need - can't believe I didn't know about it...
Thursday, June 7, 2012
Setting a Title for a Tab in iTerm2 on Mac OS X
iTerm2 (http://www.iterm2.com) by George Nachman is an excellent terminal emulator for Mac OS X that is highly configurable. I it prefer over the system Terminal application.
I tend to have a number of terminal sessions open at the same time and for a while I have wanted a way to give each Tab in the window an informative name. You can do that through the iTerm2 preferences but I wanted a way to set it directly from the shell command line.
iTerm2 can be scripted via AppleScript and so I wrote a simple script to do the job. It uses a system application called osascript which will execute a AppleScript script from the Unix command line.
The code is freely available at https://github.com/craic/iterm_title - see the README for more details.
The code should be a useful starting point if you want to write something in AppleScript that you can call from UNIX.
I tend to have a number of terminal sessions open at the same time and for a while I have wanted a way to give each Tab in the window an informative name. You can do that through the iTerm2 preferences but I wanted a way to set it directly from the shell command line.
iTerm2 can be scripted via AppleScript and so I wrote a simple script to do the job. It uses a system application called osascript which will execute a AppleScript script from the Unix command line.
The code is freely available at https://github.com/craic/iterm_title - see the README for more details.
The code should be a useful starting point if you want to write something in AppleScript that you can call from UNIX.
Friday, January 27, 2012
Getting absolute paths with the Unix ls command
The standard Unix command ls lists filenames and directories in the specified directory. The default behaviour is to list just the filenames as including the full pathname would clutter the screen.
But sometimes you want the absolute paths. I need this all the time if I want to create a file containing a list fo filenames. The obvious command to get all the YAML files, for example, is:
$ ls -1 *yml
A.yml
B.yml
In order to get the full pathnames you need to use this:
$ ls -1 -d $PWD/*yml
/home/jones/A.yml
/home/jones/B.yml
But sometimes you want the absolute paths. I need this all the time if I want to create a file containing a list fo filenames. The obvious command to get all the YAML files, for example, is:
$ ls -1 *yml
A.yml
B.yml
In order to get the full pathnames you need to use this:
$ ls -1 -d $PWD/*yml
/home/jones/A.yml
/home/jones/B.yml
Wednesday, November 16, 2011
Deleting a File that starts with '-' on UNIX
Filenames that begin with 'special' characters like '-', '.' or '*' cause problems on Unix. Standard commands like ls or rm view the characters as signifying command options.
You don't typically create files with names like this but they can arise through errors such as cut and pasting text into your command line.
Simply escaping the character or quoting the filename does not work.
The solution is to use a longer path to the file - the easiest being a relative path to the same directory.
If the filename is '--myfile' you will get an error like this:
$ ls --myfile
ls: illegal option -- -
usage: ls [-ABCFGHLOPRSTUWabcdefghiklmnopqrstuwx1] [file ...]
But this works just fine:
$ ls ./--myfile
./--myfile
Wednesday, August 10, 2011
Unzipping multiple zip files on Unix
To unzip multiple .zip files with one command command on a UNIX system the obvious command to try is 'unzip *.zip' - but that doesn't work...
UNIX is expanding the wildcard and passing that string to unzip, which thinks that you are asking for specific files from the zip archive given as the first argument.
You need to escape the wildcard with a backslash so that 'unzip' sees it and expands it correctly.
Glad I stumbled on that - saves me a lot of time...
$ unzip *zip
Archive: ipab20080103_wk01.zip
caution: filename not matched: ipab20080110_wk02.zip
caution: filename not matched: ipab20080117_wk03.zip
[...]
UNIX is expanding the wildcard and passing that string to unzip, which thinks that you are asking for specific files from the zip archive given as the first argument.
You need to escape the wildcard with a backslash so that 'unzip' sees it and expands it correctly.
$ unzip \*.zip
Archive: ipab20080103_wk01.zip
inflating: ipab20080103.xml
inflating: ipab20080103lst.txt
inflating: ipab20080103rpt.html
Archive: ipab20080110_wk02.zip
inflating: ipab20080110.xml
inflating: ipab20080110lst.txt
inflating: ipab20080110rpt.html
[...]
Glad I stumbled on that - saves me a lot of time...
Monday, October 11, 2010
/etc/paths.d as a way to configure Paths in Mac OS X
Just found out about a simple way to manage paths to installed software in Mac OS X
As well as, or instead of, setting PATH environment variables in startup files, you can simply add a path as a file to the directory /etc/paths.d
For example, here is a command that sets up a path to a custom installation of MongoDB
Start up a new shell and you will find that path added to your PATH environment variable.
Note that this is a system wide setting and so it gets set early in the process of shell creation. If you need to override PATH settings for specific users then you still need to set paths in .bashrc, etc. I don't know the order in which /etc/path.d files are sourced but I would assume alphabetical.
Also note that you need the 'sh -c ' construct in order to create the file as shown above. Simple 'sudo echo' will not work.
As well as, or instead of, setting PATH environment variables in startup files, you can simply add a path as a file to the directory /etc/paths.d
For example, here is a command that sets up a path to a custom installation of MongoDB
$ sudo sh -c 'echo "/Users/jones/Documents/mypath/mongodb/bin" > /etc/paths.d/mongodb'Start up a new shell and you will find that path added to your PATH environment variable.
Note that this is a system wide setting and so it gets set early in the process of shell creation. If you need to override PATH settings for specific users then you still need to set paths in .bashrc, etc. I don't know the order in which /etc/path.d files are sourced but I would assume alphabetical.
Also note that you need the 'sh -c ' construct in order to create the file as shown above. Simple 'sudo echo' will not work.
Subscribe to:
Posts (Atom)