首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Bash脚本转发端口

Bash脚本转发端口
EN

Stack Overflow用户
提问于 2016-05-13 20:15:26
回答 3查看 4.2K关注 0票数 0

我在谷歌上搜索了很多,但没有找到任何答案。

我想把路由器的8080端口转发到192.168.1.7:5555。我尝试过通过我的路由器的GUI来设置它,但是它不起作用。制作bash脚本并将其放入路由器可以使其成为可能。

如果有人知道如何使用shell脚本进行转发,请帮助我。

编辑

我在路由器上安装了OpenWRT。我无法更新路由器。下面是当我运行"opkg更新“时所得到的错误

代码语言:javascript
复制
Downloading http://downloads.openwrt.org/backfire/10.03.1/ath79/packages/Packages.gz.
wget: server returned error: HTTP/1.1 404 Not Found
Collected errors:
 * opkg_download: Failed to download http://downloads.openwrt.org/backfire/10.03.1/ath79/packages/Packages.gz, wget returned 1

因此,我将无法在其中安装任何软件包。

EN

回答 3

Stack Overflow用户

发布于 2016-05-13 20:34:15

您可以使用迷你型

然后,从端口8080到5555,运行以下命令。

upnpc -a 192.168.1.7 5555 8080 TCP

票数 0
EN

Stack Overflow用户

发布于 2016-05-13 20:41:06

如果您不介意Perl,您可以使用我在http://www.catonmat.net/blog/perl-tcp-proxy/上找到的解决方案:

代码语言:javascript
复制
use warnings;
use strict;

use IO::Socket::INET;
use IO::Select;

my @allowed_ips = ('all', '10.10.10.5');
my $ioset = IO::Select->new;
my %socket_map;

my $debug = 1;

sub new_conn {
    my ($host, $port) = @_;
    return IO::Socket::INET->new(
        PeerAddr => $host,
        PeerPort => $port
    ) || die "Unable to connect to $host:$port: $!";
}

sub new_server {
    my ($host, $port) = @_;
    my $server = IO::Socket::INET->new(
        LocalAddr => $host,
        LocalPort => $port,
        ReuseAddr => 1,
        Listen    => 100
    ) || die "Unable to listen on $host:$port: $!";
}

sub new_connection {
    my $server = shift;
    my $remote_host = shift;
    my $remote_port = shift;

    my $client = $server->accept;
    my $client_ip = client_ip($client);

    unless (client_allowed($client)) {
        print "Connection from $client_ip denied.\n" if $debug;
        $client->close;
        return;
    }
    print "Connection from $client_ip accepted.\n" if $debug;

    my $remote = new_conn($remote_host, $remote_port);
    $ioset->add($client);
    $ioset->add($remote);

    $socket_map{$client} = $remote;
    $socket_map{$remote} = $client;
}

sub close_connection {
    my $client = shift;
    my $client_ip = client_ip($client);
    my $remote = $socket_map{$client};

    $ioset->remove($client);
    $ioset->remove($remote);

    delete $socket_map{$client};
    delete $socket_map{$remote};

    $client->close;
    $remote->close;

    print "Connection from $client_ip closed.\n" if $debug;
}

sub client_ip {
    my $client = shift;
    return inet_ntoa($client->sockaddr);
}

sub client_allowed {
    my $client = shift;
    my $client_ip = client_ip($client);
    return grep { $_ eq $client_ip || $_ eq 'all' } @allowed_ips;
}

die "Usage: $0 <local port> <remote_host:remote_port>" unless @ARGV == 2;

my $local_port = shift;
my ($remote_host, $remote_port) = split ':', shift();


print "Starting a server on 0.0.0.0:$local_port\n";
my $server = new_server('0.0.0.0', $local_port);
$ioset->add($server);

while (1) {
    for my $socket ($ioset->can_read) {
        if ($socket == $server) {
            new_connection($server, $remote_host, $remote_port);
        }
        else {
            next unless exists $socket_map{$socket};
            my $remote = $socket_map{$socket};
            my $buffer;
            my $read = $socket->sysread($buffer, 4096);
            if ($read) {
                $remote->syswrite($buffer);
            }
            else {
                close_connection($socket);
            }
        }
    }
}

开始的时候

代码语言:javascript
复制
./tcp-proxy2.pl 8080 192.168.1.7:5555 &
票数 0
EN

Stack Overflow用户

发布于 2016-05-14 00:45:48

为什么不使用iptable呢?您可以配置端口8080上的每个接收包将被传输到端口5555上的ip 192.168.1.7。

我已经检查和openwrt 应该支持iptable!我不知道你具体的路由器模型,但你可以看看http://wiki.openwrt.org/doc/howto/netfilter#,它会解释你如何使用Netfilters (用户模式程序是iptable)。

iptable是非常有用的工具!

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37218679

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档