Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

expect_out and cut to variable?

96 views
Skip to first unread message

mjlar94

unread,
Oct 28, 2011, 12:20:43 AM10/28/11
to
Hi,

I'm very new to TCL/Expect and I am trying to use expect to automate
booting a PXE linux distro over serial console, grab the DHCP discover
IP and execute a ssh login. So far I have everything working except
for parsing one of the following lines and doing something like the
result of cut -f -d' ' in a bash script. Can someone help me to better
understand expect_out functions in this scenario?

Kernel boot messages over serial console:
..
IP-Config: Got DHCP answer from 10.60.132.162, my address is
10.2.2.251
pcieport 0000:00:1c.4: wake-up capability enabled by ACPI
IP-Config: Complete:
device=eth1, addr=10.2.2.251, mask=255.255.255.0, gw=10.2.2.1,
host=1.2.3.5, domain=foo.com, nis-domain=foo.com,
bootserver=1.2.3.4, rootserver=1.2.3.4, rootpath=
..


#!/usr/bin/expect -f
set timeout 1200
set send_human {.1 .2 1 .02 2} #so we dont miss keystrokes over the
serial console connection.
spawn ##POWER CYCLE SWITCHED PDU HERE
sleep 30
spawn telnet [lindex $argv 0] ##TELNET CONSOLE HERE
expect "boot:"
send -h "trinity-3.4"
send "\r\n"
expect "Starting sshd"
expect "OK"
sleep 6
spawn ssh -l root [lindex $argv 0] <<Currently it breaks here
without resolving IP from DHCPDiscover
expect {
"(yes/no)?" {send "yes\n"; exp_continue}
"assword:" {send "trk\n"}
}
expect "trk"
send -h "winpass\n"
expect "\[1\]\:"
send -h "1\n"
expect "Select"
send -h "4\n"
expect "trk"
send -h "winpass\n"
expect "\[1\]\:"
send -h "1\n"
expect "Select"
send -h "1\n"
send -h "reboot\n"
send_user "Password reset request completed successfully."
eof

Uwe Klein

unread,
Oct 28, 2011, 5:47:30 AM10/28/11
to
mjlar94 wrote:
> Hi,
>
> I'm very new to TCL/Expect and I am trying to use expect to automate
> booting a PXE linux distro over serial console, grab the DHCP discover
> IP and execute a ssh login. So far I have everything working except
> for parsing one of the following lines and doing something like the
> result of cut -f -d' ' in a bash script. Can someone help me to better
> understand expect_out functions in this scenario?
>
> Kernel boot messages over serial console:
> ..
> IP-Config: Got DHCP answer from 10.60.132.162, my address is
> 10.2.2.251
> pcieport 0000:00:1c.4: wake-up capability enabled by ACPI
> IP-Config: Complete:
> device=eth1, addr=10.2.2.251, mask=255.255.255.0, gw=10.2.2.1,
> host=1.2.3.5, domain=foo.com, nis-domain=foo.com,
> bootserver=1.2.3.4, rootserver=1.2.3.4, rootpath=
> ..

assumed : working expect session getting the serial console output:

set console_spid $spawn_id

expect -re \
"ess is (\[0-9.]*)" {
set newIPx $exp_output(1,string)
puts stderr IPfromDHCP:$newIPx

exp_continue
} "addr=(\[0-9.]*)" {
set newIPy $exp_output(1,string)
puts stderr IPfromDHCP:$newIPy
exp_continue
} "Starting sshd" {
exp_continue
} "OK" {
spawn ssh $newIPy ;# or ssh $newIPx, whatever you like
set ssh_spid $spawn_id
} timeout {
puts stderr DEAD?
} eof {
puts stderr EOF!
}

expect -i $ssh_spid .... ;# work on the ssh connection

uwe
0 new messages