【问题标题】:changing password in linux taking values from file在linux中更改密码从文件中获取值
【发布时间】:2015-09-18 14:52:15
【问题描述】:

脚本从文本文件或 excel 中获取值(excel 是更好的选择)。 文件包含三列

username    old password   new password
lg1211      cSR121         csd22
lf1245      fDFss          sfdge
ms1232      nHJaa          hYiao

脚本

# !/bin/bash
file=/root/Desktop/Scripts/login
# location of file
while read line
do
username=$(echo $line | awk '{print $1}')
oldpassword=$(echo $line | awk '{print $2}')
newpassword=$(echo $line | awk '{print $3}')
passwd $username

done<$file

我的问题是脚本执行此命令后如何使用 oldpassword 和 newpassword passwd $username

【问题讨论】:

  • 在 shell 中使用 excel 文档比使用 csv 或其他文本文件要困难得多。您实际上要在脚本中做什么您的“算法”实际上并没有显示任何工作?
  • @EtanReisner 实际上我想知道语法,例如通过使用 AWk 命令或任何其他命令,如何从文件中提取用户名、旧密码和新密码并更改所有用户的密码,直到 EOF、文本文件我会没事的
  • 选择一个确切的文件格式来使用,然后用 awk 或 perl 或其他你计划在以后实际使用的东西尝试它。我们无法回答一般的“我如何解析文件”问题。
  • 查看此页面 debian-administration.org/article/668/… 然后只需读取文件并将值传递给 usermod
  • @EtanReisner 现在请帮忙​​,我用脚本编辑查询

标签: linux shell scripting change-password


【解决方案1】:

如果你有管理员权限最好使用 chpasswd 命令:

# !/bin/bash
file=/root/Desktop/Scripts/login
# location of file
while read line
do
    echo $line | awk '{print $1 ":" $3}' | chpasswd  
done<$file

如果你没有管理员权限,你应该使用“expect”,查看这个链接Script to change password on linux servers over ssh

【讨论】:

    猜你喜欢
    • 2013-02-27
    • 1970-01-01
    • 2017-10-05
    • 2011-10-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-14
    • 1970-01-01
    相关资源
    最近更新 更多