Script to change password on linux servers over ssh

后端 未结 17 2759
南方客
南方客 2020-12-08 11:22

We have a number of Red Hat linux servers in our IT environment. I am being asked by my team members to write a script (preferably shell script) to change a user\'s password

17条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-08 11:35

    The remote machine(s) do not need expect installed. You can install expect on a local workstation or VM (virtualbox) or whichever *nix box, and write a wrapper that calls this .ex (expect) script (there may be small changes from distro to distro, this tested on CentOS 5/6):

    #!/usr/bin/expect -f
    # wrapper to make passwd(1) be non-interactive
    # username is passed as 1st arg, passwd as 2nd
    
    set username [lindex $argv 0]
    set password [lindex $argv 1]
    set serverid [lindex $argv 2]
    set newpassword [lindex $argv 3]
    
    spawn ssh $serverid passwd
    expect "assword:"
    send "$password\r"
    expect "UNIX password:"
    send "$password\r"
    expect "password:"
    send "$newpassword\r"
    expect "password:"
    send "$newpassword\r"
    expect eof
    

提交回复
热议问题