LoginSignup
20
9

More than 5 years have passed since last update.

macOS に Homebrew で sshpass をインストールする

Last updated at Posted at 2018-04-17

パスワード認証より公開鍵認証の方が望ましいことは言わずもがなであるが、世の中には色々と事情というものがあり、毎回 ssh 接続時にパスワードを手入力しないといけないような状況も存在する。

そのような場合に sshpass でパスワードを省略する方法が存在する。

sshpass

brew install sshpass
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.

Homebrew で sshpass をインストールしようとすると、「エラー:"sshpass" なんて名前のフォーミュラはない。素人が SSH のセキュリティを台無しにするので sshpass は追加しない。」とキレられる。

novice SSH users ではないことを証明し、目の前の課題を解決すべく、別の手段を検討する。

lalyos/sshpass.rb

brew install http://git.io/sshpass.rb

git.io でショート URL を生成しているすかした奴が居る。
もう色々アンチテーゼというかむしろ皮肉なんじゃないかと勘ぐりたくなるレベル。

賢明なる諸兄はすでにリダイレクト先のソースを確認していることと思うが、 http://git.io/sshpass.rb のリダイレクト先は https://gist.githubusercontent.com/lalyos/28b35c29d4f8d2c1f293/raw/sshpass.rb である。
Gist のページは https://gist.github.com/lalyos/28b35c29d4f8d2c1f293 になる。

ここで追加されるのは 2011-08-06 リリースの sshpass 1.05 になる。最新版は 2016-06-30 リリースの 1.06 なので注意されたい。

stefanoverna/sshpass.rb

https://gist.github.com/stefanoverna/1513663
Gist で sshpass.rb を検索すると、現存する最も古い投稿は 2011-12-23 の Stefano Verna (stefanoverna) の sshpass.rb のようだ。

M-Barnett/sshpass.rb

brew install https://gist.github.com/M-Barnett/1a22f49394f17364c1eaf39006e87c91/raw/2d95d1075772069fe2fa834194072055e3c50db1/sshpass.rb

https://gist.github.com/M-Barnett/1a22f49394f17364c1eaf39006e87c91
lalyos/sshpass.rb をフォークしてバージョンアップしている上記を利用するといいだろう。

その他の sshpass.rb

概ね大きな違いはなさそう。

自分でフォーミュラを作成する

brew create

brew create URL [--autotools|--cmake|--meson] [--no-fetch] [--set-name name] [--set-version version] [--tap user/repo]:
    Generate a formula for the downloadable file at URL and open it in the editor.
    Homebrew will attempt to automatically derive the formula name
    and version, but if it fails, you'll have to make your own template. The wget
    formula serves as a simple example. For the complete API have a look at
    http://www.rubydoc.info/github/Homebrew/brew/master/Formula.

    If --autotools is passed, create a basic template for an Autotools-style build.
    If --cmake is passed, create a basic template for a CMake-style build.
    If --meson is passed, create a basic template for a Meson-style build.

    If --no-fetch is passed, Homebrew will not download URL to the cache and
    will thus not add the SHA256 to the formula for you. It will also not check
    the GitHub API for GitHub projects (to fill out the description and homepage).

    The options --set-name and --set-version each take an argument and allow
    you to explicitly set the name and version of the package you are creating.

    The option --tap takes a tap as its argument and generates the formula in
    the specified tap.

brew create https://.../sshpass-1.06.tar.gz

brew create https://downloads.sourceforge.net/project/sshpass/sshpass/1.06/sshpass-1.06.tar.gz

sshpass の URL を指定して brew create を試してみる。

Error: sshpass is blacklisted for creation.
We won't add sshpass because it makes it too easy for novice SSH users to
ruin SSH's security.

If you really want to create this formula use --force.

ブラックリストに入っているから作れないと怒られる。
どうしても作りたいなら --force オプションを使えと。

brew create https://downloads.sourceforge.net/project/sshpass/sshpass/1.06/sshpass-1.06.tar.gz --set-name sshpass --set-version 1.06 --force

--force オプションと、ついでに名前とバージョンも渡してやると、Vim が起動する。

/usr/local/Homebrew/Library/Taps/homebrew/homebrew-core/Formula/sshpass.rb
# Documentation: https://docs.brew.sh/Formula-Cookbook
#                http://www.rubydoc.info/github/Homebrew/brew/master/Formula
# PLEASE REMOVE ALL GENERATED COMMENTS BEFORE SUBMITTING YOUR PULL REQUEST!
class Sshpass < Formula
  desc ""
  homepage ""
  url "https://downloads.sourceforge.net/project/sshpass/sshpass/1.06/sshpass-1.06.tar.gz"
  version "1.06"
  sha256 "c6324fcee608b99a58f9870157dfa754837f8c48be3df0f5e2f3accf145dee60"
  # depends_on "cmake" => :build

  def install
    # ENV.deparallelize  # if your formula fails when building in parallel
    # Remove unrecognized options if warned by configure
    system "./configure", "--disable-debug",
                          "--disable-dependency-tracking",
                          "--disable-silent-rules",
                          "--prefix=#{prefix}"
    # system "cmake", ".", *std_cmake_args
    system "make", "install" # if this fails, try separate make/make install steps
  end

  test do
    # `test do` will create, run in and delete a temporary directory.
    #
    # This test will fail and we won't accept that! For Homebrew/homebrew-core
    # this will need to be a test that verifies the functionality of the
    # software. Run the test with `brew test sshpass`. Options passed
    # to `brew install` such as `--HEAD` also need to be provided to `brew test`.
    #
    # The installed folder is not in the path, so use the entire path to any
    # executables being tested: `system "#{bin}/program", "do", "something"`.
    system "false"
  end
end

テンプレートが表示されるので deschomepage を指定する。
https://docs.brew.sh/Formula-Cookbook
http://www.rubydoc.info/github/Homebrew/brew/master/Formula

属性 内容
desc ソフトウェアの1行の説明。ソフトウェアと Homebrew のメンテナーの概要を知るために使用する。brew info 実行時に表示される。
homepage ソフトウェアのホームページ。例えばパッチ提出の連絡先としてソフトウェアと Homebrew メンテナーに関する詳細情報を入手するために使用する。brew home を実行すると開ける。
mirror Additional URLs for the #stable version of the formula. These are only used if the url fails to download. It's optional and there can be more than one. Generally we add them when the main url is unreliable. If url is really unreliable then we may swap the mirror and url.
sha256 To verify the #cached_download's integrity and security we verify the SHA-256 hash matches what we've declared in the Formula. To quickly fill this value you can leave it blank and run brew fetch –force and it'll tell you the currently valid value.
url The URL used to download the source for the #stable version of the formula. We prefer https for security and proxy reasons. If not inferrable, specify the download strategy with :using => …
version The version string for the #stable version of the formula. The version is autodetected from the URL and/or tag so only needs to be declared if it cannot be autodetected correctly.

sshpass のオプション

Usage: sshpass [-f|-d|-p|-e] [-hV] command parameters
   -f filename   Take password to use from file
   -d number     Use number as file descriptor for getting password
   -p password   Provide password as argument (security unwise)
   -e            Password is passed as env-var "SSHPASS"
   With no parameters - password will be taken from stdin

   -h            Show help (this screen)
   -V            Print version information
At most one of -f, -d, -p or -e should be used

-p オプションは履歴にパスワードが残るので望ましくないだろう。ヘルプにも "unwise" (賢くない)と書かれている。

環境変数 SSHPASS にパスワードを指定する -e オプションも ps コマンドで表示可能のようだ。1

sshpass と ssh の組合せ

sshpass -f ~/.ssh/parole ssh hostname
20
9
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
20
9