nex.cz
nex.cz

Reputation: 154

Expect script with variable hostname

I would like to use EXPECT script for mount volume after reboot tens of our UBUNTU servers (12.04 & 14.04).

I am not able to figure out how set hostname like variable (for example to grab record from /etc/hostname ) -> I dont wanna create one script (set specific hostname) for each server. My idea is use this script global for my environment.

Following script works fine - but there is set fix hostname

#!/usr/bin/expect -f

set timeout 90

set sudo_pass "Password01
set mount_pass "Password02"
set hostname "test-staging"          <<<==== here I set static hostname

# turns off the output to STDOUT
log_user 0

# run mount-script
spawn sudo mount-script

# type sudo pass
expect "password for"
send "$sudo_pass\r"

# type Pass for mount-script
expect "Enter passphrase"
send "$mount_pass\r"

# call & paste hostname
expect "Please provide a hostname for this instance,"
send "$hostname\r"

# type letter Y - for test environment
expect "Is this a test instance?  Enter 'Y', otherwise press enter"
send "Y\r"

interact

I tried to google it I found commands for hostname like

send 'lsnrctl status listener_`hostname`\r'

or

send "cat /etc/rc.d/rc.local |grep hostname \r\n"

I tried set them like SET HOSTNAME and I tried call them via command send. Unfortunately, it doesn't work, too.

this is output

    .......    
    dbg1.7>
    1: expect "Please provide a hostname for this instance
    dbg1.8>
    Please provide a hostname for this instance
    1: send 'lsnrctl status listener_`hostname`\r'

    dbg1.9>
    usage: send [args] string
        while executing
    "send 'lsnrctl status listener_`hostname`\r'"
        (file "test01-expect.sh" line 38)
    1: exit 1

I am open mind for different solution, too. thank you

Upvotes: 0

Views: 3174

Answers (1)

Dinesh
Dinesh

Reputation: 16438

To get the host name of machine, can't you use the terminal command hostname ?

Tcl has a command exec which will execute the command and will return the result.

set host_name [exec hostname]

or, equivalently,

set host_name [ exec cat /etc/hostname]

Instead of saving it into a variable, this can be directly even used in the send command as,

expect "Please provide a hostname for this instance,"
send "[exec hostname]\r"

Reference : exec

Upvotes: 2

Related Questions