You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

184 lines
5.2 KiB

<html>
<head>
<title>WAN IP Retrieval and Dynamic DNS (mdns)</title>
<style type="text/css">
#main {
width: 800px;
line-height: 1.1em;
}
</style>
</head>
<body>
<div id="main">
<h3>WAN IP Retrieval and Dynamic DNS (mdns)</h3>
<p>There are myriad ways to automatically keep your
<strong>mdns</strong> updated on SDF. Below are a couple:</p>
<p>The first script depends on passwordless SSH logons. Also,
make sure the file permissions for your scripts are executable.
For more info, see the SDF tutorial on <a
href="http://sdf.lonestar.org/index.cgi?tutorials/permissions">File
Permissions</a>.</p>
<h4>Script 1</h4>
<p>Using the variables $SSH_CLIENT and $SSH_CONNECTION, after logging
into a remote site (any remote site), you can learn your local WAN IP
address by executing either of the following two commands:</p>
<code><pre>echo $SSH_CLIENT</pre></code>
<code><pre>echo $SSH_CONNECTION</pre></code>
<p>We will take advantage of this simply by issuing a remote SSH
command from our home computer.</p>
<p>First, somewhere in your SDF directory (ex: ~/bin), create
a file called <strong>ip</strong> and put the following lines in
this file:</p>
<code>
<pre>
#!/bin/sh
IP=$(echo $SSH_CLIENT | awk '{print $1}')
OLDIP=$(cat ~/bin/.ip)
if [ "$IP" != "$OLDIP" ] ;
then
echo "$IP" > ~/bin/.ip
mdns "$IP"
fi
</pre>
</code>
<p>This will check your connecting IP against the local file <strong>
.ip,</strong> if the IP is the same it does nothing. If it has
changed (or if the file .ip does not exist) it updates
the mdns record and the <strong>.ip</strong> file.</p>
<p>Next, execute that remote script with a SSH command from your
home computer using the following code (Example filename:
<strong>ipset</strong>):</p>
<code>
<pre>
#!/bin/ksh
ssh me@remote.host '~/bin/ip' &#62;/dev/null
</pre>
</code>
<p>Standard output is directed to /dev/null because we will add
this script to cron and it's not necessary to have the output
mailed to you if cron is set up that way. If desired, redirect
standard output to a logfile like so:</p>
<code>
<pre>
#!/bin/ksh
ssh me@remote.host '~/bin/ip' &#62;&#62;$HOME/logs/ip_update
</pre>
</code>
<p>Add the script to your home machine's cron and execute every 24
hours or so. Ex:</p>
<code>
<pre>
* */24 * * * $HOME/bin/ipset
</pre>
</code>
<h4>Note</h4>
<p>The script could again be modified to echo back your IP address.
Name it <strong>ipecho</strong> or something like that. Here is the
script:</p>
<code>
<pre>
IP=$(ssh me@remote.host 'echo $SSH_CLIENT' | awk '{print $1}')
echo $IP
</pre>
</code>
<h4>Script 2</h4>
<h4>CGI script, usable with VHOST membership</h4>
<p>If you have a VHOST membership (or MetaARPA), you can use the
following CGI script to set the IP via an HTTP request. The advantage
is that you do not need to set up ssh keys for this; the disadvantage
is that somebody sniffing on your traffic could update your mdns
to some other address - so you should not use this script for
security critical applications where you rely on authorized mdns
setting (but feel free to improve the script as an exercise to
include some authentication).</p>
<p>Copy the script into some accessible directory of your VHOSTed site,
change the keyword as you like, and call the script with any browser or
applications like wget with something like</p>
<code>wget http://yourlogin.freeshell.org/setmdns.cgi?keyword</code><br/>
<p>where you set appropriate paths and replace the keyword according to your
choice (it is currently set to 'setmdns'). Note also that on SDF-EU, the
command to set the dynamic address is <code>zdns</code>, not mdns.</p>
<p>If everything is ok, the script will respond with the output of
the mdns command; otherwise, it will simply echo your query. All
calls are logged, including the IP of the calling host and the
submitted query string. The script keeps track of the IP and runs
the mdns command only if the IP has changed since the last call.</p>
<p>It is based on the fact that your user id is the first part of
the address for VHOSTed sites; as the HOME directory is normally
not set for a CGI script, but needed for mdns, the script then gets
this information by locally 'finger' the user id. This looks like
a good way to find the home directory based on the user id. The
script then changes to that directory and checks for the keyword
in the query string, and if found, executes mdns with the IP of
the calling client.</p>
<h5>CGI script</h5>
<code><pre>
#!/bin/sh
# GPL 2007,2009 Yargo C Bonetti
# Use however you like, at your own risk!
OLDIP=./.oldip
DNSCOM=mdns
LOGF=./.setmdns.log
KEYWORD=setmdns
LOGNAME=${LOGNAME:-${HTTP_HOST%%.*}}
HOME=`finger $LOGNAME|awk '/^Directory:/{print $2}'`
export LOGNAME HOME
echo "`date -u +%c` $REMOTE_ADDR $QUERY_STRING" >>$LOGF
chmod 600 $LOGF
cat <<EOH
echo Content-type: text/plain
echo
echo $REMOTE_ADDR
if echo "$QUERY_STRING" | grep $KEYWORD 2>&1 >/dev/null ; then
if [ -d "$HOME" ] ; then
if [ "$REMOTE_ADDR" = "`cat $OLDIP`" ] ; then
echo "Keeping ip at $REMOTE_ADDR"
else
$DNSCOM $REMOTE_ADDR
echo $REMOTE_ADDR >$OLDIP
fi
else echo "no $DNSCOM due to bad HOME=$HOME"
fi
else
echo $QUERY_STRING
fi
</pre></code>
<br/>
$Id: mdns-tutorial.html,v 1.19 2019/09/29 15:35:43 amrowsell Exp $
</div> <!-- main -->
</body>
</html>