shell一键安装rinetd端口转发工具

post by rocdk890 / 2018-12-24 15:04 Monday linux技术
使用阿里云服务器搭建了2台服务器,但都没有外网ip,所以需要使用rinetd来转发ssh端口来进行访问.
系统:centos 7(64位)
软件:rinetd

cat /root/soft_shell/auto_install_rinetd.sh
#!/bin/bash
# Author rocdk890
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/opt/bin:/opt/sbin:~/bin
export PATH

# Check if user is root
if [ $(id -u) != "0" ]; then
    echo "Error: You must be root to run this script, please use root to install"
    exit 1
fi

# Check the network status
NET_NUM=`ping -c 4 www.baidu.com |awk '/packet loss/{print $6}' |sed -e 's/%//'`
if [ -z "$NET_NUM" ] || [ $NET_NUM -ne 0 ];then
    echo "Please check your internet"
    exit 1
fi

# Check the OS
if [ "$(awk '{if ( $3 >= 7.0 ) print "CentOS 7.x"}' /etc/redhat-release 2>/dev/null)" != "CentOS 7.x" ];then
    err_echo "This script is used for RHEL/CentOS 7.x only."
    exit 1
fi

yum -y install gcc gcc-c++ make

if [ -s rinetd.tar.gz ];then
	echo "rinetd.tar.gz [found]"
else
	wget https://download.slogra.com/rinetd/rinetd.tar.gz
fi

tar zxf rinetd.tar.gz -C /usr/local/
cd /usr/local/rinetd/
sed -i 's/65536/65535/g' rinetd.c
mkdir /usr/man
make && make install

read -p "Enter the local port:" lport
read -p "Enter the remote port:" rport
read -p "Enter the remote ip:" ip

rm -f /etc/rinetd.conf
cat >> /etc/rinetd.conf <<EOF
# 设置允许访问的ip地址信息
# allow 192.168.2.*

# 设置拒绝访问的ip地址信息
# deny 192.168.1.*

# 设置日志文件路径
logfile /var/log/rinetd.log

# 例子: 将本机 8080 端口重定向至 192.168.10.100 的 8080 端口
# 0.0.0.0 8080 192.168.10.100 8080
0.0.0.0 $lport $ip $rport
EOF

cat > /usr/lib/systemd/system/rinetd.service <<EOF
[Unit]
Description=rinetd
After=syslog.target network.target remote-fs.target nss-lookup.target

[Service]
Type=forking
PIDFile=/run/rinetd.pid
ExecStart=/usr/sbin/rinetd -c /etc/rinetd.conf
ExecStop=/bin/kill -s HUP $MAINPID

[Install]
WantedBy=multi-user.target
EOF

最后记得在防火墙里打开你设置绑定在本机的端口.
夜空- 本站版权
1、本站所有主题由该文章作者发表,该文章作者与夜空享有文章相关版权
2、其他单位或个人使用、转载或引用本文时必须同时征得该文章作者和夜空的同意
3、本帖部分内容转载自其它媒体,但并不代表本站赞同其观点和对其真实性负责
4、如本帖侵犯到任何版权问题,请立即告知本站,本站将及时予与删除并致以最深的歉意
5、原文链接:blog.slogra.com/post-747.html

标签: centos 端口 shell 脚本 绑定 一键 tcp rinetd

评论: