Applescripting Terminal.app text string starts event???

I need to telnet into a unix box. Latency is killing my dream. I want a script that waits for a specific text string, when that text string appears in the terminal.app window, the script continues. I have looked high and low for snippets of code that may come close to what I want to do. So far I have only found this…


set theText to "login"
set lookingFor to "sample"

offset of lookingFor in theText --> if result is not "0", then it is contained
theText contains lookingFor --> true or false, by default ignores case
lookingFor is in contents of theText --> the same

The actual code does nothing for me now. But I think that I can modify this to do what I need.

These are a few of the actions I need to setup…

telnet 192.168.1.1
“wait for Login”
at login type username
“wait for Password”
at password type password

Any help would be really appreciated.

Terminal.app? Moving to OS X forum.

hi skin,

take a look at my post to darkomen on Expect:

http://bbs.applescript.net/viewtopic.php?id=17733

i think you could leverage that into a telnet script with a little work. i’ll look into it myself later today.

also, do a google search for ‘telnet expect’ or something similar. if you find something you want, but can’t figure out how to use it in AppleScript i can help.

Thanks, I really want to do this in applescript only. If possible…
I could do this in a couple of minutes in Visual Basic, I need this to run on macs and be able to move
from machine to machine without installing other apps.

hello again,

here is an AppleScript using “Expect” that will open up a Terminal session that Telnets to a Unix box. i used Solaris as my telnet server, so you may have to tweek it a bit for other Unix/Telnet Servers:


property myExpect : "#!/usr/bin/expect 
spawn /usr/bin/telnet -K [lindex \\$argv 0]
set user [lindex \\$argv 1]
expect \"login: \"
send \"\\$user
\"
set password [lindex \\$argv 2]
expect \"Password:\"
send \"\\$password
\"
interact"

set myServer to text returned of (display dialog "What Server would you like to connect to?" default answer "")

set myUser to text returned of (display dialog "What is the username for this telnet session?" default answer "")

set myPass to text returned of (display dialog "What is the password for this telnet session?" default answer "" with hidden answer)

do shell script "/bin/cat > /tmp/expectTelnet << EOF 
" & myExpect & "
EOF" with administrator privileges
do shell script "/bin/chmod +x /tmp/expectTelnet" with administrator privileges

tell application "Terminal"
	activate
	do script "/tmp/expectTelnet " & myServer & space & myUser & space & myPass --with administrator privileges
end tell

--display dialog "Click \"Ok\" when you are done."
delay 10
do shell script "/bin/rm -rf /tmp/expectTelnet" with administrator privileges

NOTE: this script just connects and then hands over control to the user. you would have to know what to ‘expect’ to wait for a particular string and do another action. this is all scriptable and automatable.

resources:

all comments welcome!

hi,

skin sent me a private email about this thread, and some of his code. i won’t post his code, but thought that the reply might be useful. Expect is kind of a cool scripting language to know–and not just for AppleScripting.

hi skin,

sorry it took so long to reply, i usually don’t check these things.

yes. it’s also available on Linux and most Unix distributions.

don’t worry. if you can use AppleScript to script something like the Terminal, then you can do Expect. you will probably find it really easy.

ok. first, i should probably explain everything that i’m doing. first, i figure out what to ‘expect’. i know that Telnet will try to authenticate as my user if i don’t give it a ‘-K’ flag. so i do that:

telnet xxx.xxx.xxx.xxx -K

and i get:

login:

so i know that will be what to ‘expect’. so already i know that i can write an Expect script like this:

#!/usr/bin/expect
spawn /usr/bin/telnet -K xxx.xxx.xxx.xxx
set user “me”
expect “login:”
send “$user”

i save this as an executable script. say i call it expectTelnet.

now when i run that at the command line, expect spawns a telnet session under itself and waits for what i told it to look for. when it sees, “login:”, it will send my username. nice, huh?

but that’s not good enough. i need to specify my telnet server, and i may need to use a different username. no problem, i just use command line arguments, like this:

#!/usr/bin/expect
spawn /usr/bin/telnet -K [lindex $argv 0]
set user [lindex $argv 1]
expect “login:”
send “$user”

now i save that as an executable and run it like this:

./expectTelnet xxx.xxx.xxx.xxx me

as you can see, as long as i know what to ‘expect’ next i can pretty much write the whole session.

now i wrap that in an AppleScript so that it has a GUI. i ask the user for the info that i want with some display dialogs & variables (i’m sure you understand this part, so i won’t bother explaining that). then i write the expect file to the ‘/tmp’ directory and call it with my arguments.

as you can see it works pretty well. you should read the wikipedia article i attached, which explains a lot.

i’m going to post this as a reply to our earlier thread. i won’t include your code, but i think the discussion may be useful to someone–cheers.