1
votes

I have a simple BASH script I use that sends a .esh script to a remote host. For example, I know what the username and password is for the host, so my script does the following:

expect login:
send root\n
expect Password:
send abc123\n
expect "# "

So, this will automatically log me into the remote host as user root with password abc123.

I am now attempting to send a BASH-style one-line for loop to pull some metrics from some log files on the host, ie:

send 'for i in /var/log/example/*.txt; do echo $i; cat $i | grep error ; done \n'

However, this fails with:

# can't read "i": no such variable
    while executing
send 'for i in /var/log/example/*.txt; do echo $i; cat $i | grep error ; done \n'

I've read some similar posts, but I haven't found any case similar to this. Once I've figured out how to implement this, I will likely include more command-line commands to refine the output of the log files (ie: more bash temporary variables, pipe the output through awk, etc).

Can anyone help me resolve this issue?

Thank you.


EDIT: As per the provided answer, I changed my script to the following, and it worked like a charm:

expect login:
send root\n
expect Password:
send abc123\n
expect "# "
send { for i in /var/log/example/*.txt; do echo $i; cat $i | grep error ; done }
send "\n"
send exit

This logs into the remote host, prints some valuable metrics to the console, then quits telnet. I can now use a bash script on my local host to loop through multiple remote hosts via a text file containing a list of hostnames/IPs.

Thanks!

1
technically you should send with \r instead of \n -- \r is carriage return, I.e. "hitting enter" - glenn jackman

1 Answers

2
votes

Single quotes aren't used for quoting in Tcl, they're normal characters. Use double quotes if you want backslash, command and variable expansion, and curly braces if you don't.

Replace the first ' with { and the last with }.