PORT=9111 until ssh -R $PORT:localhost:23 user@domain.com '(sleep 7200 ; exit)' do PORT=$((PORT+1)) if $PORT >= 65535 then PORT=1025 fi done