#!/bin/bash #V0.1 #Sample call #cat ~/.ssh/known_hosts | known_hosts_merge | known2config >~/.ssh/config.addme SpitHostRecord () { WantProxyHost='true' LongestHostname='' LongestObject='' for OneHost in $1 ; do if [ ${#OneHost} -gt ${#LongestObject} ]; then LongestObject="$OneHost" fi if ! echo "$OneHost" | egrep -q '(^[0-9][0-9\.]*$|^[0-9a-fA-F:][0-9a-fA-F:]*$)' ; then if [ ${#OneHost} -gt ${#LongestHostname} ]; then LongestHostname="$OneHost" fi fi if [ "$OneHost" = "$ProxyHost" ]; then WantProxyHost='false' #We don't want a ProxyCommand line for the gateway host itself fi done if [ -z "$LongestHostname" ]; then LongestHostname="$LongestObject" fi #echo "SHR/$1/$2/------$LongestHostname" >&2 cat <&2 exit fi ;; *) echo "Unrecognized param $1, exiting." >&2 exit ;; esac shift done echo echo '======== Beginning of hosts generated from known_hosts' HostListForOnePort='' PreviousPort='' while read HostField KeyInfo ; do #echo "Debug: $HostField ... $KeyInfo" >&2 for ComplexHost in `echo "$HostField" | tr ',' ' '` ; do if echo "$ComplexHost" | grep -q '^\[.*\]:[0-9][0-9]*$' ; then Port=`echo "$ComplexHost" | sed -e 's/.*://'` Host=`echo "$ComplexHost" | sed -e 's/^\[//' -e 's/\].*//'` #echo "Debug: Host is $Host, Port is $Port" elif echo "$ComplexHost" | grep -q ':' ; then echo "Unhandled host with colon, exiting." >&2 exit 1 elif echo "$ComplexHost" | grep -q '^[a-zA-Z0-9][a-zA-Z0-9\._-]*$' ; then Host="$ComplexHost" Port='22' else echo "Unhandled host $ComplexHost, exiting." >&2 exit 1 fi #If we've changed ports, push out the config record for the old port before resetting and continuing with the new port if [ -n "$PreviousPort" -a "$Port" != "$PreviousPort" ]; then SpitHostRecord "$HostListForOnePort" "$PreviousPort" HostListForOnePort='' PreviousPort='' fi HostListForOnePort="$HostListForOnePort $Host" PreviousPort="$Port" done #OK, we've gotten to the end of the hosts for this key, push out the config record for the remainder SpitHostRecord "$HostListForOnePort" "$Port" HostListForOnePort='' PreviousPort='' done echo echo '======== End of hosts generated from known_hosts' echo