Solaris

期望腳本不會在 Solaris 上更改密碼

  • May 16, 2022

我有這個腳本,我從 Linux 機器上執行,以更改可通過 ssh 訪問的遠端 Solaris 機器上的 root 密碼。一切似乎都很好, 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"

這將啟動到由 表示的主機的遠端會話$IP

spawn ssh user1@$IP
expect "user1"

這將啟動一個新會話來更改由 代表的使用者的密碼$USER

spawn sudo passwd $USER
expect "assword:"

請注意,這兩個會話彼此獨立,您正在更改$USER本地主機上的密碼。您可能打算send而不是spawn.

引用自:https://unix.stackexchange.com/questions/702741