Expect scripting.

I was wondering if I could do this a bit better.

another script calls this script and logs into a device, does the relavant "show commands" and then saves those to a file.

My problem is that the buffer isn't large enough to the output of some of the commands.

here is an excerpt of what i'm doing below.. there is an additional command "sho config" that outputs up to 2 meg files. when this program runs, it does 700+ devices and the program takes up a lot of space it seems like when running

#!/usr/local/bin/expect -f
# Set variables
set ip [lindex $argv 0]
set username $env(USER)
set password [lindex $argv 1]
set enablepassword [lindex $argv 1]
set hostname [lindex $argv 2]
set date [clock format [clock seconds] -format {%Y-%m-%d}]
set dir DeviceLog

#don't log ssh sessions to screen
log_user 0
# Announce which device we are working on and at what time
send_user ">>>>>  Working on $ip @ [exec date] <<<<<\n"

# Don't check keys
spawn  ssh -o StrictHostKeyChecking=no $username\@$ip

# Allow this script to handle ssh connection issues
expect {
  timeout { send_user "Timeout Exceeded - Check Host\n";
                set dir [concat "$dir/connectionIssues.$date"]
                set log [open "$dir" "a"]
                puts $log "TIMEOUT: $ip hostname: $hostname"
        exit 1 }
  eof {
        set dir [concat "$dir/connectionIssues.$date"]
        set log [open "$dir" "a"]
        puts $log "Hostname: $hostname : $expect_out(buffer)"
        send_user $expect_out(buffer)
         exit 1
         }
  "*#" {}
  "*assword:" {send "$password\n"}
}

# If we're not already in enable mode, get us there
expect {
  default {
        send_user "Password fail for $hostname: $ip\n"
        set dir [concat "$dir/passwordFail.$date"]
        set passfail [open "$dir" "a"]
        puts $passfail "Invalid password for $ip : $hostname"
        exit 1
             }

  "*#" {}
  "*>" {
        send "en\n"
        expect "*assword"
        send "$password\n"
        # If we get a pound sign, indicating we're in priv exec, expect will attempt to run all
 the command below
        expect "*#"
        }
}

#Set file name then send, expect, write

#increase buffer size
match_max 400000

##########################################################
set interfaces [open "./Interfaces/$hostname.$date" "a"]
##########################################################

send "terminal length 0\n"
expect "*#"

send "show run | i hostname\n"
expect -re "hostname (.*)\n"
set host $expect_out(1,string)
puts $interfaces $expect_out(buffer)
expect "*#"

send "show int status\n"
expect "*#"
puts $interfaces $expect_out(buffer)

send "show version | i evision\n"
expect "*#"
puts $interfaces $expect_out(buffer)

####################################################
set power [open "./Power/$hostname.$date" "a"]
####################################################

send "show power\n"
expect "*#"
puts $power $expect_out(buffer)

send "show snmp location\n"
expect "*#"
puts $power $expect_out(buffer)

send "show snmp contact\n"
expect "*#"
puts $power $expect_out(buffer)

send "show idprom chassis\n"
expect "*#"
puts $power $expect_out(buffer)

send "show idprom backplane\n"
expect "*#"
puts $power $expect_out(buffer)

send "show idprom power-supply 1\n"
expect "*#"
puts $power $expect_out(buffer)

send "show idprom power-supply 2\n"
expect "*#"
puts $power $expect_out(buffer)