私は初心者ですが、この段階でいくつかのエクササイズを試しています。"expect"の基本的なエクササイズ - 初心者
以下は、2つの連続した「スポーン」コマンドを使用して、UNIXシステムのパスワードを変更して古いものを復元する簡単な方法です。基本的には、1を手動で何を自動化:
$passwd
Changing password for <username>.
(current) UNIX password:
Enter new UNIX password:
Retype new UNIX password:
passwd: password updated successfully
問題は、無限の謎に私を保つ..「期待」は不思議なプロンプトを待って二卵のエントリで停止していることです。
#!/usr/bin/expect -f
#PREAMBLE
set timeout -1
set force_conservative 1
set send_slow {1 .1}
if {$force_conservative} {
set send_slow {1 .1}
proc send {ignore arg} {
sleep .1
exp_send -s -- $arg
}
}
#ACTUAL CODE
spawn passwd
expect "(current)"
send -- "PW-OLD\r"
expect "Enter"
send -- "PW-NEW\r"
expect "Retype"
send -- "PW-NEW\r"
sleep 1
spawn passwd
expect "(current)"
send -- "PW-NEW"
expect "Enter"
send -- "PW-OLD\r"
expect "Retype"
send -- "PW-OLD\r"
interact
あなたが気づいた悪い習慣と、あなたが伝えたいかもしれない良い習慣を聞いて欲しいです。
以下は、ユーザー@pynexjの提案の後、expect -d
の出力(最初のシバン行でexpect -f
を置き換えます)です。これは実際のコーダーのデバッグユーティリティのように見えますが、私が気づいていなかったものです。
$./exercise1.sh
expect version 5.45
argv[0] = /usr/bin/expect argv[1] = -d argv[2] = ./exercise1.sh
set argc 0
set argv0 "./exercise1.sh"
set argv ""
executing commands from command file ./exercise1.sh
spawn passwd
parent: waiting for sync byte
parent: telling child to go ahead
parent: now unsynchronized from child
spawn: returns {7848}
expect: does "" (spawn_id exp4) match glob pattern "(current)"? no
Changing password for <username>.
(current) UNIX password:
expect: does "Changing password for <username>.\r\n(current) UNIX password: " (spawn_id exp4) match glob pattern "(current)"? yes
expect: set expect_out(0,string) "(current)"
expect: set expect_out(spawn_id) "exp4"
expect: set expect_out(buffer) "Changing password for <username>.\r\n(current)"
send: sending "PW-OLD\r" to { exp4 }
expect: does " UNIX password: " (spawn_id exp4) match glob pattern "Enter"? no
expect: does " UNIX password: \r\n" (spawn_id exp4) match glob pattern "Enter"? no
Enter new UNIX password:
expect: does " UNIX password: \r\nEnter new UNIX password: " (spawn_id exp4) match glob pattern "Enter"? yes
expect: set expect_out(0,string) "Enter"
expect: set expect_out(spawn_id) "exp4"
expect: set expect_out(buffer) " UNIX password: \r\nEnter"
send: sending "PW-NEW\r" to { exp4 }
expect: does " new UNIX password: " (spawn_id exp4) match glob pattern "Retype"? no
expect: does " new UNIX password: \r\n" (spawn_id exp4) match glob pattern "Retype"? no
Retype new UNIX password:
expect: does " new UNIX password: \r\nRetype new UNIX password: " (spawn_id exp4) match glob pattern "Retype"? yes
expect: set expect_out(0,string) "Retype"
expect: set expect_out(spawn_id) "exp4"
expect: set expect_out(buffer) " new UNIX password: \r\nRetype"
send: sending "PW-NEW\r" to { exp4 }
spawn passwd
parent: waiting for sync byte
parent: telling child to go ahead
parent: now unsynchronized from child
spawn: returns {7861}
expect: does "" (spawn_id exp7) match glob pattern "(current)"? no
Changing password for <username>.
(current) UNIX password:
expect: does "Changing password for <username>.\r\n(current) UNIX password: " (spawn_id exp7) match glob pattern "(current)"? yes
expect: set expect_out(0,string) "(current)"
expect: set expect_out(spawn_id) "exp7"
expect: set expect_out(buffer) "Changing password for <username>.\r\n(current)"
send: sending "PW-NEW" to { exp7 }
expect: does " UNIX password: " (spawn_id exp7) match glob pattern "Enter"? no
^Csighandler: handling signal(2)
async event handler: Tcl_Eval(exit 130)
最初の 'spawn'が終了するのを待つ必要があります。そうしないと、1人のユーザーに対して2つの' passwd'プロセスが実行されます。 – pynexj
@pynexj私は完全な秒を待っていましたか? passwdはパスワードを再入力した後、それより速く終了します..とにかく、私は10秒でさえ試しましたが、うまくいきませんでした。私はまた、2番目のスポーンの前に "exit"コマンドを明示的に使用しましたが、それでも動作しませんでした。私はまた、 "wait"、 "close"を試みました。 –
あなたが確信していれば、それはまさに*悪い習慣だとは思うけど。 – pynexj