如何让 Expect 等待另一个脚本完成

如何让 Expect 等待另一个脚本完成

我有 3 个脚本,这是主要的:

#!/usr/bin/expect -f
#!/bin/sh
set DATE [exec date +%F]
set IP  "148.000.000.101"
set Username "user" 
set Password "pass"
set Password_sql "sqlpass"
spawn ssh -p 22 mycomputer@localhost
expect "*?"
send "yes\r"
expect "password: "
send "passlocal\r"
expect "$ "
send "telnet $IP\r"
expect "Username:"
send "$Username\r"
expect "Password: "
send "$Password\r"
expect "*>"
send "show cdp neighbors detail\r"
log_file -noappend CDPdet.dat;
expect -ex "--More--" {send -- " "; exp_continue}
expect "*>"
log_file;
expect "*>"
send "exit\r"
expect "$ "
send -- "awk '/Device ID|IP address|Interface|Port ID/ { print }' CDPdet.dat >tabladetallada.dat\r"
expect "$ "
send -- "sed 's/--More--␣*//' tabladetallada.dat>tabladetallada2.dat \r"
expect "$ "
send -- "sed -i '$ s/.$//' Disptelnet.dat\r"
expect "$ "
send -- " echo \"\nIP address: $IP\" >>Disptelnet.dat\r"
expect "$ "
send -- "dos2unix Disptelnet.dat \r"
expect "$ "
send -- "dos2unix dispositivos.dat \r"
expect "$ "
send -- "awk '\r    BEGIN {\r        RS = \"\\n\\n\"\r        FS = \"\\n\"\r        OFS = \",\"\r        print \"device_id,ip_address\"\r    }\r    {\r        for(i=1; i<=NF; i++) {\r            split(\$i, a, \":\");\r            k\[a\[1\]\] = a\[2\]\r        }\r            print k\[\"Device ID\"\], k\[\"IP address\"\]\r\r    }' dispositivos.dat>dispositivoss.csv \r"
expect "$ "
send -- "./telnetverison.sh \r"
expect "$ "

一切顺利,但是当 ./telnetverison.sh 运行时,主脚本不会等到“telnetverison.sh”完成。

我已经单独测试了 telnetverison.sh 并且它工作正常,这是代码:

#!/bin/bash

FILE1=dispositivoss.csv
set Username "user" 
set Password "pass"


NUMERODISP="$(wc -l $FILE1 | awk '{print $1}')"

# echo "$NUMERODISP"

num=3
IP="$(awk -vnum="$num" 'NR == num { print $NF }' dispositivoss.csv)"

#echo "$IP"


for i in `seq 2 $NUMERODISP`;
        do
    IP="$(awk -vnum="$i" 'NR == num { print $NF }' dispositivoss.csv)"
    expect -f conexionindividual.expect $IP $i
    done    

正如您所看到的,此代码中嵌套了另一个脚本,以防万一这是 conexionindividual.expect 的代码:

#!/usr/bin/expect -f
set Username "user" 
set Password "pass"
set IP   [lindex $argv 0];
set i [lindex $argv 1];
spawn ssh -p 22 mycomputer@localhost
expect "*?"
send "yes\r"
expect "password: "
send "mypass\r"
expect "$ "
send "telnet $IP\r"
expect "Username:"
send "$Username\r"
expect "Password: "
send "$Password\r"
expect "*>"
send "show version\r"
log_file -noappend SN_$IP.dat;
expect -ex "--More--" {send -- " "; exp_continue}
expect "*>"
log_file;

这是我运行主脚本时终端检索到的内容:

cesar@cesar-HP-Pavilion-15-NoteBook-PC:~$ awk '

...
>     BEGIN {                         #this is the last awk in the "main" script
>         RS = "\n\n"
./telnetverison.sh 
>         FS = "\n"
>         OFS = ","
>         print "device_id,ip_address"
>     }
>     {
>         for(i=1; i<=NF; i++) {
>             split($i, a, ":");
>             k[a[1]] = a[2]
>         }
>             print k["Device ID"], k["IP address"]
> 
>     }' dispositivos.dat>dispositivoss.csv 
cesar@cesar-HP-Pavilion-15-NoteBook-PC:~$ ./tecesar@cesar-HP-Pavilion-15-NoteBook-PC:~$  #here is the problem

你可以帮帮我吗?

提前致谢。

更新:已解决!我在发送后放置一个睡眠:

expect "$ "
send -- "./telnetverison.sh \r"
sleep 400
expect "$ "

但 400 秒还不够,具体取决于设备的数量。

还有其他想法吗?

答案1

在调用该脚本之前,关闭 Expect 的超时

set timeout -1
send -- "./telnetverison.sh \r"
expect "$ "

调用sleepExpect 脚本会产生代码味道。

相关内容