`
hongtoushizi
  • 浏览: 360766 次
  • 性别: Icon_minigender_1
  • 来自: 天津
社区版块
存档分类
最新评论

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

    博客分类:
  • mac
阅读更多

转载:  http://tinyhema.iteye.com/blog/2093795

 

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


安装sshpass 

试图使用homebrew安装 

Shell代码  收藏代码
  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强制安装 

Shell代码  收藏代码
  1. brew install https://raw.github.com/eugeneoden/homebrew/eca9de1/Library/Formula/sshpass.rb  


成功了。。。 

编译安装 

Shell代码  收藏代码
  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路径可以去掉,这样就会安装到默认目录中。 

使用方式 

Java代码  收藏代码
  1. sshpass -p 'ssh_password' ssh xxx.xxx.xxx.xxx  


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

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

Shell代码  收藏代码
  1. #!/bin/bash  
  2. cat <<MENU  
  3.     a   =>  10.101.81.238  
  4.     10.101.81.238   =>  10.101.81.238  
  5.     b   =>  192.168.4.151  
  6.     192.168.4.151   =>  192.168.4.151  
  7.     c   =>  192.168.4.2  
  8.     192.168.4.2     =>  192.168.4.2  
  9.   
  10. >>> 请输入ip或序号 <<<  
  11. MENU  
  12.     echo -n "Your choose:"  
  13.     read host  
  14.     case "$host" in  
  15.         a|10.101.81.238)  
  16.             exec /usr/local/bin/sshpass -p 123456  ssh root@10.101.81.238 -p22  
  17.             ;;  
  18.         b|192.168.4.151)  
  19.             exec /usr/local/bin/sshpass -p 'sdfsdf'  ssh root@192.168.4.151 -p22  
  20.             ;;  
  21.         c|192.168.4.2)  
  22.             exec /usr/local/bin/sshpass -p 'wfssfs'  ssh root@192.168.4.2 -p22  
  23.             ;;  
  24.         *)  
  25.         echo "Error, No host"  
  26.         ;;  
  27.     esac  



使用方法 

Shell代码  收藏代码
  1. $ sshp  
  2.     a   =>  10.101.81.238  
  3.     10.101.81.238   =>  10.101.81.238  
  4.     b   =>  192.168.4.151  
  5.     192.168.4.151   =>  192.168.4.151  
  6.     c   =>  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,内容如下。 

Shell代码  收藏代码
  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.     echo "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.     echo "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  



使用方法 

Shell代码  收藏代码
  1. sshp host  



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

Shell代码  收藏代码
  1. 10.101.81.238 123456  
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics