├── README.md └── socat.sh /README.md: -------------------------------------------------------------------------------- 1 | Socat一键安装脚本 2 | ----------- 3 | 系统要求:支持CentOS 6+ 、Debian 7+、Ubuntu 14+ 4 | 5 | 脚本说明:脚本默认开启`UDP`、`TCP`转发,带开机自启功能,且一次只能转发单个端口,如果想转发多个端口请重复运行本脚本。 6 | 7 | 使用`root`运行以下命令: 8 | 9 | wget https://raw.githubusercontent.com/Einic/Socat/master/socat.sh && bash socat.sh 10 | 11 | 按要求输入本地服务器端口,要转发的目标端口和服务器IP即可! 12 | 13 | 14 | -------------------------------------------------------------------------------- /socat.sh: -------------------------------------------------------------------------------- 1 | #! /bin/bash 2 | PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin 3 | export PATH 4 | 5 | # ==================================================== 6 | # System Request:CentOS 6+ 、Debian 7+、Ubuntu 14+ 7 | # Author: Rat's 8 | # Dscription: Socat一键脚本 9 | # Version: 1.0 10 | # Blog: https://www.infvie.com 11 | # Github:https://github.com/Einic/Socat 12 | # ==================================================== 13 | 14 | Green="\033[32m" 15 | Font="\033[0m" 16 | Blue="\033[33m" 17 | 18 | rootness(){ 19 | if [[ $EUID -ne 0 ]]; then 20 | echo "Error:This script must be run as root!" 1>&2 21 | exit 1 22 | fi 23 | } 24 | 25 | checkos(){ 26 | if [[ -f /etc/redhat-release ]];then 27 | OS=CentOS 28 | elif cat /etc/issue | grep -q -E -i "debian";then 29 | OS=Debian 30 | elif cat /etc/issue | grep -q -E -i "ubuntu";then 31 | OS=Ubuntu 32 | elif cat /etc/issue | grep -q -E -i "centos|red hat|redhat";then 33 | OS=CentOS 34 | elif cat /proc/version | grep -q -E -i "debian";then 35 | OS=Debian 36 | elif cat /proc/version | grep -q -E -i "ubuntu";then 37 | OS=Ubuntu 38 | elif cat /proc/version | grep -q -E -i "centos|red hat|redhat";then 39 | OS=CentOS 40 | else 41 | echo "Not supported OS, Please reinstall OS and try again." 42 | exit 1 43 | fi 44 | } 45 | 46 | disable_selinux(){ 47 | if [ -s /etc/selinux/config ] && grep 'SELINUX=enforcing' /etc/selinux/config; then 48 | sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config 49 | setenforce 0 50 | fi 51 | } 52 | 53 | disable_iptables(){ 54 | systemctl stop firewalld.service >/dev/null 2>&1 55 | systemctl disable firewalld.service >/dev/null 2>&1 56 | service iptables stop >/dev/null 2>&1 57 | chkconfig iptables off >/dev/null 2>&1 58 | } 59 | 60 | get_ip(){ 61 | ip=`curl http://whatismyip.akamai.com` 62 | } 63 | 64 | config_socat(){ 65 | echo -e "${Green}请输入Socat配置信息!${Font}" 66 | read -p "请输入本地端口:" port1 67 | read -p "请输入远程端口:" port2 68 | read -p "请输入远程IP:" socatip 69 | } 70 | 71 | start_socat(){ 72 | echo -e "${Green}正在配置Socat...${Font}" 73 | nohup socat TCP4-LISTEN:${port1},reuseaddr,fork TCP4:${socatip}:${port2} >> /root/socat.log 2>&1 & 74 | nohup socat -T 600 UDP4-LISTEN:${port1},reuseaddr,fork UDP4:${socatip}:${port2} >> /root/socat.log 2>&1 & 75 | if [ "${OS}" == 'CentOS' ];then 76 | sed -i '/exit/d' /etc/rc.d/rc.local 77 | echo "nohup socat TCP4-LISTEN:${port1},reuseaddr,fork TCP4:${socatip}:${port2} >> /root/socat.log 2>&1 & 78 | nohup socat -T 600 UDP4-LISTEN:${port1},reuseaddr,fork UDP4:${socatip}:${port2} >> /root/socat.log 2>&1 & 79 | " >> /etc/rc.d/rc.local 80 | chmod +x /etc/rc.d/rc.local 81 | elif [ -s /etc/rc.local ]; then 82 | sed -i '/exit/d' /etc/rc.local 83 | echo "nohup socat TCP4-LISTEN:${port1},reuseaddr,fork TCP4:${socatip}:${port2} >> /root/socat.log 2>&1 & 84 | nohup socat -T 600 UDP4-LISTEN:${port1},reuseaddr,fork UDP4:${socatip}:${port2} >> /root/socat.log 2>&1 & 85 | " >> /etc/rc.local 86 | chmod +x /etc/rc.local 87 | else 88 | echo -e "${Green}检测到系统无rc.local自启,正在为其配置... ${Font} " 89 | echo "[Unit] 90 | Description=/etc/rc.local 91 | ConditionPathExists=/etc/rc.local 92 | 93 | [Service] 94 | Type=forking 95 | ExecStart=/etc/rc.local start 96 | TimeoutSec=0 97 | StandardOutput=tty 98 | RemainAfterExit=yes 99 | SysVStartPriority=99 100 | 101 | [Install] 102 | WantedBy=multi-user.target 103 | " > /etc/systemd/system/rc-local.service 104 | echo "#!/bin/sh -e 105 | # 106 | # rc.local 107 | # 108 | # This script is executed at the end of each multiuser runlevel. 109 | # Make sure that the script will "exit 0" on success or any other 110 | # value on error. 111 | # 112 | # In order to enable or disable this script just change the execution 113 | # bits. 114 | # 115 | # By default this script does nothing. 116 | " > /etc/rc.local 117 | echo "nohup socat TCP4-LISTEN:${port1},reuseaddr,fork TCP4:${socatip}:${port2} >> /root/socat.log 2>&1 & 118 | nohup socat -T 600 UDP4-LISTEN:${port1},reuseaddr,fork UDP4:${socatip}:${port2} >> /root/socat.log 2>&1 & 119 | " >> /etc/rc.local 120 | chmod +x /etc/rc.local 121 | systemctl enable rc-local >/dev/null 2>&1 122 | systemctl start rc-local >/dev/null 2>&1 123 | fi 124 | get_ip 125 | sleep 3 126 | echo 127 | echo -e "${Green}Socat安装并配置成功!${Font}" 128 | echo -e "${Blue}你的本地端口为:${port1}${Font}" 129 | echo -e "${Blue}你的远程端口为:${port2}${Font}" 130 | echo -e "${Blue}你的本地服务器IP为:${ip}${Font}" 131 | exit 0 132 | } 133 | 134 | install_socat(){ 135 | echo -e "${Green}即将安装Socat...${Font}" 136 | if [ "${OS}" == 'CentOS' ];then 137 | yum install -y socat 138 | else 139 | apt-get -y update 140 | apt-get install -y socat 141 | fi 142 | if [ -s /usr/bin/socat ]; then 143 | echo -e "${Green}Socat安装完成!${Font}" 144 | fi 145 | } 146 | 147 | status_socat(){ 148 | if [ -s /usr/bin/socat ]; then 149 | echo -e "${Green}检测到Socat已存在,并跳过安装步骤!${Font}" 150 | main_x 151 | else 152 | main_y 153 | fi 154 | } 155 | 156 | main_x(){ 157 | checkos 158 | rootness 159 | disable_selinux 160 | disable_iptables 161 | config_socat 162 | start_socat 163 | } 164 | 165 | main_y(){ 166 | checkos 167 | rootness 168 | disable_selinux 169 | disable_iptables 170 | install_socat 171 | config_socat 172 | start_socat 173 | } 174 | 175 | status_socat 176 | --------------------------------------------------------------------------------