概要
Linux システム管理者は通常、パスワードを入力するか、キーベースの認証を使用して Linux サーバーにログインします。 sshpass は、コマンド プロンプトにパスワードを自動的に入力できるようにするツールで、ユーザーが必要に応じて自動化されたスクリプトを実行できるようにします。 sshpass は、専用の tty を使用して ssh プロンプトにパスワードを提供し、ssh を欺いて、対話型のユーザーがパスワードを提供していると信じ込ませます。
sshpass の一般的な用途の一部
1. リモート サーバーへのバックアップの作成
2.指定された時間にシステム上でコマンドを実行する
sshpass のインストール
1. Centos ベースのディストリビューション:
https://fedoraproject.org/wiki/EPEL から EPEL リポジトリをセットアップし、root として実行します:
# yum -y install sshpass
2. Ubuntu/Debain ベースのディストリビューション:
root として実行:
# apt-get install sshpass
3. ソースからコンパイルしてインストールします:
# wget http://sourceforge.net/projects/sshpass/files/latest/download -O sshpass.tar.gz # tar -zxvf sshpass.tar.gz # cd sshpass-1.05/ # ./configure # make # make install # which sshpass /usr/local/bin/sshpass
助けを求める
# 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
-f、-d、-p、または -e のいずれかを使用する必要があります
例 1:ssh でパスワードを提供する
# sshpass -p 'password' ssh ldap.thegeekdiary.com -l root -o StrictHostKeyChecking=no
場所:
パスワード サーバーのパスワード (ldap.thegeekdiary.com) です。 「StrictHostKeyChecking=いいえ ‘ は、ホスト キーが不明または変更されたマシンへのログインを制御するために使用されます。
例:2 リモート サーバー VIZ でコマンドを実行して、稼働時間と UNAME を確認する
例 2:リモート サーバーでコマンドを実行するには
sshpass コマンドを使用して、リモート サーバーで 2 つのコマンド「uptime」と「uname」を実行してみましょう:
# sshpass -p 'password' ssh ldap.thegeekdiary.com -l root -o StrictHostKeyChecking=no "uptime;uname -a"
サンプル出力:
18:49:34 up 21 days, 18:49, 3 users, load average: 0.01, 0.00, 0.00 Linux ldap.thegeekdiary.com 2.6.32-220.el6.x86_64 #1 SMP Tue Dec 6 19:48:22 GMT 2011 x86_64 x86_64 x86_64 GNU/Linux
例 3:rsync を使用してサーバーにファイルをコピーする
この例では、ファイル sshpass.tar.gz をリモート サーバー ldap.thegeekdiary.com にコピーしています。
# sshpass -p 'password' rsync -av --progress sshpass.tar.gz [email protected]:/tmp/
上記のコマンドの出力:
sending incremental file list sshpass.tar.gz 98362 100% 62.56MB/s 0:00:00 (xfer#1, to-check=0/1) sent 98472 bytes received 31 bytes 197006.00 bytes/sec total size is 98362 speedup is 1.00 root@server1:/home/thegeekdiary#
例 4:リモート サーバーにコピーするための for ループ
次のようにファイルを作成します:
# touch /tmp/scr
ファイルには、ホストの名前が含まれている必要があります:
server2.thegeekdiary.com server3.thegeekdiary.com server4.thegeekdiary.com server5.thegeekdiary.com
# for i in `cat /tmp/scr`; do echo " ";echo "###$i####"; sshpass -p 'password' rsync -av --progress sshpass.tar.gz root@$i:/tmp/; done
上記のコマンドの出力:
###server2.thegeekdiary.com#### sending incremental file list sent 54 bytes received 12 bytes 132.00 bytes/sec total size is 98362 speedup is 1490.33 ###server3.thegeekdiary.com#### sending incremental file list sent 54 bytes received 12 bytes 44.00 bytes/sec total size is 98362 speedup is 1490.33 ###server4.thegeekdiary.com#### sending incremental file list sent 54 bytes received 12 bytes 132.00 bytes/sec total size is 98362 speedup is 1490.33 ###server5.thegeekdiary.com#### sending incremental file list sent 54 bytes received 12 bytes 132.00 bytes/sec total size is 98362 speedup is 1490.33