Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!


Click here to return to the 'Scripts to create an IP info summary display' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Scripts to create an IP info summary display
Authored by: S Barman on Sep 01, '06 09:49:02PM
Ok... I couldn’t help myself. As a former hardcore UNIX programmer, I had to modify the functions.sh to speed them up. First, rather than use multiple grep’s, I used the full capabilities of awk along with sed and added some perl code.

When writing shell script, there are a few things to consider:

  • Every time you use a pipe, the system sets up a file in the kernel. While a pipe is not written to the disk, pipes use in-memory buffers that have to be managed
  • For each pipe, a new process is started concurrently. If you have a command like cmd | grep x | awk '{print $2}', you start three processes.
  • Understanding shell script nuances does help. For example, a construct like [ -e filename ] && command will check whether filename exists. If it does then command is executed. Using this construct rather than an if-then statement means that there are fewer lines to process. Ok, on today’s system that only reduces the execution time by miliseconds, but (IMHO) every little bit counts!
Without further ado, here are my modifications:
#!/bin/bash
get_mask ()
{
        # one call to perl removes all that conversion stuff and makes this faster
        [ "$1" == "" ] && return
        ifconfig $1 | perl -na -e 'print join(".",unpack("CCCC",pack("L*",oct $F[3]))) if (m/mask/);'
}

get_public ()
{
        # removed two calls to awk with one call to sed
        curl -s http://checkip.dyndns.org | sed 's/.*: ([^<]*)<.*/1/'
}

get_gateway ()
{
        # removed a call to grep and made one call to awk
        netstat -rn | awk '/default/{print $2}'
        # as an alternative, sed would be faster:
        # netstat -rn | sed -n '/default/s/default *([^ ]*).*/1/p'
}

get_dns ()
{
        # instead of a call to cat AND grep, try:
        [ -e /etc/resolv.conf ] && awk '/nameserver/{print $2}' /etc/resolv.conf
}

get_domain ()
{
        # same issue as above
        [ -e /etc/resolv.conf ] && awk '/domain/{print $2}' /etc/resolv.conf
}

get_ip ()
{
        # one call to awk and removed two calls to grep. There's a space after 'inet '
        ifconfig $1 | awk '/inet /{print $2}'
}


[ Reply to This | # ]
Scripts to create an IP info summary display
Authored by: jmdevaney on Sep 02, '06 04:48:32AM

Great info, I knew my script was a kludge but to me that is the great thing about unix. I have a few scripts that some of your suggestions could do wonders in speeding up.

---
Jdevaney



[ Reply to This | # ]
Scripts to create an IP info summary display
Authored by: eagle on Sep 02, '06 05:38:46AM

Doing that outputs the netmask in reverse order. Also, the public IP address area prints the complete HTML returned instead of just the IP address. If I knew enough about sed and unpack/pack, I'd fix it myself...

--------------------------------------------------------------------------------
Public: <html><head><title>Current IP Check</title></head><body>Current IP Address: 24.225.90.70</body></html>
Loopback: 127.0.0.1 / 0.0.0.255
Airport: 10.1.1.2 / 224.255.255.255
Gateway: 10.1.1.1
Domain: earthlink.net
Nameserver: 10.1.1.1
--------------------------------------------------------------------------------



[ Reply to This | # ]
Scripts to create an IP info summary display
Authored by: knouse on Sep 02, '06 12:50:13PM
the public IP address area prints the complete HTML returned instead of just the IP address

The sed command needs the -E option

get_public ()
{
        curl -s http://checkip.dyndns.org | sed -E 's/.*: ([^<]*)<.*/\1/'
}


[ Reply to This | # ]
Scripts to create an IP info summary display
Authored by: knouse on Sep 02, '06 01:40:41PM

And the replacement string must be escaped \1 instead of 1



[ Reply to This | # ]
Scripts to create an IP info summary display
Authored by: ScottTFrazer on Sep 06, '06 06:09:33AM

I get an error when I try to use -E with sed, changing it to -e doesn't really seem to work either.

Also, I always seem to reply to wrong thread. but that's a user issue :-)



[ Reply to This | # ]
Scripts to create an IP info summary display
Authored by: knouse on Sep 02, '06 02:32:06PM
Doing that outputs the netmask in reverse order

Since I don't know Perl I piped the output to sed to reverse the order

get_mask ()
{
        [ "$1" == "" ] && return
        ifconfig $1 | perl -nase 'print join(".",unpack("CCCC",pack("L*",oct $F[3]))) if (m/mask/);' \
            | sed -E 's/([0-9]+)\.([0-9]+)\.([0-9]+)\.([0-9]+)/\4.\3.\2.\1/'
}



[ Reply to This | # ]