Saturday, August 4, 2012

A tale of two scripts

One of the cool things about expect-lite is that it has a very simple interpreter, which ignores everything it doesn't understand, rather than giving a syntax error. You can paste just about any text into an expect-lite script, such as release notes, network diagrams, or sample output from commands, and have it ignored. This feature can be exploited.

In this post, I will embed a bash script inside an expect-lite script. I'll use the bash script to set up the environment, and then have the script call itself, this time using expect-lite as the interpreter. Since expect-lite ignores anything it doesn't know, it will ignore the embedded bash script.

Here's the script, bash_env.elt (with 755 permissions)
#!/usr/bin/env bash 
# Little bootstrap bash script to adjust env then run expect-lite

export EL_CONNECT_USER=user
export EL_CONNECT_PASS=secret
export EL_CONNECT_METHOD=telnet

# re-run the script as an expect-lite script
/usr/bin/env expect-lite c=$0 $*

# end the bash script, and return expect-lite pass/fail
exit $?

#start the expect-lite script
$myvar=hello world

>echo "$myvar"

Run the script:

The script starts out like any bash script, and then sets (and exports) some environment variables which control how expect-lite connects to a remote host. Then the bash starts expect-lite feeding it the script name ($0) and any parameters that might have been typed on the command line ($*). Finally, the bash script exits (exit $?) preventing bash from reading the rest of the script.

The final part of the script is expect-lite, which will be executed after expect-lite telnets to, and logs in with credentials user and secret.

Of course, you don't want to hard code these parameters for every script. After all, you may use telnet as your connect method today, and change to ssh later.

Embedding other scripts inside expect-lite scripts might just solve your challenging problem.

No comments:

Post a Comment