Last active
October 17, 2024 19:31
-
-
Save smarteist/560b9fea17937248c674dac779f4ad7d to your computer and use it in GitHub Desktop.
This script runs ssh proxy in local socks5 port 1080
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/bash -i | |
| user="root" | |
| pass="@PASS@@@@" | |
| ip_address="111.111.11.11" | |
| ssh_port=22 | |
| sudo_pass_file="/tmp/sshpass" | |
| proxy_port=1080 | |
| # Find PID listening on the proxy port | |
| pid=$(sudo lsof -t -i:"$proxy_port") | |
| if [ -n "$pid" ]; then | |
| sudo kill -9 $pid | |
| fi | |
| # Delete any existing routes for the IP address | |
| sudo ip route del "$ip_address" 2>/dev/null | |
| # Fetch all non-loopback, active network interfaces | |
| interfaces=$(ip -o link show up | awk -F': ' '{print $2}' | grep -v '^lo$') | |
| for iface in $interfaces; do | |
| metric=$(ip route show dev "$iface" | awk '/metric/ {print $NF; exit}') | |
| # Check if metric is a number and >= 100 | |
| if [[ "$metric" =~ ^[0-9]+$ ]] && (( metric >= 100 )); then | |
| # Retrieve the gateway for the interface | |
| gateway=$(ip route show default dev "$iface" | awk '/default/ {print $3; exit}') | |
| # If a gateway is found, add the route and exit the loop | |
| if [[ -n "$gateway" ]]; then | |
| echo "Adding route for $ip_address via gateway $gateway on interface $iface" | |
| sudo ip route add "$ip_address" via "$gateway" dev "$iface" | |
| break | |
| fi | |
| fi | |
| done | |
| # Create a temporary SSH_ASKPASS script if password is provided | |
| if [ -n "$pass" ]; then | |
| cat >"$sudo_pass_file" <<EOF | |
| #!/bin/bash | |
| echo "$pass" | |
| EOF | |
| chmod 700 "$sudo_pass_file" | |
| export SSH_ASKPASS="$sudo_pass_file" | |
| export DISPLAY=':0' # SSH requires DISPLAY to be set for SSH_ASKPASS | |
| else | |
| unset SSH_ASKPASS | |
| unset DISPLAY | |
| fi | |
| echo "Use this SOCKS proxy: socks5://127.0.0.1:$proxy_port/" | |
| # Start the SSH session with the specified source IP | |
| # We prevent SSH from accessing the TTY to force it to use SSH_ASKPASS | |
| setsid --fork ssh \ | |
| -o ExitOnForwardFailure=yes \ | |
| -o ServerAliveInterval=60 \ | |
| -o ServerAliveCountMax=3 \ | |
| -ND "$proxy_port" \ | |
| "$user@$ip_address" \ | |
| -p "$ssh_port" \ | |
| </dev/null | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment