通过 ssh 在 linux 服务器上更改密码的脚本 [英] Script to change password on linux servers over ssh

查看:21
本文介绍了通过 ssh 在 linux 服务器上更改密码的脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们的 IT 环境中有许多 Red Hat linux 服务器.我的团队成员要求我编写一个脚本(最好是 shell 脚本)以使用 SSH 一次性更改每个用户的密码.

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.

我试图找到解决方案,但我发现的许多脚本都使用了 Expect.我们的服务器上没有安装 Expect,系统管理员拒绝让我们安装它.此外,用户没有 root 访问权限,因此不能使用 passwd --stdinchpasswd.

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?

推荐答案

远程机器不需要安装.您可以在本地工作站或 VM(虚拟机)或任何 *nix 机器上安装 expect,并编写一个调用此 .ex(expect)脚本的包装器(从发行版到发行版可能会有小的变化,这在 CentOS 5/6 上测试):

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
"
expect "UNIX password:"
send "$password
"
expect "password:"
send "$newpassword
"
expect "password:"
send "$newpassword
"
expect eof

这篇关于通过 ssh 在 linux 服务器上更改密码的脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆