如何在一个脚本/批处理文件中更改多个unix密码?
我使用putty中的连接类型“SSH”,从Windows连接到8个不同的unix服务器。我对每台服务器使用相同的用户名/密码 当前,当我需要更改密码时(每60天),我需要打开putty,选择要连接的会话,键入当前密码(在打开的putty窗口中),键入“passwd”,输入当前密码,然后输入新密码 然后我退出并重复该过程7次 我如何将其转换为一个自动化流程,在这个流程中,我只需要提供一个带有新旧密码的脚本/批处理流程 听起来像是您想要的,它是TCL的一个扩展,可以模拟控制台应用程序的键盘输入。有关如何执行此操作,请参见 现在你写了一些让我担心的东西: 我使用putty中的连接类型“SSH”连接到8个不同的unix服务器。我对每台服务器使用相同的用户名/密码如何在一个脚本/批处理文件中更改多个unix密码?,unix,scripting,batch-file,ssh,putty,Unix,Scripting,Batch File,Ssh,Putty,我使用putty中的连接类型“SSH”,从Windows连接到8个不同的unix服务器。我对每台服务器使用相同的用户名/密码 当前,当我需要更改密码时(每60天),我需要打开putty,选择要连接的会话,键入当前密码(在打开的putty窗口中),键入“passwd”,输入当前密码,然后输入新密码 然后我退出并重复该过程7次 我如何将其转换为一个自动化流程,在这个流程中,我只需要提供一个带有新旧密码的脚本/批处理流程 听起来像是您想要的,它是TCL的一个扩展,可以模拟控制台应用程序的键盘输入。有关
为什么不使用SSH密钥来自动登录?以下是我如何自动完成该过程的:
package require Expect
exp_log_user 0
set exp::nt_debug 1
proc changepw {host user oldpass newpass} {
spawn plink $host
log_user 0
expect {
"login as: " { }
}
exp_send "$user\r"
expect "sword: "
exp_send "$oldpass\r"
expect "\$ "
exp_send "passwd\r"
expect "sword: "
exp_send "$oldpass\r"
expect "sword: "
exp_send "$newpass\r"
expect "sword: "
exp_send "$newpass\r"
set result $expect_out(buffer)
exp_send "exit\r"
return $result
}
label .userlbl -text "Username:"
label .oldpasslbl -text "\nOld Password: "
label .newpasslbl -text "\nNew Password: "
set username "username"
entry .username -textvariable username
set oldpassword "oldpassword"
entry .oldpassword -textvariable oldpassword
set newpassword "newpassword"
entry .newpassword -textvariable newpassword
button .button1 -text "Change Password" -command {
set fp [open "servers.txt" r]
set file_data [read $fp]
close $fp
set data [split $file_data "\n"]
foreach line $data {
.text1 insert end "Changing password for: $line\n"
set output [changepw $line $username $oldpassword $newpassword]
.text1 insert end "$output\n\n"
}
}
text .text1 -width 50 -height 30
pack .userlbl .username .oldpasslbl .oldpassword .newpasslbl .newpassword .button1 .text1
其余的应该不言自明。注意:当密码更改成功时,程序不会输出,但当密码更改失败时,程序会告诉您。另外请注意,这是我的第一个tcl脚本(也是第一次使用Expect),因此该脚本绝不是“优化”的,可能会得到改进,但它完成了任务。请随意编辑或提出建议/改进。很棒的文章!只是详细说明第三步。请注意,在“teacup install Expect”因连接问题而失败时,提供代理服务器信息的命令:
%teacup install Expect
Resolving Expect ... Not found in the archives.
...
Aborting installation, was not able to locate the requested entity.
child process exited abnormally
% teacup list teacup
0 entities found
Problems which occurred during the operation:
* http://teapot.activestate.com :
{connect failed connection refused} {can't read
"state(sock)": no such element in array while executing
"fileevent $state(sock) writable {}"} NONE
% teacup proxy "abcproxy.mycorp.com" 8080
Proxying through abcproxy.mycorp.com @ 8080
% set http_proxy_user MyNetworkID
MyNetworkID
% set http_proxy_pass MyNetworkPassword
MyNetworkPassword
% teacup list teacup
entity name version platform
----------- ------ --------------- ----------
application teacup 8.5.16.0.298388 win32-ix86
----------- ------ --------------- ----------
1 entity found
% teacup install Expect
Resolving Expect ... [package Expect 5.43.2 win32-ix86 @ http://teapot.activestate.com]
Resolving Tcl 8.4 -is package ... [package Tcl 8.6.1 _ ... Installed outside repository, probing dependencies]
Retrieving package Expect 5.43.2 win32-ix86 ...@ http://teapot.activestate.com ...
Ok
Installing into C:/app/Tcl/lib/teapot
Installing package Expect 5.43.2 win32-ix86
%
您能否提供有关使用Except/TCL的更多详细信息?我已经下载了ActiveState ActiveTCL,它在我的任务栏中转储了Tclsh85、tkcon、vfs explorer和wish85。我应该使用哪一个以及如何使用它下载/使用Expect?活动的TCL文档在这些简单的步骤中没有太大的帮助,只是说“使用茶壶获得期望”。@Droid抱歉,我从未在Windows上使用过TCL。我想我完全错过了你问题中“unix”之后的“putty”。无论如何,我猜想您可以编写一个Expect脚本,驻留在Unix服务器上,通过命令行获取参数。然后,您只需指示Putty通过SSH发送远程命令。如何修改TCL脚本,使其在设置新密码之前可以执行“SUDO SU-”?在某些系统上,您首先需要将perms/access提升到root级别,然后才能更改密码。请记住,在运行“SUDO SU-”(当然没有引号)时,有时需要重新输入当前密码。PS-非常好的说明!