- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在编写一个 bash 脚本,它使用 expect 登录到一堆 Cisco ASA(它们不支持证书登录,因此使用 expect),更改配置然后注销。
如果无法登录,我希望脚本移至下一个 ASA。
这是脚本:
#!/bin/bash
# Scriptname: set-mtu
for asa in $(cat asa-list-temp)
do
/usr/bin/expect << EndExpect
spawn ssh admin_15@$asa
expect "assword:"
send "pa$$w0rd\r"
expect ">"
send "do something\r"
expect ">"
send "exit\r"
EndExpect
done
我想我可以在 expect "assword:"
上设置超时,但我不知道如何让它关闭生成的 ssh session ,然后转到 for 中的下一个 ASA列表。
最佳答案
首先,我会为此使用一个 expect 脚本并丢失 bash 脚本。
然后是预期的部分:您可以通过使用一个也匹配超时的开关来做到这一点(explore expect 的第 12 页)。通过这种方式,您可以在预期超时时明确地采取一些行动。
否则通过设置超时,它将继续执行下一个命令。
set timeout 60
expect {
"assword:" {
}
timeout {
exit 1 # to exit the expect part of the script
}
}
我创建了类似的东西,我使用一个整体期望脚本来并行运行一个期望脚本。
多重.exp
#!/bin/sh
# the next line restarts using tclsh \
exec expect "$0" "$@"
# multiple.exp --
#
# This file implements the running of multiple expect scripts in parallel.
# It has some settings that can be found in multiple.config
#
# Copyright (c) 2008
#
# Author: Sander van Knippenberg
#####
# Setting the variables
##
source [file dirname $argv0]/.multiple.config
# To determine how long the script runs
set timingInfo("MultipleProcesses") [clock seconds]
# ---------------------------------------------------------------------
######
# Procedure to open a file with a certain filename and retrieve the contents as a string
#
# Input: filename
# Output/Returns: content of the file
##
proc openFile {fileName} {
if {[file exists $fileName] } {
set input [open $fileName r]
} else {
puts stderr "fileToList cannot open $fileName"
exit 1
}
set contents [read $input]
close $input
return $contents
}
######
# Procedure to write text to a file with the given filename
#
# Input: string, filename
##
proc toFile {text filename} {
# Open the filename for writing
set fileId [open $filename "w"]
# Send the text to the file.
# Failure to add '-nonewline' will reslt in an extra newline at the end of the file.
puts -nonewline $fileId $text
# Close the file, ensuring the data is written out before continueing with processing
close $fileId
}
# ---------------------------------------------------------------------
# Check for the right argument
if {$argc > 0 } {
set hostfile [lindex $argv 0]
} else {
puts stderr "$argv0 --- usage: $argv0 <hosts file>"
exit 1
}
# Create the commands that can be spawned in parallel
set commands {}
# Open the file with devices
set hosts [split [openFile $hostfile] "\n"]
foreach host $hosts {
if { [string length $host] > 1 } {
lappend commands "$commandDir/$commandName $host" # Here you can enter your own command!
}
}
# Run the processes in parallel
set idlist {}
set runningcount 0
set pattern "This will never match I guess"
# Startup the first round of processes until maxSpawn is reached,
# or the commands list is empty.
while { [llength $idlist] < $maxSpawn && [llength $commands] > 0} {
set command [lindex $commands 0]
eval spawn $command
lappend idlist $spawn_id
set commands [lreplace $commands 0 0]
incr runningcount
set commandInfo($spawn_id) $command
set timingInfo($spawn_id) [clock seconds]
send_user " $commandInfo($spawn_id) - started\n"
}
# Finally start running the processes
while {$runningcount > 0} {
expect {
-i $idlist $pattern {
}
eof {
set endedID $expect_out(spawn_id)
set donepos [lsearch $idlist $endedID]
set idlist [lreplace $idlist $donepos $donepos]
incr runningcount -1
set elapsedTime [clock format [expr [clock seconds] - $timingInfo($endedID)] -format "%M:%S (MM:SS)"]
send_user " $commandInfo($endedID) - finished in: $elapsedTime\n"
# If there are more commands to execute then do it!
if {[llength $commands] > 0} {
set command [lindex $commands 0]
eval spawn $command
lappend idlist $spawn_id
set commands [lreplace $commands 0 0]
incr runningcount
set commandInfo($spawn_id) $command
set timingInfo($spawn_id) [clock seconds]
}
}
timeout {
break
}
}
}
set elapsed_time [clock format [expr [clock seconds] - $timingInfo("MultipleProcesses")] -format "%M:%S (MM:SS)"]
send_user "$argv0 $argc - finished in: $elapsedTime\n"
multiple.config
# The dir from where the commands are executed.
set commandDir "/home/username/scripts/expect/";
set commandName "somecommand.exp";
# The maximum number of simultanious spawned processes.
set maxSpawn 40;
# The maximum timeout in seconds before any of the processes should be finished in minutes
set timeout 20800;
关于bash - 尝试登录到它生成的 ssh session 时如何期望超时?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7011732/
我在远程机器上导航基于 Java 的 CLI 菜单,并在 bash 脚本中使用 expect,我试图从输出中提取某些内容而不离开 expect session 。 我的脚本中的 Expect 命令是:
我正在尝试使用 expect.h header 编译用 c 编写的程序。我正在尝试这个: cc -I/usr/include main.c -lexpect -ltcl cc -I/usr/inclu
我正在使用Expect与SSH session 和ERP程序进行自动交互。 而不是依靠正则表达式来捕获我期望脚本中的变量,是否有可能在收到用户的特定击键后将屏幕区域(例如一个字段)捕获为代码中的变量?
我是 PHP 面向对象编程的新手。我有个问题。我写了一个代码,但它不起作用。我知道这很容易,但我想知道它有什么问题。我出现以下错误: 当我尝试在另一个文件中使用它时,我现在遇到了这个错误:( 最佳答
声明了哪些出现了前所未见的错误,并试图找到解决方案。与以前的程序一样奇怪,它使用相同的语法但不会抛出任何错误 这是一个使用游标从表中检索信息,然后将其插入到另一个表中的过程,这样做是为了可以使用其中的
我已经用 CASE 编写了一个查询,但遇到了 () 问题。 select SM.subscriber_name as name , SM.accountType as accountTy
这个问题在这里已经有了答案: Why does removing return give me an error: expected type `()` but found type (1 个回答)
我有一个脚本可以登录服务器并执行一些命令。我需要能够从每个命令中检索返回代码,以确定脚本是否成功。我写了以下脚本,但没有按照我的意愿进行。目标是执行“cd/I/dont/exist”,这会产生错误代码
关闭。 这个问题需要 debugging details 。它目前不接受答案。 编辑问题以包含 desired behavior, a specific problem or error, and
我正在运行一个 expect 脚本,它将生成一些来自 stdin 的动态输入。 是否有一种方法/模式可以解决从标准输入读取并将相关输入存储(?)到某处以在后面的步骤中处理/解析的概念? 示例: ./m
我正在运行一个 expect 脚本,该脚本在远程机器上调用多个脚本。这些 shell 脚本返回颜色输出(主要是红色和绿色)。问题是,那些颜色代码进入了我不想要的 log_file 和 STDOUT。我
我正在开发一个脚本,用于对软件安装进行回归测试。期望代码如下。前几行代码在浏览并同意许可证文件的地方运行良好。但是,脚本在“请输入有效许可证文件的路径名:”处停止,并且不执行任何操作。 (注意:手动安
我们创建以下简单的 expect 脚本以运行 netdata-installer.sh 预期脚本是: #!/usr/bin/expect set timeout 20 send "cd /tmp/ne
有人有T_PAAMAYIM_NEKUDOTAYIM吗? 最佳答案 是双冒号运算符 :: (见 list of parser tokens)。 关于PHP 期望 T_PAAMAYIM_NEKUDOTAY
我正在使用 Vercel SWR Hook usrSWR,我希望我可以将数据存储在某个遥远组件的缓存中,而不必使用上下文或其他一些全局状态管理器。 具体来说,我在 IndexPage 中使用 init
我刚刚注意到,如果我添加 if,Spock 不会断言条件。预期块中的子句,如 def myTest() { given: a = true expect: if ( a ) {
我有一个这样的方法: getValues(...args: Array) : Array { return args.map(k => { return this.shared
我正在使用 typescript + jest,并且在创建模拟实现时遇到了一些类型检查问题。例如,我想模拟 Credentials来自 aws-sdk 的对象: import { Credential
我依赖于一个以 Map 作为参数的方法。 public interface Service { void doSomething(Map map); } 我想写一个断言,用适当的 map 内容
我有一个适配器,它有一个方法,它采用可变参数列表,并将其转发给一个在我使用的框架中采用相同参数的方法。我想测试我的适配器是否正确转发了参数。然而,我不希望我的测试知道框架支持哪种参数。 我有一个工作期
我是一名优秀的程序员,十分优秀!