gpt4 book ai didi

bash - 尝试登录到它生成的 ssh session 时如何期望超时?

转载 作者:行者123 更新时间:2023-11-29 09:49:24 25 4
gpt4 key购买 nike

我正在编写一个 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com