Command-Line

顯示在 unix“passwd”命令中輸入的新密碼

  • May 21, 2014

是否可以執行帶有顯示新輸入密碼的選項的**passwd命令?**預設情況下,它不顯示我輸入的內容,我不想要這個。

[dave@hal9000 ~]$ passwd 
Changing password for user dave.
Changing password for dave.
(current) UNIX password: 
New password: bowman
Retype new password: bowman
passwd: all authentication tokens updated successfully.

如果你真的想走這條路並且沒有passwd參數,你可以使用這個Expect腳本:

#!/usr/bin/env expect -f
set old_timeout $timeout
set timeout -1

stty -echo
send_user "Current password: "
expect_user -re "(.*)\n"
set old_password $expect_out(1,string)

stty echo
send_user "\nNew password: "
expect_user -re "(.*)\n"
set new_password $expect_out(1,string)

set timeout $old_timeout
spawn passwd
expect "password:"
send "$old_password\r"
expect "password:"
send "$new_password\r"
expect "password:"
send "$new_password\r"
expect eof

這個怎麼運作:

[dave@hal9000 ~]$ ./passwd.tcl
Current password: 
New password: bowman
spawn passwd
Changing password for user dave.
Changing password for dave.
(current) UNIX password: 
New password: 
Retype new password: 
passwd: all authentication tokens updated successfully.

這個 shell 腳本也可以工作(在 Fedora 20 上使用 bash-4.2.47-2 和 passwd-0.79-2 測試):

#!/bin/sh
stty -echo
echo -n "Current password: "
read old_password

stty echo
echo
echo -n "New password: "
read new_password

passwd << EOF
$old_password
$new_password
$new_password
EOF

這個怎麼運作:

[dave@hal9000 ~]$ ./passwd.sh
Current password: 
New password: bowman
Changing password for user dave.
Changing password for dave.
(current) UNIX password: New password: Retype new password: passwd: all authentication tokens updated successfully.

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