ssh 通过sshpass自动登录远程主机

OpenSSH 自带的 ssh 客户端程序(也就是 'ssh' 命令)默认不允许你以非交互的方式传递密码

sshpass: 用于非交互的 ssh 密码验证

>>通过输入项控制登录主机

#!/bin/sh
echo "Through sshpass automatic login"
echo "please enter host suffix "
while true 
do
        echo -n "'host suffix':"
        read host_suffix
        case $host_suffix in
                70)
                sshpass -p "password" ssh huzorro@host -p 22
                break;;
                71)
                sshpass -p "password" ssh huzorro@host -p 22
                break;;
                90)
                sshpass -p "password" ssh huzorro@host -p 22
                break;;
                91)
                sshpass -p "password" ssh huzorro@host -p 22
                break;;
                85)
                sshpass -p "password" ssh huzorro@host -p 22
                break;;
                28)
                sshpass -p "password" ssh huzorro@host -p 22
                break;;
                14)
                sshpass -p "password" ssh huzorro@host -p 22
                break;;
                145)
                sshpass -p "password" ssh huzorro@host -p 10000
                break;;
                exit|quit)
                exit 0;;
                *)
                echo "Please enter the correct host suffix"
                echo "enter 'exit' or 'quit'  exit the script" 
                continue;;
        esac
done
exit 0

[huzorro@CM5 ~]$ trlogin.sh 
Through sshpass automatic login
please enter host suffix 
'host suffix':70

>>通过选单控制登录主机

#!/bin/sh
echo "Through sshpass automatic login"
echo "please enter host suffix "
select host_suffix in '70' '71' '90' '91' '85' '28' '14' '145' 'exit'
do
        case $host_suffix in
                70)
                sshpass -p "password" ssh huzorro@host -p 22
                break;;
                71)
                sshpass -p "password" ssh huzorro@host -p 22
                break;;
                90)
                sshpass -p "password" ssh huzorro@host -p 22
                break;;
                91)
                sshpass -p "password" ssh huzorro@host -p 22
                break;;
                85)
                sshpass -p "password" ssh huzorro@host -p 22
                break;;
                28)
                sshpass -p "password" ssh huzorro@host -p 22
                break;;
                14)
                sshpass -p "password" ssh huzorro@host -p 22
                break;;
                145)
                sshpass -p "password" ssh huzorro@host -p 10000
                break;;
                exit|quit)
                exit 0;;
                *)
                echo "Please enter the correct host suffix"
                echo "enter 'exit' or 'quit'  exit the script" 
                ;;
        esac
done
exit 0

[huzorro@CM5 ~]$ mlogin.sh 
Through sshpass automatic login
please enter host suffix 
1) 70	 3) 90	  5) 85	   7) 14    9) exit
2) 71	 4) 91	  6) 28	   8) 145
#? 1


你可能感兴趣的:(linux,shell,ssh,sshpass)