Currently I have some code that is doing an SFTP via expect/tcl. Its something like this:
send -i $ftpid "$cmd\r"
expect {
-i $ftpid -re "\n5\[0-9]\[0-9] \[^bB].*\nsftp> " {
set errorCode 149
set errorInfo "SFTP command error on $cmd."
return 1
}
-i $ftpid "452 Err.*\nsftp> " {
set errorCode 149
set errorInfo "SFTP 452 command error on $cmd."
return 1
}
-i $ftpid "Invalid command*\nsftp> " {
set errorCode 149
set errorInfo "SFTP invalid command on $cmd."
return 1
}
-i $ftpid -re "\n2\[0-9]\[0-9] .*\nsftp> " {
return 0
}
}
Occasionally I run into situations where an FTP server has return codes or messages that do not match what is already populated in the code base. Instead of modifying the core code for these what I would like to do is have an external file (ex: returncodes.tbl) where I can have a list of messages like:
552 Invalid Return*\nsftp>;<errorCode>;<errorInfo>
400 Some Error*\nsftp>;<errorCode>;<errorInfo>
...
So then it will interpret it in the expect code like:
-i $ftpid "552 Invalid Return*\nsftp>" {
set errorCode <errorCode>
set errorInfo "<errorInfo>"
return 1
}
-i $ftpid "400 Some Error*\nsftp>" {
set errorCode <errorCode>
set errorInfo "<errorInfo>"
return 1
}
I know how to read the external file and chop the variables up (open/read/split). However I cannot figure out how to create the loop needed within the expect statement. Was hoping someone might have an idea on how to accomplish this.
I think I would approach this, by building a case on the end of your existing expect statement that can catch a pattern and parse it with a regexp. Then you just need a for loop to weed through the file and snag a match.
You may need to play with the regexp to get it to work just right – sending \r a second time will align the expect buffer so that you can catch the error code.
(I don’t know how you loaded the file, so right now I’m assuming it’s a flat list split by “\n” and “;”)