2017-12-07 17 views
1

私は初心者ですが、この段階でいくつかのエクササイズを試しています。"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) 
+0

最初の 'spawn'が終了するのを待つ必要があります。そうしないと、1人のユーザーに対して2つの' passwd'プロセスが実行されます。 – pynexj

+0

@pynexj私は完全な秒を待っていましたか? passwdはパスワードを再入力した後、それより速く終了します..とにかく、私は10秒でさえ試しましたが、うまくいきませんでした。私はまた、2番目のスポーンの前に "exit"コマンドを明示的に使用しましたが、それでも動作しませんでした。私はまた、 "wait"、 "close"を試みました。 –

+0

あなたが確信していれば、それはまさに*悪い習慣だとは思うけど。 – pynexj

答えて

1

私は、すべてのパスワードに適用される正規表現パターンを使用するには、要求されます:

expect -re "password: $" 

変更の両方sleep 1interact

# create new password 
spawn passwd 
expect -re "password: $"; send -- "PW-OLD\r" 
expect -re "password: $"; send -- "PW-NEW\r" 
expect -re "password: $"; send -- "PW-NEW\r" 
expect eof 

# restore old password 
spawn passwd 
expect -re "password: $"; send -- "PW-NEW\r" 
expect -re "password: $"; send -- "PW-OLD\r" 
expect -re "password: $"; send -- "PW-OLD\r" 
expect eof 

expect eofにグロブパターンでは、場合あなたはワイルドカードを指定しない、基本的に文字列equをやっているアリティチェック。あなたは次のようにすることができます:

expect "*(current) UNIX password: " 
expect "*Enter new UNIX password: " 
expect "*Retype new UNIX password: " 

passwdプロンプトには末尾のスペースがあります。

+0

'expect * foo'は' expec foo'と同じです。 RE 'expect -re。* foo'は' expect -re foo'と同じです。 – pynexj

関連する問題