SSH를 통해 액세스할 수 있는 원격 Solaris 시스템의 루트 비밀번호를 변경하기 위해 Linux 시스템에서 실행하는 스크립트가 있습니다. 모든 것이 괜찮은 것 같습니다. passwd 명령은 비밀번호를 요청합니다. 두 번째 비밀번호는 응답 길이가 8자가 아니고...으로 끝납니다. 업데이트는 성공했지만 Solaris로 가서 확인하면 비밀번호가 변경되지 않습니다. 섀도우 파일은 수정되지 않습니다. 아무런 문제 없이 Solaris 시스템에서 직접 비밀번호를 변경할 수 있습니다.
$ cat ./expect2.txt
#!/usr/bin/expect --
# Input: username password hostname
set USER [lindex $argv 0]
set PASS [lindex $argv 1]
set IP [lindex $argv 2]
spawn ssh user1@$IP
expect "user1"
spawn sudo passwd $USER
expect "assword:"
send "$PASS\r"
expect "assword:"
send "$PASS\r"
expect eof
exit
exit
스크립트를 실행합니다.
$ expect ./expect2.txt root abc123 host1
spawn ssh user1@host1
host1 user1 : spawn sudo passwd root
Changing password for user root.
New password:
BAD PASSWORD: The password is shorter than 8 characters
Retype new password:
passwd: all authentication tokens updated successfully.
디버그
$ expect -d ./expect2.txt root abc123 host1
expect version 5.45.4
argv[0] = expect argv[1] = -d argv[2] = ./expect2.txt argv[3] = root argv[4] = abc123 argv[5] = host1
set argc 3
set argv0 "./expect2.txt"
set argv "root abc123 host1"
executing commands from command file ./expect2.txt
spawn ssh user1@host1
parent: waiting for sync byte
parent: telling child to go ahead
parent: now unsynchronized from child
spawn: returns {2669765}
expect: does "" (spawn_id exp4) match glob pattern "user1"? no
match glob pattern "user1"? yes
expect: set expect_out(0,string) "user1"
expect: set expect_out(spawn_id) "exp4"
spawn sudo passwd root
parent: waiting for sync byte
parent: telling child to go ahead
parent: now unsynchronized from child
spawn: returns {2669769}
expect: does "" (spawn_id exp7) match glob pattern "assword:"? no
Changing password for user root.
New password:
expect: does "Changing password for user root.\r\nNew password: " (spawn_id exp7) match glob pattern "assword:"? yes
expect: set expect_out(0,string) "assword:"
expect: set expect_out(spawn_id) "exp7"
expect: set expect_out(buffer) "Changing password for user root.\r\nNew password:"
send: sending "abc123\r" to { exp7 }
expect: does " " (spawn_id exp7) match glob pattern "assword:"? no
BAD PASSWORD: The password is shorter than 8 characters
Retype new password:
expect: does " \r\nBAD PASSWORD: The password is shorter than 8 characters\r\nRetype new password: " (spawn_id exp7) match glob pattern "assword:"? yes
expect: set expect_out(0,string) "assword:"
expect: set expect_out(spawn_id) "exp7"
expect: set expect_out(buffer) " \r\nBAD PASSWORD: The password is shorter than 8 characters\r\nRetype new password:"
send: sending "abc123\r" to { exp7 }
passwd: all authentication tokens updated successfully.
expect: read eof
expect: set expect_out(spawn_id) "exp7"
expect: set expect_out(buffer) " \r\npasswd: all authentication tokens updated successfully.\r\n"
답변1
그러면 대표 호스트와의 원격 세션이 시작됩니다 $IP
.
spawn ssh user1@$IP
expect "user1"
이것이 시작된다새 세션다음으로 표시되는 사용자의 비밀번호를 변경하십시오 $USER
.
spawn sudo passwd $USER
expect "assword:"
이 두 세션은 서로 독립적이며 $USER
localhost에서 비밀번호를 변경한다는 점에 유의하세요. 의도적으로 그렇게 할 수도 있지만 send
그렇지 않습니다 spawn
.