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

查看:383
本文介绍了通过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,系统管理员拒绝让我们安装它.另外,用户没有超级用户访问权限,因此无法使用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(期望)脚本的包装器(发行版之间的细微变化,已在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\r"
expect "UNIX password:"
send "$password\r"
expect "password:"
send "$newpassword\r"
expect "password:"
send "$newpassword\r"
expect eof

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

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