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

标签 bash ssh timeout expect

我正在编写一个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 脚本。

然后对于期望部分: 您可以通过使用也与超时匹配的开关来做到这一点(探索期望的第 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"

多个.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/

相关文章:

linux - 我可以提供我的裸 git 存储库吗?

linux - 了解虚拟机 IP 地址 ????基本?

ruby-on-rails - ruby 2.1.2 超时仍然不是线程安全的吗?

timeout - 尝试重新启动 mysqld 后无法再启动它

Linux:将文件上传到实时服务器 - 如何自动化流程?

linux - 后台进程日志显示消息时间戳存在很大差距

bash - 删除行尾并且没有新段落

linux - 如何从 linux shell 循环遍历字符串以获得模式?

windows - 帮助处理 Cygwin bash 文件