0

I have a remote device that sends data to an IP address through a specific port. The problem I want to solve is that I need it sends data to a domain, but device only allow set IP addresses.

I want to know, how can I configure Linux in order to do a IP routing to a given domain.

Is that possible?

I could set up the device to address the domain's ip, but this IP address is dynamic. The domain I have is a noip.com's domain.

2 Answers 2

1

2-part solution:

  1. Using IPTABLES, you can do some PREROUTING to capture all traffic to a.b.c.d (the remote server), then mask that traffic and redirect it to e.f.g.h (your noip.com IP)

  2. Using a cron script, running every N minutes, if the IP has changed, purge the IPTABLES rules and re-insert them with the new IP.

I haven't tested it, but this is how I would begin:

iptables -t nat -I PREROUTING -s [localwanip] -d [remoteip] -p -m tcp --dport [port] --to-destination [newremoteip]

...and the script, again proofing required (results may vary depending on versions and system flavour):

#!/bin/bash

NOIPNAME=yourname.noip.com
# Your IP on your WAN interface
LOCALIP=1.2.3.4
# The IP the software is _mistakenly_ trying to talk to
REMOTEIP=4.3.2.1
# The TCP port the software is using to connect to the remote IP
PORT=1234
# Just a file to keep track of what the last IP was...
REMEMBERFILE=/var/run/oldip.txt

# and now the magic, if it works...

HOSTLINE=$(host $NOIPNAME ns1.no-ip.com | grep 'has address')
HOSTLEN=$(echo $HOSTLINE | wc -m)

# Make sure the return string is > 8 characters (1.2.3.4\n)
if [ $HOSTLEN -lt 8 ]; then
  # Host resolve failure
  echo "Bad host"
  exit 1
fi

# Extract the IP from the return string.
DYNIP=$(echo $HOSTLINE | sed -rn 's/^.* ([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}).*/\1/p')

OLDIP=$(cat $REMEMBERFILE)

if [ "x$DYNIP" = "x$OLDIP" ]; then
    # Nothing to do.
    exit 0
else
    echo $DYNIP > $REMEMBERFILE
fi

# Flush
iptables -t nat -F
# and re-write
iptables -t nat -I PREROUTING -s $LOCALIP -d $REMOTEIP -p -m tcp --dport $PORT --to-destination $DYNIP

exit 0

Lastly, add it to cron; the below runs every 10 minutes (via this part: */10).

echo "*/10    *  *  *   *   root /your/path/to/script" >> /etc/crontab

Caveats: Now the IPTABLES will be tricky, if the command I gave doesn't work (probably won't - it was just a guess) - have a dig in google, for example:

http://ribbalicious.com/rewrite-destination-ip-address-using-iptables/

Good luck.

0

You should set the IP address of a Linux system in the device. This Linux would do NAT (iptables, see DNAT). It would also check regularly for DNS updates and update the DNAT configuration accordingly. Of course, you may lose some data due to the IP changes.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.