TransWikia.com

Unix shell script using Expect shell

Unix & Linux Asked by satsensort on December 1, 2021

Im writing a script to collect micro code levels for adapters on each servers in AIX, using Expect shell.

#!/bin/ksh
for hostname in ABCD123 ABCD234 ABCD445
do
expect << 'EOS'
set hos {$hostname}
spawn ssh padmin@$hostname
expect "Password:"
send "ABC1234n"
expect "$"
send "oem_setup_envn"
expect "#"
send "lsmcode -A | sed -e 's/^/$hos: /'n"
expect "#"
send "exitn"
expect "$"
send "exitn"
EOS
done

But unfortunately the script fails to run. Can you please help me

Output should be like this

ABCD123 : sys0!system:CL320_040 (t) CL320_040 (p) CL320_040 (t)
ABCD123 : ent0!14101103.CN0110
ABCD123 : ent1!14101103.CN0110
ABCD123 : ent2!14101103.CN0110
ABCD123 : ent3!14101103.CN0110
ABCD123 : ent4!14108802.DV0210

One Answer

Because you are using a quoted heredoc, the shell does not expand the shell variable $hostname. Do this: pass the variable to expect via the environment

export hostname
for hostname in ABCD123 ABCD234 ABCD445
do
    expect << 'EOS'
        set hos $env(hostname) ;# access the environment variable
        spawn ssh padmin@$hos
        expect "Password:"
        send "ABC1234r"
        expect "$"
        send "oem_setup_envr"
        expect "#"
        send "lsmcode -A | sed -e 's/^/$hos: /'r"
        expect "#"
        send "exitr"
        expect "$"
        send "exitr"
        expect eof             ;# wait for the connection to close
EOS
done

Idiomatically, you use r to "hit enter" for the send commands.

Answered by glenn jackman on December 1, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP