|
|
Scripts to create an IP info summary display
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:
#!/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}'
}
Scripts to create an IP info summary display
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.
Scripts to create an IP info summary display
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...
Scripts to create an IP info summary display
the public IP address area prints the complete HTML returned instead of just the IP address
The sed command needs the -E option
Scripts to create an IP info summary display
And the replacement string must be escaped \1 instead of 1
Scripts to create an IP info summary display
I get an error when I try to use -E with sed, changing it to -e doesn't really seem to work either.
Scripts to create an IP info summary display
Doing that outputs the netmask in reverse order
Since I don't know Perl I piped the output to sed to reverse the order
|
SearchFrom our Sponsor...Latest Mountain Lion HintsWhat's New:HintsNo new hintsComments last 2 daysNo new commentsLinks last 2 weeksNo recent new linksWhat's New in the Forums?
Hints by TopicNews from Macworld
From Our Sponsors |
|
Copyright © 2014 IDG Consumer & SMB (Privacy Policy) Contact Us All trademarks and copyrights on this page are owned by their respective owners. |
Visit other IDG sites: |
|
|
|
Created this page in 0.26 seconds |
|