Using the passwd command from within a shell script

前端 未结 13 996
遥遥无期
遥遥无期 2020-11-28 04:20

I\'m writing a shell script to automatically add a new user and update their password. I don\'t know how to get passwd to read from the shell script instead of interactively

13条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-11-28 04:58

    This is the definitive answer for a teradata node admin.

    Go to your /etc/hosts file and create a list of IP's or node names in a text file.

    SMP007-1
    SMP007-2
    SMP007-3
    

    Put the following script in a file.

    #set a password across all nodes
    printf "User ID: "
    read MYUSERID
    printf "New Password: "
    read MYPASS
    
    while read -r i; do
        echo changing password on "$i"
        ssh root@"$i" sudo echo "$MYUSERID":"$MYPASS" | chpasswd
        echo password changed on "$i"
    done< /usr/bin/setpwd.srvrs
    

    Okay I know I've broken a cardinal security rule with ssh and root but I'll let you security folks deal with it.

    Now put this in your /usr/bin subdir along with your setpwd.srvrs config file.

    When you run the command it prompts you one time for the User ID then one time for the password. Then the script traverses all nodes in the setpwd.srvrs file and does a passwordless ssh to each node, then sets the password without any user interaction or secondary password validation.

提交回复
热议问题