From: Michael Shigorin <mike@osdn.org.ua>
To: sysadmins@lists.altlinux.org
Subject: Re: [Sysadmins] скриптик для портфорварда на достижимый IP из альтернатив
Date: Sun, 29 Mar 2009 00:07:27 +0200
Message-ID: <20090328220727.GA26250@osdn.org.ua> (raw)
In-Reply-To: <20090328164513.GV20208@osdn.org.ua>
[-- Attachment #1: Type: text/plain, Size: 631 bytes --]
On Sat, Mar 28, 2009 at 06:45:13PM +0200, I bravely wrote:
> то был нарисован прицепленный скриптик, надеюсь, достаточно
> самодокументированный. При необходимости его можно обобщить
> от двух альтернатив, но решил не усложнять без таковой.
> Также здесь:
> http://fly.osdn.org.ua/~mike/works/misc/notebook-portforward
Поправочка: как root cronjob оказался неэффективным:
++ iptables -L PREROUTING -t nat -n
/usr/local/sbin/notebook-portforward: line 43: iptables: command not found
Пришлось явно добавить /sbin в PATH, обновил.
--
---- WBR, Michael Shigorin <mike@altlinux.ru>
------ Linux.Kiev http://www.linux.kiev.ua/
[-- Attachment #2: notebook-portforward --]
[-- Type: text/plain, Size: 1353 bytes --]
#!/bin/sh
# check which IP is reachable since the notebook can be
# connected via either Ethernet or WiFi, or both;
# adjust portforwarding (which must otherwise be
# already set up correctly -- like not forgetting
# -A INPUT -d $IP -p tcp -m tcp --dport $DPT -j ACCEPT
#
# try to ping an already existing portforward target;
# if unreachable or none, try to ping/add another one
#
# (c) 2009 Michael Shigorin <mike altlinux org>
# use, modify, distribute, cronjob this freely
PATH=/sbin:$PATH
IP=EX.TER.NAL.IP # 123.45.67.8
IP_E=NOTE.BOOK.ETH.IP # 192.168.1.2
IP_W=NOTE.BOOK.WIFI.IP # 192.168.1.3
PORT=EXT_PORT # 12345
DPT=DEST_PORT # 22
COUNT=0
IFS=":"
ipt()
{
iptables "$1" PREROUTING -t nat \
-d "$IP" -p tcp -m tcp \
--dport "$PORT" \
-j DNAT --to-destination "$2:$DPT"
}
remove() { ipt -D "$1"; }
add() { ipt -A "$1"; }
try() { ping -w1 "$1" >&/dev/null; }
# is current one reachable?
COUNT="`iptables -L PREROUTING -t nat -n \
| fgrep "dpt:$port" \
| (
while read header to dest rest; do
try "$dest" && let COUNT=$COUNT+1 || remove "$dest"
done; echo $COUNT
)`"
# any job left?
[ "$COUNT" = 0 ] || exit
for ip in "$IP_W" "$IP_E"; do
try "$ip" && let COUNT=$COUNT+1 && add "$ip" && break
done
# any job still left?
[ "$COUNT" = 0 ] || exit
# safety net: add via wifi if nothing else currently possible
add "$IP_W"
prev parent reply other threads:[~2009-03-28 22:07 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2009-03-28 16:45 Michael Shigorin
2009-03-28 22:07 ` Michael Shigorin [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20090328220727.GA26250@osdn.org.ua \
--to=mike@osdn.org.ua \
--cc=shigorin@gmail.com \
--cc=sysadmins@lists.altlinux.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
ALT Linux sysadmins discussion
This inbox may be cloned and mirrored by anyone:
git clone --mirror http://lore.altlinux.org/sysadmins/0 sysadmins/git/0.git
# If you have public-inbox 1.1+ installed, you may
# initialize and index your mirror using the following commands:
public-inbox-init -V2 sysadmins sysadmins/ http://lore.altlinux.org/sysadmins \
sysadmins@lists.altlinux.org sysadmins@lists.altlinux.ru sysadmins@lists.altlinux.com
public-inbox-index sysadmins
Example config snippet for mirrors.
Newsgroup available over NNTP:
nntp://lore.altlinux.org/org.altlinux.lists.sysadmins
AGPL code for this site: git clone https://public-inbox.org/public-inbox.git