LoginSignup
13

More than 5 years have passed since last update.

【Mac】ssh接続で毎回パスワードを求められるのでaliasとsshpassを使って解決

Last updated at Posted at 2016-01-06

社内サーバで一部、鍵認証ではなくパスワード認証のサーバがあり、毎回パスワードを探してコピペするのが面倒だったので、簡単にできないか探してみました。

※ macユーザ対象に書いてます。

ssh接続で毎回パスワードを求められるのでaliasとsshpassを使って解決

sshする際にパスワードを指定できないかなと探してみたら、sshpassなるものがありました!

$ sshpass -p 'password' ssh username@xxx.xxx.xxx.xxx

これでパスワードを指定して認証できるんです。

ただ、このままだと結局パスワードを探してコピペしなければいけないので、aliasに登録するのが今回の記事になります。

1. sshpassをHomebrewを使ってインストール

※ Homebrewをインストールしてない人は下記等を参考にインストールしてください。

パッケージ管理システム Homebrew
Homebrewまとめ
MacにHomebrewをインストールする

1-1. brew install sshpass

「sshpass brew install」等で調べると下記のコマンドが出てきましたが、残念ながらエラーになりインストールができませんでした。

$ brew install https://raw.github.com/eugeneoden/homebrew/eca9de1/Library/Formula/sshpass.rb
######################################################################## 100.0%
==> Downloading http://sourceforge.net/projects/sshpass/files/sshpass/1.05/sshpass-1.05.tar.gz
==> Downloading from http://jaist.dl.sourceforge.net/project/sshpass/sshpass/1.05/sshpass-1.05.tar
######################################################################## 100.0%
Error: MD5 support has been dropped for security reasons.
Please switch this formula to SHA256.

1-2. エラー解消

エラーの解消方法を調べてみると
https://gist.github.com/arunoda/7790979
brew_install_sshpassのエラー解消
MD5に対応したものを作成してくれてました!!!

こちらのsshpassでインストールできました!
kadwanevさんありがとうございます。

$ brew install https://raw.githubusercontent.com/kadwanev/bigboybrew/master/Library/Formula/sshpass.rb
######################################################################## 100.0%
==> Downloading http://sourceforge.net/projects/sshpass/files/sshpass/1.05/sshpass-1.05.tar.gz
Already downloaded: /Library/Caches/Homebrew/sshpass-1.05.tar.gz
==> ./configure --prefix=/usr/local/Cellar/sshpass/1.05
==> make install
🍺  /usr/local/Cellar/sshpass/1.05: 8 files, 39.2K, built in 9 seconds
$
$ sshpass -V
sshpass 1.05 (C) 2006-2011 Lingnu Open Source Consulting Ltd.
This program is free software, and can be distributed under the terms of the GPL
See the COPYING file for more information.

2. aliasにsshコマンドを登録

※ zshの方は、zshに置き換えて読んでもらえればと思います。

bashのaliasにsshpassを使ったssh接続コマンドを記載しましょう!

2-1. .bashrcにaliasを記載

$ vi ~/.bashrc
# ~/.bashrc

alias hogehoge='sshpass -p 'password' ssh username@xxx.xxx.xxx.xxx'

2-2. bashを更新してaliasを即反映

$ source ~/.bashrc
$ alias
alias hogehoge='sshpass -p password ssh username@xxx.xxx.xxx.xxx'
$ hogehoge
Last login: Wed Jan  6 12:08:08 2016 from 192.168.21.99

パスワード認証なしで簡単にsshできました!

少しでも参考になれば幸いです。
以上!「【Mac】ssh接続で毎回パスワードを求められるのをaliasとsshpassを使って解決」でした!

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
13