2016年2月5日金曜日

[CentOS7] パスワードをコマンドラインで指定してSSH接続する


sshpass を使用すると、コマンドラインでパスワードを指定して SSH接続できます。

expect を使うより簡単です。

CentOS7 には、yum で sshpass をインストールできます。 まず、epel レポジトリをインストールします。

[root@node71 ~]# yum install epel-release

sshpass は以下のようにインストールします。

[root@node71 ~]# yum install sshpass

使い方は以下のとおり。
"-p" でパスワードを指定します。
下記例では root ユーザで実行していますが、一般ユーザでも同じように実行できます。

[root@node71 ~]# sshpass -p 'password' ssh user@10.1.0.21

パスワードは、環境変数やファイルでも指定できます。

[root@node71 ~]# sshpass -h
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