Skip to content

Instantly share code, notes, and snippets.

@Lucho00Cuba
Created June 16, 2023 18:23
Show Gist options
  • Save Lucho00Cuba/76caaff27da773ea8759731522eef8d8 to your computer and use it in GitHub Desktop.
Save Lucho00Cuba/76caaff27da773ea8759731522eef8d8 to your computer and use it in GitHub Desktop.
Socat Control
#!/bin/bash
SOCKS_PID_FILE="/tmp/socat.pid"
start_socat() {
socat TCP4-LISTEN:1080,fork SOCKS4A:localhost:www.example.com:80,socksport=1080 &
echo $! > "$SOCKS_PID_FILE"
echo "socat started with PID $!"
}
stop_socat() {
if [[ -f "$SOCKS_PID_FILE" ]]; then
pid=$(cat "$SOCKS_PID_FILE")
kill "$pid"
rm "$SOCKS_PID_FILE"
echo "socat stopped (PID $pid)"
else
echo "socat is not running"
fi
}
status_socat() {
if [[ -f "$SOCKS_PID_FILE" ]]; then
pid=$(cat "$SOCKS_PID_FILE")
if ps -p "$pid" > /dev/null; then
echo "socat is running (PID $pid)"
else
echo "socat is not running"
rm "$SOCKS_PID_FILE"
fi
else
echo "socat is not running"
fi
}
case "$1" in
start)
start_socat
;;
stop)
stop_socat
;;
status)
status_socat
;;
*)
echo "Usage: $0 {start|stop|status}"
exit 1
;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment