如何在一个脚本/批处理文件中更改多个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的一个扩展,可以模拟控制台应用程序的键盘输入。有关

我使用putty中的连接类型“SSH”,从Windows连接到8个不同的unix服务器。我对每台服务器使用相同的用户名/密码

当前,当我需要更改密码时(每60天),我需要打开putty,选择要连接的会话,键入当前密码(在打开的putty窗口中),键入“passwd”,输入当前密码,然后输入新密码

然后我退出并重复该过程7次

我如何将其转换为一个自动化流程,在这个流程中,我只需要提供一个带有新旧密码的脚本/批处理流程

听起来像是您想要的,它是TCL的一个扩展,可以模拟控制台应用程序的键盘输入。有关如何执行此操作,请参见

现在你写了一些让我担心的东西:

我使用putty中的连接类型“SSH”连接到8个不同的unix服务器。我对每台服务器使用相同的用户名/密码


为什么不使用SSH密钥来自动登录?

以下是我如何自动完成该过程的:

  • 下载并安装(即使您在64位windows上,也要下载32位版本,因为64位版本没有运行自动脚本所需的“Expect”)

  • 打开安装程序创建的tclsh85可执行文件

  • 运行此命令“teacup install Expect”(注意,此命令区分大小写。如果收到错误和/或在vpn上或使用代理,则可能需要设置特殊的http设置)

  • 并将其放置在ActiveTCL的bin目录中(默认安装目录为“C:\Tcl\bin”),或更改“Path”环境变量以包含此可执行文件的路径(无论您在何处下载plink.exe)。这是脚本将使用的Putty的命令行版本

  • 在驱动器的任何位置,创建一个名为“servers.txt”的文本文件,其中包含服务器列表(每行一个)。它们都应该共享相同的密码,因为脚本将使用相同的密码(您提供的)登录到它们,并将密码更改为您提供的密码

  • 在与“servers.txt”相同的目录中创建一个名为“ChangePassword.tcl”的新文本文件(或任何您想调用的文件,但确保其文件类型为“tcl”)。右键单击文件并在记事本中编辑(或您喜欢的任何文本编辑器),然后将此脚本粘贴到其中

    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
    
  • 保存脚本,然后启动ChangePassword.tcl文件

  • 下面是打开ChangePassword.tcl文件时的图像:


    其余的应该不言自明。注意:当密码更改成功时,程序不会输出,但当密码更改失败时,程序会告诉您。另外请注意,这是我的第一个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-非常好的说明!