尝试登录到它产生的 ssh 会话时如何预期超时?

dun*_*nxd 2 ssh bash 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
Run Code Online (Sandbox Code Playgroud)

我想我可以设置超时,expect "assword:"但我不知道如何让它关闭生成的 ssh 会话,然后移动到 for 列表中的下一个 ASA。

San*_*erg 5

首先,我会为此使用一个期望脚本并丢失 bash 脚本。

然后对于期望部分:您可以通过使用也匹配超时的开关来做到这一点(探索期望的第 12 页)。通过这种方式,您可以在预期超时时明确采取某些行动。

否则,通过设置超时,它将继续执行下一个命令。

set timeout 60
expect {
 "assword:" {
 }
 timeout {
    exit 1 # to exit the expect part of the script
 }
}
Run Code Online (Sandbox Code Playgroud)

我已经创建了类似的东西,我使用了一个整体的期望脚本来并行运行一个期望脚本。

多个.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"
Run Code Online (Sandbox Code Playgroud)

多重配置

# 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;
Run Code Online (Sandbox Code Playgroud)