0

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 "ABC1234\n"
expect "$"
send "oem_setup_env\n"
expect "#"
send "lsmcode -A | sed -e 's/^/$hos: /'\n"
expect "#"
send "exit\n"
expect "$"
send "exit\n"
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
  • for hostname in ABCD123 ABCD234 ABCD445 do expect << 'EOS' set hos {$hostname} spawn ssh padmin@$hostname expect "Password:" send "ABC1234\n" expect "$" send "oem_setup_env\n" expect "#" send "lsmcode -A | sed -e 's/^/$hos: /'\n" expect "#" send "exit\n" expect "$" send "exit\n" EOS done – satsensort May 23 '18 at 9:19
  • take a look at sexpect with which you can write "Expect" scripts with only shell code. – pynexj May 23 '18 at 10:11
0

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 "ABC1234\r"
        expect "$"
        send "oem_setup_env\r"
        expect "#"
        send "lsmcode -A | sed -e 's/^/$hos: /'\r"
        expect "#"
        send "exit\r"
        expect "$"
        send "exit\r"
        expect eof             ;# wait for the connection to close
EOS
done

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

  • it didnt pick up that variable could you advise if any possibility.can't read "env(hostname)": no such variable while executing – satsensort May 24 '18 at 8:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.