Script to change password on linux servers over ss

2020-02-02 12:10发布

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 on each one of those in a single go, using SSH.

I have tried to find a solution but many of the scripts I found are using Expect. We do not have Expect installed on our servers and the system admins have refused to let us install it. Also, the users do not have root access so passwd --stdin or chpasswd cannot be used.

Is there any way a script can be written so that a user can run it and change the password of only his own user on all the servers in a list?

16条回答
等我变得足够好
2楼-- · 2020-02-02 12:39

An alternative you may want to present to your peers would be to have them use password-less authentication. They'd generate a public/private key pair and register their public key in the ~/.ssh/authorized_keys file on each of the servers they log into.

查看更多
倾城 Initia
3楼-- · 2020-02-02 12:39

Can you use Perl?

Here there is an script that changes the password in a set of hosts.

If requires some Perl modules (Net::OpenSSH::Parallel, Expect and their dependencies) installed on the local machine running the script but nothing on the remote servers where the password has to be changed.

查看更多
Summer. ? 凉城
4楼-- · 2020-02-02 12:40
  1. Install sshpass on any of the server from where you want to execute the script.

    yum -y install sshpass
    
  2. Prepare a text file in which you have to pass details like Host, User Name, Password and Port. (Based on your requirement).

    192.168.1.2|sachin|dddddd|22
    
  3. Prepare a script file using below details.

    #!/bin/bash
    
    FILE=/tmp/ipaddress.txt
    
    MyServer=""
    MyUser=""
    MyPassword=""
    MyPort=""
    
    exec 3<&0
    exec 0<$FILE
    
    while read line
    do
        MyServer=$(echo $line | cut -d'|' -f1)
        MyUser=$(echo $line | cut -d'|' -f2)
        MyPassword=$(echo $line | cut -d'|' -f3)
        MyPort=$(echo $line | cut -d'|' -f4)
    
        HOST=$MyServer
        USR=$MyUser
        PASS=$MyPassword
    
        sshpass -p $PASS ssh -p $MyPort -o StrictHostKeychecking=no $USR@$HOST \
                -T "echo 'sachin@patel' | passwd --stdin root"                 \
                < /dev/null | tee -a output.log
    done
    
    exec 0<&3
    
查看更多
干净又极端
5楼-- · 2020-02-02 12:43
echo "name:password" | chpasswd
查看更多
我命由我不由天
6楼-- · 2020-02-02 12:46

echo -e "wakka2\nwakka2\n" | passwd root

查看更多
叼着烟拽天下
7楼-- · 2020-02-02 12:48

If you have ssh, why have passwords in the first place? Push the user's public ssh key to all the servers they're authorized to use and be done with it. This also lets you easily grant and revoke access all you want.

At a previous $dayjob, where we had literally tens of thousands of servers, they had a database of which engineers were allowed on which servers, and the installation of ssh keys was an automated process. Almost NOBODY had a password on ANY machine.

查看更多
登录 后发表回答