【问题标题】:User input for Expect scriptExpect 脚本的用户输入
【发布时间】:2014-02-20 00:41:44
【问题描述】:

我是脚本新手。如何编写 Expect 脚本以 ssh 进入设备并提示用户输入密码?我们使用 pin + RSA 令牌代码作为密码,所以我无法存储密码。

#!/usr/bin/expect -f

spawn ssh device1

【问题讨论】:

    标签: expect prompt


    【解决方案1】:

    我以前使用过此代码来实现您希望实现的目标。您可以将其作为参考点来编写您自己的代码版本。

    #!/usr/bin/env expect -f
    set passw [lindex $argv 0]
    
    #timeout is a predefined variable in expect which by default is set to 10 sec
    set timeout 60 
    spawn ssh $user@machine
    while {1} {
      expect {
    
        eof                          {break}
        "The authenticity of host"   {send "yes\r"}
        "password:"                  {send "$password\r"}
        "*\]"                        {send "exit\r"}
      }
    }
    wait
    #spawn_id is another default variable in expect. 
    #It is good practice to close spawn_id handle created by spawn command
    close $spawn_id
    

    来源:Expect Wiki

    【讨论】:

      【解决方案2】:

      您需要使用交互命令自己输入密码。

      下面的骨架应该会让你上路:

      set prompt "$"
      
      spawn ssh user@host
      
      set timeout 10
      expect {
          timeout {
              puts "Unable to connect"
              exit 1
          }
      
          #Authenticty Check
          "*(yes/no)?" {
              send "yes\r"
              exp_continue
          }
      
          "assword: " {
              #Hand control back to user
              interact -o "\r" exp_continue
          }
      
          "$prompt" {
              puts "Cool by the pool"
          }
      }
      

      【讨论】:

        【解决方案3】:

        下面的代码将要求用户输入密码,并将在需要密码的脚本中进一步使用它。就像 $pass。

        获取要在脚本中进一步使用的密码

        stty -echo
        send_user -- "Enter the password: "
        expect_user -re "(.*)\n"
        send_user "\n"
        stty echo
        set pass $expect_out(1,string)
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2010-11-24
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-05-07
          相关资源
          最近更新 更多