* [Sysadmins] скриптик для портфорварда на достижимый IP из альтернатив
@ 2009-03-28 16:45 Michael Shigorin
2009-03-28 22:07 ` Michael Shigorin
0 siblings, 1 reply; 2+ messages in thread
From: Michael Shigorin @ 2009-03-28 16:45 UTC (permalink / raw)
To: sysadmins
[-- Attachment #1: Type: text/plain, Size: 1006 bytes --]
Здравствуйте.
Добрался поднять дома WiFi. Работает. Теперь добрался решить
порождённую этим проблему портфорварда на хост с уже двумя
интерфейсами -- городить несколько портов неудобно, поскольку
маршрутизирующая система более приспособлена к проверке, как бы
добраться до хоста, чем человек.
Поскольку ничего существующего не нагуглилось по
iptables forward "same port" different addresses
iptables forward "same port" multiple destination
-- самое близкое:
http://ubuntuforums.org/archive/index.php/t-497781.html
(а спрашивавшему IMHO стоило просто делать tcp reset на tcp/113:
-A INPUT -p tcp -m tcp --dport 113 -j REJECT --reject-with tcp-reset),
то был нарисован прицепленный скриптик, надеюсь, достаточно
самодокументированный. При необходимости его можно обобщить
от двух альтернатив, но решил не усложнять без таковой.
Также здесь:
http://fly.osdn.org.ua/~mike/works/misc/notebook-portforward
--
---- WBR, Michael Shigorin <mike@altlinux.ru>
------ Linux.Kiev http://www.linux.kiev.ua/
[-- Attachment #2: notebook-portforward --]
[-- Type: text/plain, Size: 1334 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
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"
^ permalink raw reply [flat|nested] 2+ messages in thread
* Re: [Sysadmins] скриптик для портфорварда на достижимый IP из альтернатив
2009-03-28 16:45 [Sysadmins] скриптик для портфорварда на достижимый IP из альтернатив Michael Shigorin
@ 2009-03-28 22:07 ` Michael Shigorin
0 siblings, 0 replies; 2+ messages in thread
From: Michael Shigorin @ 2009-03-28 22:07 UTC (permalink / raw)
To: sysadmins
[-- 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"
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2009-03-28 22:07 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2009-03-28 16:45 [Sysadmins] скриптик для портфорварда на достижимый IP из альтернатив Michael Shigorin
2009-03-28 22:07 ` Michael Shigorin
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