使用过程散布SSH无法正常工作 [英] Using procedure for spawing SSH doesn't work properly with expect

查看:56
本文介绍了使用过程散布SSH无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Expect脚本,当使用一个程序生成SSH时,后来使用Expect不起作用,当直接使用ssh spawn时,Expect看起来工作正常. 知道这里有什么问题吗?

I have a expect script which when uses a procedure to spawn SSH and, later using expect doesn't work, when ssh spawn is directly used, the expect thing seems to work fine. Any idea what can be wrong here ??

在其他情况下,我使用相同的代码段,并且工作正常.

I am using the same code snippet in the other case and works fine.

proc sshLogin {serverName userName password} {
  global logfd
  set timeout 10
  logMessage "Connecting to  server = $serverName, please wait..."
  log_user 0
  spawn -noecho ssh -X "$userName\@$serverName"
  expect { 
    "password:" {
      send "$password\r"
      expect {
        "]#" {
          log_user 1
          logMessage "Logged in to Server = $serverName"
          return 1
        }
        "Permission denied" {
          log_user 1
          logMessage "Incorrect password!"
        }
      }
    }
    timeout {
      log_user 1
      logMessage "Connection to $serverName timed out"
    }
    eof {
      log_user 1
      logMessage "Connection to $serverName failed!"
    }
  }
  return 0
}

推荐答案

您还应该通过添加以下内容来声明spawn_id是过程中的全局变量:

You should also declare that spawn_id is a global variable in your procedure, by either adding:

global spawn_id

或将global logfd更改为:

global logfd spawn_id

这是因为spawn_id变量的当前值用于确定要控制的内容(如果希望对两个子过程进行控制,则非常重要). Expect试图做一些聪明"的事情来处理将expect/send调用放在过程中而无需声明的情况,但这不是一个非常可靠的方法,并且不能与spawn一起使用本身:您必须使其明确(无论如何使用过程,这都是一个好主意).

This is because the current value of the spawn_id variable is used to determine what is being controlled (vital if you want to have expect control two subprocesses). Expect tries to do some "clever" stuff to handle the case where you've put the expect/send calls in a procedure without declaring things, but it's not a very reliable method and it does not work with spawn itself: you have to make it explicit (which is a good idea anyway when working with procedures).

这篇关于使用过程散布SSH无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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