シェルスクリプトを介してプログラムでパスワードプロンプトに応答する

シェルスクリプトを介してプログラムでパスワードプロンプトに応答する

プログラムでパスワードプロンプトに応答したいと思います。ただプロンプトが表示されたら、シェルスクリプトを実行します。これをこのように自動化することは可能ですか?私は他のツールを完全に避けたいです。つまり、ないかexpect類似のツールを使用してこれを達成するために、単にシェルスクリプトを使用したいと思います。

ベストアンサー1

指定しなかったのでどのシェル、ちょうどだまされます。次の情報を見つけることができます。grml.org~のzsh 恋人コマンドリファレンスmanページ。

  $ zmodload zsh/zpty
  $ zpty PW passwd $1
  $ zpty PW passwd $1
# ``-r'': read the output of the command name.
# ``z'' : Parameter
  $ zpty -r PW z '*password:'
# send the to command name the given strings as input
  $ zpty -w PW $2
  $ zpty -r PW z '*password:'
  $ zpty -w PW $2
# The second form, with the -d option, is used to delete commands
# previously started, by supplying a list of their names. If no names
# are given, all commands are deleted. Deleting a command causes the HUP
# signal to be sent to the corresponding process.
  $ zpty -d PW

私が自分でやったこともあります。私の端末からコピーして貼り付けた内容は次のとおりです。

mikeserv@localhost ~ % :                  
sudo zsh -c '
zmodload zsh/zpty
userdel dummy
useradd -m dummy
zpty dummyadd "passwd dummy"
zpty -r dummyadd nl "*UNIX password:"
zpty -w dummyadd "dummypw^M"
zpty -r dummyadd nl "*UNIX password:"
zpty -w dummyadd "dummypw^M"
zpty -r dummyadd nl "*"
printf %s\\n "$nl"
zpty -d dummyadd
'
userdel: user 'dummy' does not exist
useradd: warning: the home directory already exists.
Not copying any file from skel directory into it.

mikeserv@localhost ~ % ssh dummy@localhost
dummy@localhost's password: 
dummy@localhost ~ % 

数回試しましたが、その理由は次のとおりです。ホームディレクトリはすでにあります。ただし、コマンドを実行したときにユーザーが存在せず、それを設定するためのセッションを作成したことがわかります。パスワード。かなり愚かなスクリプトですが、終わったらログインできます。

だから可能だと思います。おそらく実現可能しかし、目的に応じて変わることがあります。

おすすめ記事