Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I want to write a login script in expect. But I want this to be reused in different other scripts. I want to make all the login commands part of a bash subroutine. ie instead of
#!/bin/usr/expect -f
spawn ....
set ....

I want this:

    # put some necessary command to initiate expect program

    spawn ...
    set ...

so I would like to place this subroutine in one file/library that would be reused by many different scripts.

How can I do that?


PS: Pardon my imprecise syntax of bash/expect. I just wanted to write in a pseudocode manner.

share|improve this question
Do you want the login to stay in effect after the expect portion runs so that Bash commands can operate over the connection? You'd be better off doing everything in Expect. – Dennis Williamson Sep 12 '13 at 2:08
Yeah I thought about that option too. That would be a feasible solution. But there is one scenario - the script will be used to access the iLO4 and read the different pieces of information. Now I can process all the information in the expect script. But what if another bash script is out there which needs to gain the same pieces of information? – user285825 Sep 12 '13 at 2:34
The expect script can pass the information back to the Bash script. The problem with trying to have the Bash script use an Expect script (or within a Bash function) is that Expect doesn't establish a persistent session and Bash has no concept of such things. At least both are true when qualified with the word "easily". I'm assuming iLO4 is HP's Integrated Lights Out system management feature - with which I'm unfamiliar. Don't they provide a protocol or API that you can query without having to use Expect? – Dennis Williamson Sep 12 '13 at 2:59
Yeah you are right - they support the ipmitool. But the problem is ipmitool is generic which works across all vendors; while HP has specific CLP commands to get their specific features exclusive to iLO. Hence using ipmitool I can't obtain all the HP-specific information. – user285825 Sep 12 '13 at 22:54
I would recommend that you ask separate, specific questions about problems with what you're trying to accomplish. The question as it stands now is overly broad and vague and too much like an XY Problem. – Dennis Williamson Sep 12 '13 at 23:55

I would go for 2 part solution. One part is the expect script, the other part is the Shell script.

For the expect script, it should be a generic script that accept input, and produce output.

This is my example expect script that accept hostname and password, and will produce the vcprofile name for the server

[user@server ~]$ cat getvcprofile.expect

set timeout 2

set host [lindex $argv 0]

set password [lindex $argv 1]

spawn ssh "ADMIN\@$host"

expect_after eof { exit 0 }

expect  "yes/no" { send "yes\r" }

expect  "assword" { send "$password\r" }

expect "oa>" { send "show vcmode\r" }

expect "oa>" { send "exit\r" }


In the shell script, I will call the expect script and providing it with the variable, in this case the hostname for vcsystem. The password is actually a pattern according to the hostname OA@XXXX - where the XXXX is the last 4 digit number of the server

[user@server ~]$ cat

# get VC profile for a host


#get the blade enclosure
enclosure=`callsub -enc $host |grep Enclosure: | cut -d" " -f2`

if [ ! -z $enclosure ]; then

#get the last 4 digit of the enclosure
fourdigit=${enclosure: -4}

domain=`./getvcprofile.expect ${enclosure}oa OA@${fourdigit} |grep Domain|awk '{print $NF}'`
echo $domain


echo "None"


With this 2 part solution, I can do something like this:

for X in `cat serverlist.txt`; do echo -n $X": "; ./ $X; done 

And it will print out the vcprofile for each of the servers in the file serverlist.txt

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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