由于有一些场景不能使用ssh私钥来实现免登,因此需要想其它办法解决一下这个问题。 


安装sshpass 

试图使用homebrew安装 



mac下使用sshpass实现ssh记住密码_sshpass



1. $ brew install sshpass  
2. Error: No available formula for sshpass  
3. We won't add sshpass because it makes it too easy for novice SSH users to  
4. ruin SSH's security.



这个萌卖的好。。。。 

使用homebrew强制安装 



mac下使用sshpass实现ssh记住密码_sshpass



    1. brew install https://raw.github.com/eugeneoden/homebrew/eca9de1/Library/Formula/sshpass.rb


    成功了。。。 

    编译安装 



    mac下使用sshpass实现ssh记住密码_sshpass



    1. wget http://sourceforge.net/projects/sshpass/files/sshpass/1.05/sshpass-1.05.tar.gz  
    2. tar xvzf sshpass-1.05.tar.gz  
    3. ./configure --prefix=/usr/local/Cellar/sshpass/1.05
    4. make  
    5. sudo make install



    编译安装的步骤是从brew的步骤中copy出来的,绝对可行。其中./configure 后面的prefix路径可以去掉,这样就会安装到默认目录中。 

    使用方式 



    1. sshpass -p 'ssh_password'



    可以看到这种方式其实还是要在命令里指定host+密码登录,还是不够方便。期待的方式是只需要指定host即可,密码神马的,能自己处理。 

    简单的使用方式 
    写一个脚本,记录一些常用的用户名与密码,通过简单的选择即可完成ssh登录。 
    创建一个文件sshp。 



    mac下使用sshpass实现ssh记住密码_sshpass

    1. #!/bin/bash  
    2. cat <<MENU  
    3. 10.101.81.238
    4. 10.101.81.238   =>  10.101.81.238
    5. 192.168.4.151
    6. 192.168.4.151   =>  192.168.4.151
    7. 192.168.4.2
    8. 192.168.4.2     =>  192.168.4.2
    9.   
    10. >>> 请输入ip或序号 <<<  
    11. MENU  
    12. "Your choose:"
    13.     read host  
    14. "$host"
    15. 10.101.81.238)  
    16. 123456  ssh root@10.101.81.238
    17.             ;;  
    18. 192.168.4.151)  
    19. 'sdfsdf'  ssh root@192.168.4.151
    20.             ;;  
    21. 192.168.4.2)  
    22. 'wfssfs'  ssh root@192.168.4.2
    23.             ;;  
    24.         *)  
    25. "Error, No host"
    26.         ;;  
    27.     esac

    使用方法 



    mac下使用sshpass实现ssh记住密码_sshpass



    1. $ sshp  
    2. 10.101.81.238
    3. 10.101.81.238   =>  10.101.81.238
    4. 192.168.4.151
    5. 192.168.4.151   =>  192.168.4.151
    6. 192.168.4.2
    7. 192.168.4.2     =>  192.168.4.2
    8.   
    9. >>> 请输入ip或序号 <<<  
    10. Your choose:a  
    11. # ssh login success



    可以看到,相比第一种方法,这种模式要简单很多。 

    更简单的使用方法,应该是只需要输入 sshp host,就可以完成ssh登录。 

    更简单的使用方式 
    使用一个文件存储host、password对,自行根据host匹配密码,并登录。 

    创建一个脚本,名为sshp,内容如下。 



    mac下使用sshpass实现ssh记住密码_sshpass



      1. #!/bin/bash  
      2.   
      3. RC_ERR_NO_HOST=11
      4. RC_ERR_NO_PASSWORD=21
      5. RC_SUCCESS=0
      6.   
      7. pass_path=~/.ssh/sshp_pass  
      8.   
      9. host=$1
      10.   
      11. # arguments   
      12. if [ -z $host ]; then  
      13. "ERR_NO_HOST, please input host."
      14.     exit $RC_ERR_NO_HOST    
      15. fi  
      16.   
      17. # read file  
      18. pwd=`grep $host\  $pass_path | cut -d' ' -f 2`  
      19. if [ -z $pwd ]; then  
      20. "ERR_NO_PASSWORD, please record password first. file path $pass_path"
      21.     exit $RC_ERR_NO_PASSWORD  
      22. fi  
      23.   
      24. exec sshpass -p $pwd  ssh root@$host -p22  
      25. exit $RC_SUCCESS



      使用方法 




      1. sshp host  



      创建一个文件 ~/.ssh/sshp_pass,存放 host 与密码数据,格式为"host password"。 
      例如