前言

sshpass是ansible密码输入的必要条件,在Linux中使用yum install sshpass或者apt-get install sshpass都可以轻松安装,但在macOS新版本中由于安全原因无法直接使用brew install sshpass,需要采用其它安全的办法绕过。

使用sshpass的场景

在macOS下使用ansible命令(inventory文件中使用了密码验证的方式)或者使用iTerm2来完成自动密码填充等场景会使用到sshpass。
比如下面的样例:Inventory文件中使用了ansible_ssh_pass选项

  1. # 编辑inventory
  2. cat hosts
  3. 10.10.66.66 ansible_port=22 ansible_user=root ansible_ssh_pass=test666
  4. # 使用ansible命令会失败,提示缺少sshpass
  5. ansible all -i test.hosts -m ping
  6. 10.10.66.66 | FAILED! => {
  7. "failed": true,
  8. "msg": "to use the 'ssh' connection type with passwords, you must install the sshpass program"
  9. }

安装sshpass及各种常见小问题处理

直接brew install会提示不安全,被拒绝,brew install —force强制安装也不行

可以通过如下方法进行安装

  1. # 先将文件下载至本地,然后执行安装命令
  2. wget https://raw.githubusercontent.com/kadwanev/bigboybrew/master/Library/Formula/sshpass.rb
  3. # 执行如下命令虽然提示了一个错误,但是也顺利安装成功了
  4. brew install sshpass.rb
  5. Error: Failed to load cask: sshpass.rb
  6. Cask 'sshpass' is unreadable: wrong constant name #<Class:0x00007fb25716b2d0>
  7. Warning: Treating sshpass.rb as a formula.
  8. ==> Downloading http://sourceforge.net/projects/sshpass/files/sshpass/1.06/sshpass-1.06.tar.gz
  9. ==> Downloading from https://cfhcable.dl.sourceforge.net/project/sshpass/sshpass/1.06/sshpass-1.06.tar.gz
  10. ######################################################################## 100.0%
  11. Warning: A newer Command Line Tools release is available.
  12. Update them from Software Update in System Preferences or run:
  13. softwareupdate --all --install --force
  14. If that doesn't show you any updates, run:
  15. sudo rm -rf /Library/Developer/CommandLineTools
  16. sudo xcode-select --install
  17. Alternatively, manually download them from:
  18. https://developer.apple.com/download/all/.
  19. You should download the Command Line Tools for Xcode 13.0.
  20. ==> ./configure --prefix=/usr/local/Cellar/sshpass/1.06
  21. ==> make install
  22. 🍺 /usr/local/Cellar/sshpass/1.06: 8 files, 77.9KB, built in 13 seconds

可能会由于网络问题,导致无法直接下载sshpass.rb,所以贴上脚本源码

  1. cat >> sshpass.rb <<EOF
  2. require 'formula'
  3. class Sshpass < Formula
  4. url 'http://sourceforge.net/projects/sshpass/files/sshpass/1.06/sshpass-1.06.tar.gz'
  5. homepage 'http://sourceforge.net/projects/sshpass'
  6. sha256 'c6324fcee608b99a58f9870157dfa754837f8c48be3df0f5e2f3accf145dee60'
  7. def install
  8. system "./configure", "--disable-debug", "--disable-dependency-tracking",
  9. "--prefix=#{prefix}"
  10. system "make install"
  11. end
  12. def test
  13. system "sshpass"
  14. end
  15. end
  16. EOF

原文链接