Skip to content

Instantly share code, notes, and snippets.

@jesussuarz
Created May 1, 2024 04:18
Show Gist options
  • Save jesussuarz/4e29438a55ea41061da34b8355d4a52c to your computer and use it in GitHub Desktop.
Save jesussuarz/4e29438a55ea41061da34b8355d4a52c to your computer and use it in GitHub Desktop.

The script provided is a Bash shell script designed to compute an SSH port number based on a specific set of rules applied to an input IP address. Here's a detailed description of how the script works and what it's designed to do:

Script Description:

The purpose of this Bash script is to dynamically calculate a port number for SSH connections based on a unique format derived from an IP address. The script takes a single IP address as an input argument and processes it to generate a custom SSH port. The custom port number is constructed following these rules:

  • The first digit of the port is always '6'.
  • The second digit is taken from the first digit of the first octet of the IP address.
  • The next three digits are composed of the last digit from each of the three remaining octets of the IP address.

This method ensures that each IP address is likely to correspond to a unique port number, facilitating distinct SSH configurations for different servers or devices identified by their IP addresses.

How It Works:

Input Validation: The script first checks if an IP address has been passed as an argument. If not, it prompts the user to provide one and exits if none is provided. IP Address Parsing: Using Bash's Internal Field Separator (IFS), the script splits the IP address into an array of four octets. Validation of IP Structure: It then checks if the IP address indeed contains exactly four octets. If the IP address format is incorrect, the script notifies the user and terminates.

Port Calculation:

The script constructs the port number by appending digits according to the predefined rules. It starts with '6', followed by the first digit of the first octet, and ends with the last digits of the second, third, and fourth octets. Output: Finally, the script outputs the calculated port number, indicating the custom SSH port that can be used for connecting to the server or device with the given IP address.

#!/bin/bash

# Validate that an IP address has been provided as an argument
if [ -z "$1" ]; then
  echo "Please provide an IP address as an argument."
  exit 1
fi

# Extract the octets from the IP address
IFS='.' read -ra ADDR <<< "$1"

# Ensure that the IP has exactly four octets
if [ ${#ADDR[@]} -ne 4 ]; then
  echo "The provided IP address is invalid."
  exit 1
fi

# Construct the port number
# The first number is always 6
# The second number is the first number of the first octet
# The next three numbers are the last number from each of the last three octets
port="6${ADDR[0]:0:1}${ADDR[1]: -1}${ADDR[2]: -1}${ADDR[3]: -1}"

# Display the port
echo "The calculated SSH port for IP $1 is: $port"

Usage:

To use the script:

  1. Save the script to a file, e.g., calculate_port.sh.
  2. Make the script executable by running chmod +x calculate_port.sh in the terminal.
  3. Execute the script by passing an IP address as a parameter, like so: ./calculate_port.sh 192.168.1.1.

This script is particularly useful for administrators who need to configure SSH access dynamically across multiple servers or networks, where each device might require a distinct port number based on its IP address to enhance security or manage network configurations efficiently.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment