Training, Open Source computer languages

This is page http://www.wellho.net/forum/The-Tcl- ... guage/Regexp-and-expect.html

Our email: info@wellho.net • Phone: 01144 1225 708225

 
For 2023 (and 2024 ...) - we are now fully retired from IT training.
We have made many, many friends over 25 years of teaching about Python, Tcl, Perl, PHP, Lua, Java, C and C++ - and MySQL, Linux and Solaris/SunOS too. Our training notes are now very much out of date, but due to upward compatability most of our examples remain operational and even relevant ad you are welcome to make us if them "as seen" and at your own risk.

Lisa and I (Graham) now live in what was our training centre in Melksham - happy to meet with former delegates here - but do check ahead before coming round. We are far from inactive - rather, enjoying the times that we are retired but still healthy enough in mind and body to be active!

I am also active in many other area and still look after a lot of web sites - you can find an index ((here))
Regexp and expect

Posted by johnjacky (johnjacky), 30 September 2005
I am very newbie to Expect started on a day back.. I am not a hardcore programmer  even. But a newbie to programming even.

May I have some help , from you on finding a regex which matches the below expressions,

=====
[root@calgary ~]#
[jj@projeon jj]$
======

I tried using expect -re "\[root" (It didn't work and was throwing errors]
I tried using expect -re "\u001b]0;root" [it did work, but it will only match if the user is root)

I want to send some commands , if it matches like  "^\[A-Za-z0-9@" How can I use a such a pattern with expect ? i.e if the line starts with --> [username@  <--, I would like to send some commands

Thanks in advance,
Masters help!!!!

Posted by johnjacky (johnjacky), 30 September 2005
Also if anyone could tell me why I need \u001b ( [for Control + [)  and ]0; in the regexp match of "[root" . Not sure what is the meaning of unicode ]0; Is there any util for knowing that ? I will much appreciate if anyone could help me on it.

And for a solution, ofcourse I can use the expect -re "(%|#|>|\\\$) $". Taken from the forums itself Thanks.

Posted by admin (Graham Ellis), 1 October 2005
I think you want to write something like

{\[[a-z0-9]+@}

as your regular expression.   Depending on how you protect it, the [ character can mean any one of three things in the regular expression area of Tcl / expect program:

1. Without and protection from the Tcl interpretter, it means "run the command in these square brackets first, and substitute the result into the command.  Thus ...

Code:
set fred [clock seconds]
puts $fred


2. Within a regular expression (typically protected from the Tcl interpretter using double quotes or curly braces), a list of characters in square brackets matches any one character from the list.

3. Within a regular expression, and protected from the Tcl interpretter as described just above AND protected from the regular expression handler with a \ character, you can look literally for the [ character.

Here's an example piece of code that matches the [username@ sequence in a Tcl regular expression:

Code:
gets stdin stuff
puts $stuff

if {[regexp {\[[a-z0-9]+@} $stuff found]} {
       puts "yes - $found"
} else {
       puts "no"
}


That's a complete, tested program.

Your use of unicode characters is fair enough - it could be easier that way in some circumstances as it avoids the needs for quite as much protection of special characters from the various interpretters.

Posted by Custard (Custard), 1 October 2005
Hi, the control characters you mention look like the ANSI escape sequence to make the prompt appear a different colour.
ANSI escapes begin with an ESC (0x1b) followed by a code or codes terminated by a semicolon (

HTH

B

Posted by johnjacky (johnjacky), 2 October 2005
WOW..this is simply superb..I never expected such a response like this...Simply excelltent..If I was somewhere at UK, I wouldn't have had a second thought to get trained in programming concepts. Thanks Graham..Thanks Custard too...

May I have one more help, please..I am not sure whether this is a bug or any work around possible for this..My code snippet is as below,


Code:
expect -re "connecting (yes/no)?" {
               send -- "yes\r"
               expect -nocase -re "password:"
                       {
               send "{[lindex $argv 1]}\r"
                       }
                       } -nocase -re "password:" {
                       send -- "{[lindex $argv 1]}\r"
                        }


Everything works except when I give the pass as something like --> p0lic3%$# <-- It is getting interpretted as p0lic3%0. The exact message when I turn on the exp_internal is

send: sending "{p0lic3%0}\r" to { exp4 }

How can I make the expect program take the pass as it is ?

Thanks a bunch
JJ

Posted by admin (Graham Ellis), 2 October 2005
I notice that you're picking up the password from the command line (argv).   With most command line interpretters (shell programs), the # character means that it's the start of a comment which will get stripped out before it even gets to Tcl, and I suspect this may be your problem.

Suggestion - print out (JUST for testing as people hate to have their passwords echoed in plain text) $argv in you Tcl and see what you've actually got there.   Once we know if it's a Tcl or shell problem, we can look for an appropriate solution.

Posted by johnjacky (johnjacky), 2 October 2005
yes that was the issue Thank you once again.



This page is a thread posted to the opentalk forum at www.opentalk.org.uk and archived here for reference. To jump to the archive index please follow this link.

© WELL HOUSE CONSULTANTS LTD., 2024: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 793803 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho