Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Read file into String and do a loop in Expect Script

What I am trying to do is to:

  1. Create a .exp file, which will read from the *.txt file from the same directory and parse all the content in the text file into a string variable in the expect script.
  2. Loop the string, which contains a series of host names, and excecute a series of command until the string is enumerated.

So what the script does, is read a series of hostname from a txt file in the same directory, and then read them into a string, and the .exp file will auto log into each of them and excecute a series of commands.

I have the following code written but it's not working:

#!/usr/bin/expect

set timeout 20
set user test
set password test

set fp [open ./*.txt r]
set scp [read -nonewline $fp]
close $fp

spawn ssh $user@$host

expect "password"
send "$password\r"

expect "host1"
send "$scp\r"

expect "host1"
send "exit\r"

Any help is greatly appreciated....

like image 669
Tony Avatar asked Jul 15 '13 19:07

Tony


People also ask

How does expect work in Python script?

It expects specific string, and sends (or responds) strings accordingly. If you are new to expect, read our 6 expect script examples (including hello world example) to get a jump start. This article explains the following in the expect scripting language. Note: For expect command line arguments, read 6 Expect Script Command Line Argument Examples.

What is a read while loop in C++?

In the while loop, if we have to read a file, we would need to use a keyword read and hence this while is known as read while loop. The main charismatic property is the ability to read line by line when you use the keyword read.

How many expect script examples are there?

If you are new to expect, read our 6 expect script examples (including hello world example) to get a jump start. This article explains the following in the expect scripting language. Note: For expect command line arguments, read 6 Expect Script Command Line Argument Examples.

How to read a shell script file in Linux?

In reading a shell script file we first analyze and confirm if the file has read privileges or not. In case the privilege is present, we would start reading the file. In reading there are various ways possible. Either we can read the file character by character, word by word or line by line.


2 Answers

The code should read the contents of the two files into lists of lines, then iterate over them. It ends up like this:

# Set up various other variables here ($user, $password)

# Get the list of hosts, one per line #####
set f [open "host.txt"]
set hosts [split [read $f] "\n"]
close $f

# Get the commands to run, one per line
set f [open "commands.txt"]
set commands [split [read $f] "\n"]
close $f

# Iterate over the hosts
foreach host $hosts {
    spawn ssh $user@host
    expect "password:"
    send "$password\r"

    # Iterate over the commands
    foreach cmd $commands {
        expect "% "
        send "$cmd\r"
    }

    # Tidy up
    expect "% "
    send "exit\r"
    expect eof
    close
}

You could refactor this a bit with a worker procedure or two, but that's the basic idea.

like image 68
Donal Fellows Avatar answered Nov 07 '22 07:11

Donal Fellows


I'd refactor a bit:

#!/usr/bin/expect

set timeout 20
set user test
set password test

proc check_host {hostname} {
    global user passwordt

    spawn ssh $user@$hostname
    expect "password"
    send "$password\r"
    expect "% "                ;# adjust to suit the prompt accordingly
    send "some command\r"
    expect "% "                ;# adjust to suit the prompt accordingly
    send "exit\r"
    expect eof
}

set fp [open commands.txt r]
while {[gets $fp line] != -1} {
    check_host $line
}
close $fp
like image 38
glenn jackman Avatar answered Nov 07 '22 07:11

glenn jackman