macOS 安装 sshpass

2020-10-30 11:53:19 +08:00
 wsgzao

前言

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

更新历史

2020 年 10 月 13 日 - 初稿

阅读原文 - https://wsgzao.github.io/post/sshpass/


使用 sshpass 的场景

在 macOS 下使用 ansible 命令(inventory 文件中使用了密码验证的方式)或者使用 iTerm2 来完成自动密码填充等场景会使用到 sshpass 。

比如下面的样例:Inventory 文件中使用了ansible_ssh_pass选项

# 编辑 inventory
cat hosts
10.10.66.66 ansible_port=22 ansible_user=root ansible_ssh_pass=test666

# 使用 ansible 命令会失败,提示缺少 sshpass
ansible all -i test.hosts  -m ping
10.10.66.66 | FAILED! => {
    "failed": true,
    "msg": "to use the 'ssh' connection type with passwords, you must install the sshpass program"
}

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

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

brew install sshpass -f
Updating Homebrew...
Error: No available formula with the name "sshpass"
We won't add sshpass because it makes it too easy for novice SSH users to
ruin SSH's security.


可以通过下面的命令进行安装

brew install https://raw.githubusercontent.com/kadwanev/bigboybrew/master/Library/Formula/sshpass.rb

Error: Calling Non-checksummed download of sshpass formula file from an arbitrary URL is disabled! Use 'brew extract' or 'brew create' and 'brew tap-new' to create a formula file in a tap on GitHub instead.

# 出现上述报错可以下载至本地再次执行安装
wget https://raw.githubusercontent.com/kadwanev/bigboybrew/master/Library/Formula/sshpass.rb
brew install sshpass.rb

sshpass.rb 文件内容

require 'formula'

class Sshpass < Formula
  url 'http://sourceforge.net/projects/sshpass/files/sshpass/1.06/sshpass-1.06.tar.gz'
  homepage 'http://sourceforge.net/projects/sshpass'
  sha256 'c6324fcee608b99a58f9870157dfa754837f8c48be3df0f5e2f3accf145dee60'

  def install
    system "./configure", "--disable-debug", "--disable-dependency-tracking",
                          "--prefix=#{prefix}"
    system "make install"
  end

  def test
    system "sshpass"
  end
end

如果(因为众所周知的网络原因)通过brew install https://raw.githubusercontent.com/kadwanev/bigboybrew/master/Library/Formula/sshpass.rb的方式不能正常工作,可以将上面 rubby 脚本sshpass.rb的内容拷贝到本地执行 brew install sshpass.rb

再次尝试安装提示缺少xcode-select命令行工具,可以通过xcode-select --install进行安装

brew install sshpass.rb
Error: Xcode alone is not sufficient on Mojave.
Install the Command Line Tools:
  xcode-select --install

xcode-select --install
xcode-select: note: install requested for command line developer tools

安装完xcode-select之后重新执行brew install命令,sshpass安装成功。

brew install sshpass.rb
Updating Homebrew...
==> Downloading http://sourceforge.net/projects/sshpass/files/sshpass/1.06/sshpass-1.06.tar.gz
==> Downloading from https://jaist.dl.sourceforge.net/project/sshpass/sshpass/1.06/sshpass-1.06.tar.g
######################################################################## 100.0%
==> ./configure --prefix=/usr/local/Cellar/sshpass/1.06
==> make install
🍺  /usr/local/Cellar/sshpass/1.06: 9 files, 41.6KB, built in 33 seconds

1710 次点击
所在节点    macOS
5 条回复
cz5424
2020-10-30 13:24:07 +08:00
感谢楼主分享,但是还是私钥登录比较香
justsosososo
2020-10-30 13:40:12 +08:00
CRT 不香么
Keyes
2020-10-30 13:46:23 +08:00
sshpss 这种魔鬼工具应该被全球 rm -rf
bluehtt
2020-10-30 13:47:54 +08:00
@justsosososo #2 不香。首先是付费软件,其次不能通过终端命令操作
tairan2006
2020-10-30 13:56:01 +08:00
一般 ssh-copy-id 不就够了

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/720109

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX