3

I've got 3 scripts, this is the main one:

#!/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 "$ "

everything goes well, but when ./telnetverison.sh runs, the main script doesn't wait until "telnetverison.sh" finish.

I've tested telnetverison.sh separately and it's working fine, this is the code:

#!/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    

As you can see, there is another script nested in this code, just in case this is conexionindividual.expect's code:

#!/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;

this is what the terminal retrieves when I run the main script:

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

Could you help me?

Thanks in advance.

Update: solved! I place an sleep after send:

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

But 400 sec couldn't be enough depending of the number of devices.

Any other idea?

1 Answer 1

5

Before you call that script, turn off expect's time-out

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

Calling sleep in an expect script is a code smell.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .