I'm writing this expect script that sshs into a device and runs a cat command to view the mac address of the device. I wan't to assign this mac address to a variable. However, I'm having trouble parsing the data.
This is the contents of my expect_out(buffer):
expect: set expect_out(buffer) "cat /sys/class/net/eth0/address\r\nd0:21:f9:4f:c1:68\r\nU6-Lite-BZ.6.0.21# "
The d0:21:f9:4f:c1:68 is what I actually want.
This is what commands I have been running:
expect -re {(?n)^d0.*}
set test "$expect_out(0,string)"
puts "$test"
And this does store the correct output to a variable. However, normally I wouldn't know the first 2 chars of the mac address (d0), so I need to edit the line such that it just reads after the newline to the next newline. Or reads a number of characters after the first newline ending. Does anyone know how to do this? Thanks for the help.
CodePudding user response:
Since you already know the file content is a MAC addr which is 17 chars (:
and HEX digits) long --
expect -re {[:[:xdigit:]]{17}}
set test "$expect_out(0,string)"
puts "$test"
CodePudding user response:
You could do
set lines [split $expect_out(buffer) \n]
set macaddr [string trim [lindex $lines end-1] \r]