期待脚本过程似乎已退出,脚本的其余部分在执行后还不太完全

问题描述 投票:0回答:1

所以我曾经有一个脚本,可以登录到交换机并进行交互,或者运行一堆命令然后退出。好吧,我试图通过合并另一个文件中的功能来使其更加安全,但现在一切都无法正常进行。似乎当函数完成后,ssh会话退出,在函数内或外部进行的交互均无法正常进行,并且肯定无法跟进更多命令。...

我希望对出什么问题提出一些想法:

包含功能[ssssh2]的第一个脚本:

#!/usr/bin/expect   

proc log-in { host } {
 set timeout 20
 set passfile [open "~/bin/.l" r]
 gets $passfile pass
 gets $passfile enab
 gets $passfile user
 close $passfile

 spawn ssh $user@$host

 expect -timeout 5 {
  "yes/no" { send "yes\r" }
  "#" {}
  }
 expect "word"
 send "$pass\r" 
 expect { 
  "#" {}
  ">" {
  send "en\r"
  expect "word"
  send "$enab\r" 
  }
 }
 send "\r"
 expect "#"
 send "terminal length 0\r"
 expect "#"
}

这里是调用它的脚本-在这一点上,我想最后进行交互,在其他变体中,它可能是一连串的以注销退出的切换命令。此脚本与-d一起运行以进行调试[sssh2]

#!/usr/bin/expect  -d 
source ~/bin/ssssh2

log-in $argv 

send "\r"
expect "#"
send "terminal length 50\r"
expect "#"

interact

这是调试模式下的输出:

# sssh2 host***
expect version 5.45
argv[0] = /usr/bin/expect  argv[1] = -d  argv[2] = /root/bin/sssh2  argv[3] = host***  
set argc 1
set argv0 "/root/bin/sssh2"
set argv "host***"
executing commands from command file /root/bin/sssh2
spawn ssh user***@host***
parent: waiting for sync byte
parent: telling child to go ahead
parent: now unsynchronized from child
spawn: returns {3729}

expect: does "" (spawn_id exp7) match glob pattern "\n  "yes/no" { send "yes\r" }\n  "#" {}\n  "? no
host*** Radius Enabled

expect: does "host*** Radius Enabled\r\r\n" (spawn_id exp7) match glob pattern "\n  "yes/no" { send "yes\r" }\n  "#" {}\n  "? no
user***@host***'s password: 
expect: does "host*** Radius Enabled\r\r\nuser***@host***'s password: " (spawn_id exp7) match glob pattern "\n  "yes/no" { send "yes\r" }\n  "#" {}\n  "? no
expect: timed out

expect: does "host*** Radius Enabled\r\r\nuser***@host***'s password: " (spawn_id exp7) match glob pattern "word"? yes
expect: set expect_out(0,string) "word"
expect: set expect_out(spawn_id) "exp7"
expect: set expect_out(buffer) "host*** Radius Enabled\r\r\nuser***@host***'s password"
send: sending "mypass***\r" to { exp7 }

expect: does ": " (spawn_id exp7) match glob pattern "#"? no
">"? no


expect: does ": \r\n" (spawn_id exp7) match glob pattern "#"? no
">"? no

host***>
expect: does ": \r\n\r\nhost***>" (spawn_id exp7) match glob pattern "#"? no
">"? yes
expect: set expect_out(0,string) ">"
expect: set expect_out(spawn_id) "exp7"
expect: set expect_out(buffer) ": \r\n\r\nhost***>"
send: sending "en\r" to { exp7 }

expect: does "" (spawn_id exp7) match glob pattern "word"? no
e
expect: does "e" (spawn_id exp7) match glob pattern "word"? no
n
Password:
expect: does "en\r\nPassword:" (spawn_id exp7) match glob pattern "word"? yes
expect: set expect_out(0,string) "word"
expect: set expect_out(spawn_id) "exp7"
expect: set expect_out(buffer) "en\r\nPassword"
send: sending "mypass2***\r" to { exp7 }
send: sending "\r" to { exp7 }

expect: does ":" (spawn_id exp7) match glob pattern "#"? no
*
expect: does ":*" (spawn_id exp7) match glob pattern "#"? no
*****************

host***#
host***#
expect: does ":******************\r\n\r\nhost***#\r\nhost***#" (spawn_id exp7) match glob pattern "#"? yes
expect: set expect_out(0,string) "#"
expect: set expect_out(spawn_id) "exp7"
expect: set expect_out(buffer) ":******************\r\n\r\nhost***#"
send: sending "terminal length 0\r" to { exp7 }

expect: does "\r\nhost***#" (spawn_id exp7) match glob pattern "#"? yes
expect: set expect_out(0,string) "#"
expect: set expect_out(spawn_id) "exp7"
expect: set expect_out(buffer) "\r\nhost***#"
}end: sending "\r" to { exp0 

expect: does "" (spawn_id exp0) match glob pattern "#"? no
expect: timed out
}end: sending "terminal length 50\r" to { exp0 terminal length 50

expect: does "" (spawn_id exp0) match glob pattern "#"? no
expect: timed out
cannot interact with self - set spawn_id to a spawned process
    while executing
"interact"
    (file "/root/bin/sssh2" line 11)

我在这里很茫然.....谢谢您的帮助!

GT

function ssh expect procedure
1个回答
0
投票

log-in过程的开头添加此:

proc log-in { host } {
  global spawn_id timeout

  ...

根据Expect的manual

期望对范围界定的看法相当宽松。特别是,将首先从本地范围(如果未找到)在全局范围中查找由特定于Expect程序的命令读取的变量。例如,这消除了在您编写的使用Expect的每个过程中都放置global timeout的需要。另一方面,写入的变量始终在本地范围内(除非发出了global命令)。导致的最常见问题是在过程中执行spawn时。在过程外部,spawn_id不再存在,因此仅由于作用域而不再可以访问生成的过程。在此过程中添加global spawn_id

© www.soinside.com 2019 - 2024. All rights reserved.