mac下使用sshpass實現ssh記住密碼


由於有一些場景不能使用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  



除了可以動態指定host以外,還可以實現 用戶名、端口的自定義,暫時沒有需求,就這樣吧。^_^


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM