2017-12-07 18 views
1

나는 초보자가 "expect"도구를 배우기 때문에이 단계에서 여러 가지 연습을 시도하고 있습니다."expect"에 대한 기본 연습 - 초보자

다음은 Unix 시스템의 암호를 변경하고 이전 버전을 복원하는 두 개의 연속 "spawn"명령 사용을 보여주는 간단한 연습입니다. 기본적으로, 하나는 수동으로 무엇을 자동화 :

$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'이 종료 될 때까지 기다려야합니다. 그렇지 않으면 하나의 단일 사용자에 대해 두 개의'passwd' 프로세스가 실행됩니다. – pynexj

+0

@pynexj 전체 초를 기다렸지, 그렇지? 암호를 다시 입력하면 passwd가 빠져 나간다. 어쨌든 10 초 만에 시도해 보았지만 작동하지 않았다. 나는 또한 두 번째 스폰 전에 명시 적으로 "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