Skip to content

Instantly share code, notes, and snippets.

@2600box
Forked from n-st/snmp-tunnel.sh
Created September 18, 2019 01:44
Show Gist options
  • Save 2600box/93ca4eba952721107f76fb00578da406 to your computer and use it in GitHub Desktop.
Save 2600box/93ca4eba952721107f76fb00578da406 to your computer and use it in GitHub Desktop.
Forward SNMP requests over a SSH connection (e.g. for use with Observium or other monitoring tools).
#!/bin/bash
# This script will make a local SNMP server accessible on a remote system by
# forwarding its UDP traffic over an SSH connection.
# To do this, the following port forwardings are applied (in this order):
# - local TCP port 42061 (arbitrary) to local UDP port 161 (SNMP)
# - remote TCP port 42061 to local TCP port 42061 (via SSH connection)
# - remote UDP port 42061 to remote TCP port 42061
# The remote UDP port can differ from the TCP port used for forwarding, as can
# the TCP ports used locally and remotely, as long as they are connected via
# the SSH connection.
socat tcp4-listen:42061,reuseaddr,fork UDP:localhost:161 &
autossh -R 42061:localhost:42061 omega "fuser -k -n udp 42061; socat -T15 udp4-recvfrom:42061,reuseaddr,fork,bind=127.0.0.1 tcp:localhost:42061"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment