シェルスクリプトのコマンドに回答を追加する方法を知りたいです。これはどう説明したらいいかわからないので、あまり明確ではないことは承知しています。例えば:
$> su
$> Password: <automatically_fill_in_the_password_here>
パスワードを自動的に入力するにはどうすればよいですか?
シェルスクリプトのコマンドに回答を追加する方法を知りたいです。これはどう説明したらいいかわからないので、あまり明確ではないことは承知しています。例えば:
$> su
$> Password: <automatically_fill_in_the_password_here>
パスワードを自動的に入力するにはどうすればよいですか?
expect
あなたが探しているものです。
このページを引用させてください。非常によく説明されていexpect
ます。
Expectは、Unix および Linux の自動化およびテスト ツールです。
telnet
、ftp
、passwd
、fsck
、rlogin
、tip
、ssh
および他の多くのインタラクティブなアプリケーションで動作します。Unix 疑似端末を使用してサブプロセスを透過的にラップし、端末経由でアクセスされる任意のアプリケーションの自動化を可能にします。
以下は、リモート ssh サーバーに OpenSSH ルート/管理者パスワードを提供し、Unix / Linux / BSD コマンドを実行するための単純な予期スクリプトです。(まず、次の手順に従って、expect ツールをインストールする必要があります。)
#!/usr/bin/expect -f
# Expect script to supply root/admin password for remote ssh server
# and execute command.
# This script needs three argument to(s) connect to remote server:
# password = Password of remote UNIX server, for root user.
# ipaddr = IP Addreess of remote UNIX server, no hostname
# scriptname = Path to remote script which will execute on remote server
# For example:
# ./sshlogin.exp password 192.168.1.11 who
# ------------------------------------------------------------------------
# Copyright (c) 2004 nixCraft project <http://cyberciti.biz/fb/>
# This script is licensed under GNU GPL version 2.0 or above
# -------------------------------------------------------------------------
# This script is part of nixCraft shell script collection (NSSC)
# Visit http://bash.cyberciti.biz/ for more information.
# ----------------------------------------------------------------------
# set Variables
set password [lrange $argv 0 0]
set ipaddr [lrange $argv 1 1]
set scriptname [lrange $argv 2 2]
set arg1 [lrange $argv 3 3]
set timeout -1
# now connect to remote UNIX box (ipaddr) with given script to execute
spawn ssh root@$ipaddr $scriptname $arg1
match_max 100000
# Look for passwod prompt
expect "*?assword:*"
# Send password aka $password
send -- "$password\r"
# send blank line (\r) to make sure we get back to gui
send -- "\r"
expect eof
スクリプトのコメントを読まなかった場合 (読むべきです)、使用方法は次のとおりです。
./sshlogin.exp password 192.168.1.11 who