Chinaunix首页 | 论坛 | 博客
  • 博客访问: 3759239
  • 博文数量: 880
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 6155
  • 用 户 组: 普通用户
  • 注册时间: 2016-11-11 09:12
个人简介

To be a better coder

文章分类

全部博文(880)

文章存档

2022年(5)

2021年(60)

2020年(175)

2019年(207)

2018年(210)

2017年(142)

2016年(81)

分类: LINUX

2018-10-15 11:00:23

[root@localhost /]#
[root@localhost /]# cat etc/shadow
root:$1$yactor$hQOUjv5W8P8X0RBq79mBC1:16452::::::
bin:x:14871::::::
daemon:x:14871::::::
mail:x:14871::::::
ftp:x:14871::::::
http:x:14871::::::
uuidd:x:14871::::::
dbus:x:14871::::::
nobody:x:14871::::::
systemd-journal-gateway:x:14871::::::
systemd-timesync:x:14871::::::
systemd-network:x:14871::::::
systemd-bus-proxy:x:14871::::::
systemd-resolve:x:14871::::::
systemd-journal-remote:!!:16410::::::
systemd-journal-upload:!!:16410::::::
tcpdump:!:16527:0:99999:7:::
sshd:x:16580:0:99999:7:::
adm:$1$yoctor$T17F0mrpOJuD2ephfo6ff1:16580:0:99999:7:::
username:$6$GdReOenu$i7znCr0j7TdHU4PXYkw/cW9bQRCWfHrVb9nx.cca3yKLfqdOVJAHc1DnQF6mSJGVdpcJ/qdLLq7oAUUxCsYaB1:17813:0:99999:7:::
zlg:$6$2OsXyA.d$su.E49d64b9MRiyBSnK4ZJZV/pmj9.9ad4.VIlfkpQ1pKrqh.jK.CSNuEKZDPdJRE9kcaDmxlahZ6mI/4kPJ90:17816:0:99999:7:::
[root@localhost /]#
[root@localhost /]# cat etc/passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/usr/bin/nologin
daemon:x:2:2:daemon:/:/usr/bin/nologin
mail:x:8:12:mail:/var/spool/mail:/usr/bin/nologin
ftp:x:14:11:ftp:/srv/ftp:/usr/bin/nologin
http:x:33:33:http:/srv/http:/usr/bin/nologin
uuidd:x:68:68:uuidd:/:/usr/bin/nologin
dbus:x:81:81:dbus:/:/usr/bin/nologin
nobody:x:99:99:nobody:/:/usr/bin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
systemd-journal-gateway:x:191:191:systemd-journal-gateway:/:/usr/bin/nologin
systemd-timesync:x:192:192:systemd-timesync:/:/usr/bin/nologin
systemd-network:x:193:193:systemd-network:/:/usr/bin/nologin
systemd-bus-proxy:x:194:194:systemd-bus-proxy:/:/usr/bin/nologin
systemd-resolve:x:195:195:systemd-resolve:/:/usr/bin/nologin
systemd-journal-remote:x:999:999:systemd Journal Remote:/:/sbin/nologin
systemd-journal-upload:x:998:998:systemd Journal Upload:/:/sbin/nologin
tcpdump:x:1000:1000::/home/tcpdump:/bin/bash
adm:x:0:0:root:/home/:/usr/bin/term
username:x:1001:1001::/home/username:/usr/bin/term
zlg:x:1002:1002::/home/zlg:/bin/bash
[root@localhost /]#
[root@localhost /]#
[root@localhost /]# cat etc/shells
#
# /etc/shells
#

/bin/sh
/bin/bash
/usr/bin/term
# End of file
[root@localhost /]#
[root@localhost /]#
[root@localhost /]# export $PATH
-bash: export: `/usr/local/sbin:/usr/local/bin:/usr/bin:/software/bin:/usr/bin/site_perl:/usr/bin/vendor_perl:/usr/bin/core_perl': not a valid identifier
[root@localhost /]#


最近管理的一批机器,有个需求是要统一修改一个帐号的用户名密码,比如将qa帐号的密码改为1234,后来还为了脚本化,很方便的执行,还使用了非交互式地修改用户的密码。简单记录一下吧。

  1. 交互式配置本地用户的密码:passwd 命令
[root@host_221-81 ~]# passwd qa Changing password for user qa. New password: 
BAD PASSWORD: it is too short BAD PASSWORD: is too simple
Retype new password: 
passwd: all authentication tokens updated successfully.
  1. 非交互式修改本地用户的密码:chpasswd
# chpasswd命令使用起来很简洁 [root@host_221-81 ~]# echo "qa:1234" | chpasswd # 使用passwd命令,也可以实现非交互式修改密码 [root@host_221-81 ~]# echo "1234" | passwd --stdin "qa" Changing password for user qa.
passwd: all authentication tokens updated successfully.
  1. 使用expect来处理交互式输入,从而实现非交互式的密码修改。
#!/bin/sh # \ exec expect -f "$0" "$@" if { $argc != 2 } {
    puts "Usage: $argv0  " exit 1 } set password [lindex $argv 1]
spawn passwd [lindex $argv 0]
sleep 1 expect "assword:" send "$password\r" expect "assword:" send "$password\r" expect eof

注意:脚本的第二行,这种写法可能比较陌生,这是在TCL语言中的语法,The backslash is recognized as part of a comment to sh, but in Tcl the backslash
continues the comment into the next line which keeps the exec command from executing again.

该脚本的执行结果为:

[root@smilejay ~]# ./change-pwd-expect.sh qa 1234 spawn passwd qa
Changing password for user qa. New password: 
BAD PASSWORD: it is too short BAD PASSWORD: is too simple
Retype new password: 
passwd: all authentication tokens updated successfully.
阅读(1135) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~