Re: Trying to create a ssh command using applescript?
Re: Trying to create a ssh command using applescript?
- Subject: Re: Trying to create a ssh command using applescript?
- From: Courtney Moore <email@hidden>
- Date: Thu, 10 Apr 2003 10:27:59 -0500
On 4/9/03 6:56 PM, "Andrew Oliver" <email@hidden> wrote:
>
>
>
On 4/9/03 3:16 PM, "Courtney Moore" <email@hidden> wrote:
>
>
> I have almost answered my own question
>
> If I run:
>
> Tell application "Terminal"
>
> Do script "ssh blah@blah" in window 1
>
> Delay 10
>
> Do script "mypassword" in window 1
>
> End tell
>
>
>
> It works if the Terminal is not already open or if there is a terminal
>
> window on the screen, YEAH! But there are now two new problems.
>
>
>
> The first problem is, if I run this script while Terminal is open but with
>
> no active windows it gives me the error 3Terminal got an error:
>
> NSArgumentEvaluationScriptError2.
>
>
Two ways of dealing with this one.
>
>
First, you can check for the existence of a window using the syntax:
>
>
if exists (window 1) then
>
-- the window exists
>
end if
>
>
And take appropriate action (or use 'if not exists (window 1)...' to check
>
it does not exist).
>
>
However, in this case, there's a potentially simpler solution.:
>
>
tell application "terminal"
>
do script "ssh blah@blah"
>
delay 10
>
do script "my password" in window 1
>
end tell
>
>
The premise here is that the first 'do script' will open a new window for
>
the ssh session, thereby ensuring that 'window 1' exists by the time the
>
second command executes.
>
>
This also has the advantage that it won't interfere with whatever's going on
>
in the current front window if one exists - imagine, for example, what might
>
happen if the user is using vi to edit a file when this script fires off -
>
not pretty.
>
>
>
>
> The second problem is that if I am sshing into a host for the first time
>
> then I will be prompted to give a yes no answer is there a way to trap for
>
> this so I can include and if statement to output 3yes2??
>
>
>
This is the problem with trying to AppleScript terminal.app - there's no
>
feedback from previous actions.
>
>
About the only way you can do this is:
>
>
do script "ssh blah@blah"
>
delay 10
>
set thePrompt to last word of ((contents of window 1) as string)
>
if thePrompt is "no" then -- asking us if we want to continue?
>
do script "yes" in window 1
>
delay 5
>
end if
>
do script "my password" in window 1
>
>
>
i.e., the script is checking 'contents of window 1' which is the entire
>
transcript of what's in the current window. If the last word is "no" (as it
>
would be in the line "Are you sure you want to continue connecting
>
(yes/no)?", then it types "yes", pauses and the enters the password.
>
>
If course, you could also check that the last word is "password" to ensure
>
you're being asked for the password. For example, if the user enters an
>
invalid hostname, the last line of the terminal will be "Bad host name:
>
hostname", or if the host is down, you'll have a timeout message to which
>
you'd blindly type the password, revealing it to the user.
>
>
Andrew
>
:)
>
Thanks very much! Your insight has been very helpful!
Till the next AppleScript inquiry
Courtney
_______________________________________________
applescript-users mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/applescript-users
Do not post admin requests to the list. They will be ignored.