#!/bin/bash target_host=$1 if [ ! -z "$2" ];then target_port=$2 else target_port="22" fi while read record;do if [ ! -z "$record" ];then host=$(echo $record | awk '{print $NF}') port=$(echo $record | awk '{print $(NF-1)}') if timeout 1 bash -c "cat < /dev/null > /dev/tcp/$host/$port" &> /dev/null;then target_host=$host target_port=$port break fi fi done < <(dig +short SRV _ssh._tcp.$target_host @$(dig +short NS $(echo $target_host | awk -F '.' '{print $(NF-1)"."$NF}') | head -n1) | sort -nk1) socat STDIO TCP:$target_host:$target_port 2> /dev/null