Next Previous Contents

8. Anexo: ejemplos de programas Expect

8.1 ftp_1.exp


#!/usr/bin/expect -f
#
set force_conservative 0  ;# set to 1 to force conservative mode even if
                          ;# script wasn't run conservatively originally
if {$force_conservative} {
        set send_slow {1 .001}
        proc send {ignore arg} {
                sleep .1
                exp_send -s -- $arg
        }
}
set timeout 2
puts "\n"
#
spawn ftp localhost 
expect -exact "Name (localhost:root): "
send -- "ftp\r"
expect "Password:"
send -- "barreiro@arrakis.es\r"
expect -exact "ftp> "
send -- "binary\r"
expect -exact "ftp> "
send -- "hash\r"
expect -exact "ftp> "
send -- "cd pub/spain/gnome\r"
log_user 1
expect -exact "ftp> "
send -- "ls\r"

puts "\n"
exit

8.2 ftp_21.exp


#!/usr/bin/expect -f
#
set force_conservative 1  ;# set to 1 to force conservative mode even if
                          ;# script wasn't run conservatively originally
if {$force_conservative} {
        set send_slow {1 .001}
        proc send {ignore arg} {
                sleep .1
                exp_send -s -- $arg
        }
}
set timeout 2
puts "\n"
#
spawn ftp ftp.desconocido.es
expect -exact "Name (localhost:root): "
send -- "ftp\r"
expect "Password:"
send -- "barreiro@arrakis.es\r"
expect -exact "ftp> "
send -- "binary\r"
expect -exact "ftp> "
send -- "hash\r"
expect -exact "ftp> "
send -- "cd pub/spain/gnome\r"
expect -exact "ftp> "
send -- "ls\r"

puts "\n"
exit

8.3 ftp_22.exp


#!/usr/bin/expect -f
#
set force_conservative 1  ;# set to 1 to force conservative mode even if
                          ;# script wasn't run conservatively originally
if {$force_conservative} {
        set send_slow {1 .001}
        proc send {ignore arg} {
                sleep .1
                exp_send -s -- $arg
        }
}
set timeout 2
puts "\n"
#
spawn ftp 12.0.0.2
expect -exact "Name (localhost:root): "
send -- "ftp\r"
expect "Password:"
send -- "barreiro@arrakis.es\r"
expect -exact "ftp> "
send -- "binary\r"
expect -exact "ftp> "
send -- "hash\r"
expect -exact "ftp> "
send -- "cd pub/spain/gnome\r"
expect -exact "ftp> "
send -- "ls\r"

puts "\n"
exit

8.4 ftp_31.exp


#!/usr/bin/expect -f
#
set force_conservative 1  ;# set to 1 to force conservative mode even if
                          ;# script wasn't run conservatively originally
if {$force_conservative} {
        set send_slow {1 .001}
        proc send {ignore arg} {
                sleep .1
                exp_send -s -- $arg
        }
}
set timeout 2
puts "\n"
#
spawn ftp ftp.desconocido.es
expect {
 -exact "conection refused" {puts "Conexion denegada\n";exit}
 -exact "Host name lookup failure" {puts "Servidor desconocido\n";exit}
 -exact "Name (localhost:root):*"

}
send -- "ftp\r"
expect "Password:"
send -- "barreiro@arrakis.es\r"
expect -exact "ftp> "
send -- "binary\r"
expect -exact "ftp> "
send -- "hash\r"
expect -exact "ftp> "
send -- "cd pub/spain/gnome\r"
expect -exact "ftp> "
send -- "ls\r"

puts "\n"
exit

8.5 ftp_32.exp


#!/usr/bin/expect -f
#
set force_conservative 1  ;# set to 1 to force conservative mode even if
                          ;# script wasn't run conservatively originally
if {$force_conservative} {
        set send_slow {1 .001}
        proc send {ignore arg} {
                sleep .1
                exp_send -s -- $arg
        }
}
set timeout 2
puts "\n"
#
spawn ftp 12.0.0.2
set timeout 10 
expect {
 timeout {puts "El comando ha temporizado\n";exit}
 -exact "conection refused" {puts "Conexion denegada\n";exit}
 -exact "Host name lookup failure" {puts "Servidor desconocido\n";exit}
 -exact "Name (localhost:root):*"

}
send -- "ftp\r"
set timeout 5
expect "Password:"
send -- "barreiro@arrakis.es\r"
expect -exact "ftp> "
send -- "binary\r"
expect -exact "ftp> "
send -- "hash\r"
expect -exact "ftp> "
send -- "cd pub/spain/gnome\r"
expect -exact "ftp> "
send -- "ls\r"

puts "\n"
exit

8.6 ftp_41.exp


#!/usr/bin/expect -f
#
set force_conservative 1  ;# set to 1 to force conservative mode even if
                          ;# script wasn't run conservatively originally
if {$force_conservative} {
        set send_slow {1 .001}
        proc send {ignore arg} {
                sleep .1
                exp_send -s -- $arg
        }
}
set timeout 2
puts "\n"
#
spawn ftp localhost 
expect "Name*:*"
send -- "ftp\r"
expect "assword:*"
send -- "barreiro@arrakis.es\r"
expect "ftp>*"
send -- "binary\r"
expect "ftp>*"
send -- "hash\r"
expect "ftp>*"
send -- "cd pub/spain/gnome\r"
expect "ftp>*"
send -- "ls\r"

puts "\n"
exit

8.7 ftp_51.exp


#!/usr/bin/expect -f
#
set force_conservative 1  ;# set to 1 to force conservative mode even if
                          ;# script wasn't run conservatively originally
if {$force_conservative} {
        set send_slow {1 .001}
        proc send {ignore arg} {
                sleep .1
                exp_send -s -- $arg
        }
}
puts "\n"
#
log_user 0
spawn ftp localhost 
expect "Name*:*"
send -- "ftp\r"
expect "assword:*"
send -- "barreiro@arrakis.es\r"
expect "ftp>*"
send -- "binary\r"
expect "ftp>*"
send -- "hash\r"
expect "ftp>*"
send -- "cd pub/spain/gnome\r"
log_user 1
expect "ftp>*"
send -- "ls\r"

puts "\n"
exit

8.8 ftp_52.exp


#!/usr/bin/expect -f
#
set force_conservative 1  ;# set to 1 to force conservative mode even if
                          ;# script wasn't run conservatively originally
if {$force_conservative} {
        set send_slow {1 .001}
        proc send {ignore arg} {
                sleep .1
                exp_send -s -- $arg
        }
}
set timeout 2
puts "\n"
#
log_user 0
spawn ftp localhost 
send_user "Conectando al servidor\n"
expect "Name*:*"
send -- "ftp\r"
expect "assword:*"
send -- "barreiro@arrakis.es\r"
expect "ftp>*"
send_user "Acceso concedido\n"
send -- "binary\r"
expect "ftp>*"
send -- "hash\r"
expect "ftp>*"
send_user "Cambiando al directorio elegido\n"
send -- "cd pub/spain/gnome\r"
log_user 1
expect "ftp>*"
send -- "ls\r"

puts "\n"
exit

8.9 ftp_61.exp


#!/usr/bin/expect -f
#
set force_conservative 1  ;# set to 1 to force conservative mode even if
                          ;# script wasn't run conservatively originally
if {$force_conservative} {
        set send_slow {1 .001}
        proc send {ignore arg} {
                sleep .1
                exp_send -s -- $arg
        }
}
set timeout 2
puts "\n"
#
log_user 0
if $argc<2 {
  send_user "$argv0: faltan parametros\n"
  exit
} 
set sistema [lindex $argv 0]
set usuario [lindex $argv 1]
send_user "$sistema\n"
send_user "$usuario\n"
send_user "[lindex $argv 0]\n"
send_user "[lindex $argv 1]\n"


spawn ftp $sistema
send_user "Conectando al servidor\n"
expect "Name*:*"
send -- "ftp\r"
expect "assword:*"
send -- "$usuario\r"
expect "ftp>*"
send_user "Acceso concedido\n"
send -- "binary\r"
expect "ftp>*"
send -- "hash\r"
expect "ftp>*"
send_user "Cambiando al directorio elegido\n"
send -- "cd pub/spain/gnome\r"
log_user 1
expect "ftp>*"
send -- "ls\r"

puts "\n"
exit

8.10 ftp_71.exp


#!/usr/bin/expect -f
#
set force_conservative 1  ;# set to 1 to force conservative mode even if
                          ;# script wasn't run conservatively originally
if {$force_conservative} {
        set send_slow {1 .001}
        proc send {ignore arg} {
                sleep .1
                exp_send -s -- $arg
        }
}
set timeout 2
puts "\n"
#
log_user 0
spawn ftp localhost 
expect "Name*:*"
send -- "ftp\r"
expect "assword:*"
send -- "barreiro@arrakis.es\r"
expect "ftp>*"
send -- "binary\r"
expect "ftp>*"
send -- "hash\r"
expect "ftp>*"
send -- "cd pub/spain/gnome\r"
log_user 1
expect "ftp>*"
interact "" return
send_user "Salgo de la zona interactiva\n"
send -- "cd ..\r"
expect -exact "ftp> "
send -- "ls\r"
expect -exact "ftp> "

puts "\n"
exit

8.11 telnet_gl.exp


#!/usr/bin/expect -f
set force_conservative 1  ;# set to 1 to force conservative mode even if
                          ;# script wasn't run conservatively originally
if {$force_conservative} {
        set send_slow {1 .001}
        proc send {ignore arg} {
                sleep .1
                exp_send -s -- $arg
        }
}
puts "\n"

spawn telnet localhost
expect "ogin:*"
send -- "mante\r"
expect "assword:*"
send -- "a12345\r"
expect {
timeout {exit}
-gl "\\\[*]\$*"
}
send_user "\nestoy en el directorio: "
send -- "pwd\r"
expect -gl "\\\[*]\$*"

puts "\n"
exit

8.12 telnet_re.exp


#!/usr/bin/expect -f
set force_conservative 1  ;# set to 1 to force conservative mode even if
                          ;# script wasn't run conservatively originally
if {$force_conservative} {
        set send_slow {1 .001}
        proc send {ignore arg} {
                sleep .1
                exp_send -s -- $arg
        }
}
puts "\n"

spawn telnet localhost
expect "ogin:*"
send -- "mante\r"
expect "assword:*"
send -- "a12345\r"
expect -re "\\\[.*]\\\$.*"
send_user "\nestoy en el directorio: "
send -- "pwd\r"
expect -re "\\\[.*]\\\$.*"

puts "\n"
exit


Next Previous Contents