#!/bin/ash
#
#
#
 clear
 echo ""
 read -p " Hostname (e.g. domain.com)?: " HOST
 XHOST=X$HOST
 if [ $XHOST = X ]; then
   HOST=localhost
   else
   HOST=$HOST
 fi
 echo " [$HOST]"
 echo ""
 read -p " Port (just press Enter if 22)?: " PORT
 XPORT=X$PORT
 if [ $XPORT = X ]; then
   PORT=22
   else
   PORT=$PORT
 fi
 echo " [$PORT]"
 echo ""
 read -p " Username (just press Enter if root)?: " USER
 XUSER=X$USER
 if [ $XUSER = X ]; then
   USER=root
   else
   USER=$USER
 fi
 echo " [$USER]" && sleep 1
 clear
 echo ""
 echo " Testing port $PORT, please wait..."
 pscan -p $PORT -P $PORT $HOST > /tmp/ftptest.tmp1 2>&1
 cat /tmp/ftptest.tmp1 | grep '1 open' > /tmp/ftptest.tmp2
 if [ ! -s /tmp/ftptest.tmp2 ]; then
   echo ""
   echo " Ooops!"
   echo " Port $PORT at $HOST is not responding."
   echo ""
   rm /tmp/ftptest.tmp*
   exit
   else
   echo -n  " OK."
   sleep 1
   rm /tmp/ftptest.tmp*
 fi
 echo ""
 echo ""
 echo " Testing SSH server, please wait..."
 psftp -batch -v -P $PORT $HOST > /tmp/ftptest.tmp1 2>&1 &
 for i in 1 2 3 4 5 6 7 8 9; do
   if [ ! -s /tmp/ftptest.tmp2 ]; then
     sleep 0.5
     cat /tmp/ftptest.tmp1 | grep 'fingerprint is' > /tmp/ftptest.tmp2
   fi
 done
 killall psftp 2>/dev/null
 if [ ! -s /tmp/ftptest.tmp2 ]; then
   echo ""
   echo " Ooops!"
   echo " SSH server at $HOST:$PORT is not responding."
   echo ""
   rm /tmp/ftptest.tmp*
   exit
   else
   echo -n  " OK."
   sleep 1
   rm /tmp/ftptest.tmp*
 fi
 clear
 echo ""
 echo " Midnight Commander will try to connect now."
 echo " Please wait till the connection is established."
 echo ""
 read -p " Press Enter to continue..."
 clear
 LOCAL=$HOME
 if [ -e /tmp/local ]; then
   LOCAL=$(cat /tmp/local)
   rm /tmp/local
 fi
 mc $LOCAL sftp://$USER@$HOST:$PORT