退出状态code表示期待脚本称为猛砸 [英] Exit status code for Expect script called from Bash

查看:131
本文介绍了退出状态code表示期待脚本称为猛砸的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我做它使用一个期望脚本来自动SSH logins.The脚本连接到多台服务器,并运行一些命令的Bash脚本。
在bash脚本提示输入登录凭据一次。

I made a Bash script which uses an expect script to automate ssh logins.The script connects to multiple servers and runs some commands. The bash script prompts for login credentials once.

我想,其中,如果登录失败的第一服务器以避免该脚本检查导致用户帐户下服务器被锁定脚本终止掺入功能。帐户锁定发生3连续登录失败和服务器的脚本尝试连接的数量超过3。

I want to incorporate a feature wherein the script terminates if the login fails for the first server to avoid the script checking for next servers resulting in the user account getting locked. The account lockout happens for 3 consecutive login failures and the number of server the script tries to connect is more than 3.

这是在bash脚本会调用expect脚本片段。

This is the snippet in the bash script which calls the expect script.

countu=0
for servername in $(cat $linux_host_list)
do
./script.expect $LUSERNAME $LPASS $servername Linux >> linux_log_file.txt & < /dev/null
let countl=countl+1
done

和这里是期待脚本( script.expect )段

and here is the expect script (script.expect) snippet

#!/usr/bin/expect -f
set timeout 30
set username [lindex $argv 0]
set SPASS [lindex $argv 1]
set servername [lindex $argv 2]
set case [lindex $argv 3]
set prompt "(%|#|\\$|%\]) $"
switch $case {
    Linux {
        log_user 0
        spawn ssh -o StrictHostKeyChecking=no $username@$servername
        expect  {
            "assword:" {
                send "$SPASS\r"
                expect -re "$prompt"
            }
            expect -re "$prompt"
        }
        send "sudo su -\r"
        expect {
            "assword:" { send "$SPASS\r" }
        }
        expect -re "$prompt"
        log_user 1
        send "opcagt -status ; opctemplate -l ; cat watch.cf 2> /dev/null\r"
        expect -re "$prompt"
        log_user 0
        send "exit\r"
        expect -re "$prompt"
        log_user 1
    }

我试图抓住bash命令输出( $?)假设,如果登录的期望脚本,但对于不正确的密码无法bash命令将返回一个非零值没有发挥出来。
任何建议将是多少AP preciated。

I tried grabbing the bash command output ($?) assuming that the bash command would return a non zero value if login fails for incorrect password in the expect script but that did not work out. Any suggestions would be much appreciated.

推荐答案

有关错误在expect脚本检查,有一个在的 http://systeminetwork.com/article/handle-errors-expect-scripts

For error checking in an expect script, there's a decent example at http://systeminetwork.com/article/handle-errors-expect-scripts

你应该做的是这样的:

proc do_exit {msg} {
    puts stderr $msg
    exit 1
}

switch -exact -- $case {
    Linux {
        spawn ssh ...
        expect {
            -re {assword: $} {
                send -- "$SPASS\r"
                exp_continue 
                # remain in this expect block and look for the next matching pattern
            }
            "some message about incorrect password" {
                do_exit "incorrect password"
            }
            timeout {do_exit "timed out waiting for prompt"}
            default {do_exit "something else happened"}
            -re $prompt
        }
        # ... rest of your script
    }
}

我假设你并不需要了解的退出状态opcagt ......一系列命令(你只是想看看watch.cf文件的内容。如果你做护理,你会需要得到shell来告诉你:

I assume you don't need to know about the exit status of the "opcagt ..." series of commands (you just want to see the contents of the watch.cf file. If you do care, you'll need to get the shell to tell you:

send -- "opcagt -status 2>&1 || echo "non-zero return from opcagt: $?"
expect {
    "non-zero return" { handle error and exit? }
    -re $prompt
}
# ... continue

这篇关于退出状态code表示期待脚本称为猛砸的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆