別のスクリプトが完了するまで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 "$ "

Expectスクリプトを呼び出すと、sleepコードの臭いが発生する可能性があります。

おすすめ記事