由於有一些場景不能使用ssh私鑰來實現免登,因此需要想其它辦法解決一下這個問題。
安裝sshpass
試圖使用homebrew安裝
- $ brew install sshpass
- Error: No available formula for sshpass
- We won't add sshpass because it makes it too easy for novice SSH users to
- ruin SSH's security.
這個萌賣的好。。。。
使用homebrew強制安裝
- brew install https://raw.github.com/eugeneoden/homebrew/eca9de1/Library/Formula/sshpass.rb
成功了。。。
編譯安裝
- wget http://sourceforge.net/projects/sshpass/files/sshpass/1.05/sshpass-1.05.tar.gz
- tar xvzf sshpass-1.05.tar.gz
- ./configure --prefix=/usr/local/Cellar/sshpass/1.05
- make
- sudo make install
編譯安裝的步驟是從brew的步驟中copy出來的,絕對可行。其中./configure 后面的prefix路徑可以去掉,這樣就會安裝到默認目錄中。
使用方式
- sshpass -p 'ssh_password' ssh xxx.xxx.xxx.xxx
可以看到這種方式其實還是要在命令里指定host+密碼登錄,還是不夠方便。期待的方式是只需要指定host即可,密碼神馬的,能自己處理。
簡單的使用方式
寫一個腳本,記錄一些常用的用戶名與密碼,通過簡單的選擇即可完成ssh登錄。
創建一個文件sshp。
- #!/bin/bash
- cat <<MENU
- a => 10.101.81.238
- 10.101.81.238 => 10.101.81.238
- b => 192.168.4.151
- 192.168.4.151 => 192.168.4.151
- c => 192.168.4.2
- 192.168.4.2 => 192.168.4.2
- >>> 請輸入ip或序號 <<<
- MENU
- echo -n "Your choose:"
- read host
- case "$host" in
- a|10.101.81.238)
- exec /usr/local/bin/sshpass -p 123456 ssh root@10.101.81.238 -p22
- ;;
- b|192.168.4.151)
- exec /usr/local/bin/sshpass -p 'sdfsdf' ssh root@192.168.4.151 -p22
- ;;
- c|192.168.4.2)
- exec /usr/local/bin/sshpass -p 'wfssfs' ssh root@192.168.4.2 -p22
- ;;
- *)
- echo "Error, No host"
- ;;
- esac
使用方法
- $ sshp
- a => 10.101.81.238
- 10.101.81.238 => 10.101.81.238
- b => 192.168.4.151
- 192.168.4.151 => 192.168.4.151
- c => 192.168.4.2
- 192.168.4.2 => 192.168.4.2
- >>> 請輸入ip或序號 <<<
- Your choose:a
- # ssh login success
可以看到,相比第一種方法,這種模式要簡單很多。
更簡單的使用方法,應該是只需要輸入 sshp host,就可以完成ssh登錄。
更簡單的使用方式
使用一個文件存儲host、password對,自行根據host匹配密碼,並登錄。
創建一個腳本,名為sshp,內容如下。
- #!/bin/bash
- RC_ERR_NO_HOST=11
- RC_ERR_NO_PASSWORD=21
- RC_SUCCESS=0
- pass_path=~/.ssh/sshp_pass
- host=$1
- # arguments
- if [ -z $host ]; then
- echo "ERR_NO_HOST, please input host."
- exit $RC_ERR_NO_HOST
- fi
- # read file
- pwd=`grep $host\ $pass_path | cut -d' ' -f 2`
- if [ -z $pwd ]; then
- echo "ERR_NO_PASSWORD, please record password first. file path $pass_path"
- exit $RC_ERR_NO_PASSWORD
- fi
- exec sshpass -p $pwd ssh root@$host -p22
- exit $RC_SUCCESS
使用方法
- sshp host
創建一個文件 ~/.ssh/sshp_pass,存放 host 與密碼數據,格式為"host password"。
例如
- 10.101.81.238 123456
除了可以動態指定host以外,還可以實現 用戶名、端口的自定義,暫時沒有需求,就這樣吧。^_^