Batch change of user passwords in linux

Source: Internet
Author: User
Batch change of user passwords in linux

Script objective: to modify linux user passwords in batches

Condition: The modified user must be root, because only root has the permission to use the passwd command.

Note: First, write the IP address, user, password, and port information to the old_info file. the script reads the corresponding information from this file and uses the login CT to log on to the system without interaction. Change the randomly generated 8-bit password to the new password of the root user. Then save the new password to the net_info file.

# Cat old_info

# Ip user passwd port

#------------------------------------------------

192.168.18.217 root n8wX3mU % 22

192.168.18.218 root c87; ZnnL 22

# Cat change_pass.sh

#! /Bin/bash

OLD_INFO = ~ /Old_info

NEW_INFO = ~ /New_info

For IP in 'awk'/^ [^ #]/{print $1} '$ OLD_INFO'; do

USER = 'awk-v I = $ IP' {if (I = $1) print $2} '$ OLD_INFO'

PASS = 'awk-v I = $ IP' {if (I = $1) print $3} '$ OLD_INFO'

PORT = 'awk-v I = $ IP' {if (I = $1) print $4} '$ OLD_INFO'

NEW_PASS = 'mkpasswd-l 8'

Echo "$ IP $ USER $ NEW_PASS $ PORT" >>$ NEW_INFO

CT-c"

Spawn ssh-p $ PORT $ USER @ $ IP

Set timeout 2

CT {

\ "(Yes/no) \" {send \ "yes \ r \"; exp_continue}

\ "Password: \" {send \ "$ PASS \ r \"; exp_continue}

\ "$ USER @ * \" {send \ "echo \ '$ NEW_PASS \' | passwd -- stdin $ USER \ rexit \ r \"; exp_continue}

# \ "$ USER @ * \" {send \ "df-h \ r exit \ r \"; exp_continue}

}"

Done

# Cat new_info

192.168.18.217 root n8wX3mU % 22

192.168.18.218 root c87; ZnnL 22

Receive free Brothers IT education original linux O & M engineers video/detailed linux tutorial, details consulting official website Customer Service: http://www.lampbrother.net/linux/

Learn PHP, Linux, HTML5, UI, Android and other video tutorials (courseware + Notes + video )! Contact Q2430675018


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.