miniupnp.tuxfamily.org Forum Index miniupnp.tuxfamily.org
The forum about miniupnp and libnatpmp
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Improving the "external-ip" script

 
Post new topic   Reply to topic    miniupnp.tuxfamily.org Forum Index -> miniupnpc Bugs
View previous topic :: View next topic  
Author Message
unitedwallabies



Joined: 23 Sep 2011
Posts: 8
Location: North-Eastern US

PostPosted: Wed Oct 26, 2011 6:51 pm    Post subject: Improving the "external-ip" script Reply with quote

The one-line script invokes `upnpc -s' and parses the output looking for line(s) matching "ExternalIP". The parsing currently uses both grep and sed. Combining grep, cut, awk, and sed on the same pipe-line, though sometimes convenient, is always sub-optimal -- it is Ok for one's own one-shot command-line, but not for an installable utility. Below is a more efficient implementation, that produces equivalent result at much lower cost. Though both approaches are very cheap on modern computers, there is no never a need to invoke more processes than necessary...
Code:
#!/bin/sh
# $Id: external-ip.sh,v 1.1 2010/08/05 12:57:41 nanard Exp $
# (c) 2010 Reuben Hawkins
exec upnpc "$@" -s | exec sed -e "s/^ExternalIPAddress = //" -e t -e d

Using exec(1) allows subshell-process(es) to bypass fork(2)-ing in order to execute the command... Some sh-implementations are smart enough to automatically detect the possibility of such bypassing on their own, but not all.

The "$@" allows to pass other options (such as -u descURL) to the program:
Quote:
% external-ip -u http://192.168.1.1:2555/upnp/6c352473-8521-319e-8757-639e9dca9979/desc.xml
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    miniupnp.tuxfamily.org Forum Index -> miniupnpc Bugs All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group
Protected by Anti-Spam ACP
© 2007 Thomas Bernard, author of MiniUPNP.