Checking for a SSH connection

I’m writing a script to launch a set of applications on a server via X11 and terminal.
I’ve got everything figured out except that I would like to let the user switch between which program to use with out having to log out of ssh and log back in (which would still be hard because the script closes as soon as the hosted application closes.

So what I would like to know is if there is a way to check for an ssh connection - and if it can be a specific connection then even better - at the beginning of my script so I can run an if command telling the script to connect or not based on weather or not the computer is connected via ssh.

thank you for you help, and if you are in any way confused by what I’m trying to do, by all means ask me about it and I will answer to the best of my ability.

you might be able to do something like this…

do shell script "ps -aux |grep ssh |grep -v sshd |grep -v grep"

if there is any current connection from your system to another system it would display it something like this…

"USERNAME  1498   0.0  0.4    27976   1620  p1  S+   11:58PM   0:01.60 ssh SERVER"

thank you for your response, I’m not at home right now so I can’t ceck to see if that works but I’ll check on monday and get back to you.

I wrote up a little script that will check and ssh connection to a specific server.
hope this helps

property theServer : "YourServer" -- notice that the name might get truncated. -- theserver.thedomain.com might be truncated to theserve
tell application "System Events"
	try
		do shell script "ps -aux | grep ssh | grep -v sshd | grep -v grep | wc |awk '{print $1}'"
		if result >= "1" then
			do shell script "ps -aux |grep ssh |grep " & theServer & "| wc | awk '{print $1}'"
			if result >= "1" then
				return "connection to " & theServer & " is active"
			else
				return "connection to " & theServer & " is not active"
			end if
		else
			return "no ssh connections active"
		end if
	on error theErr
		display dialog theErr
	end try
end tell

cool, tanks for the script. I actually found a way to get the first idea to work with the following script:

do shell script "ps -aux |grep ssh"
set essh to result as string
set yssh to offset of UsrID & "@" & server in essh
if yssh is equal to 0 then
	display dialog "No connection"
else if yssh is not equal to 0 then
	display dialog "there already is a connection"
end if

I had to reduce the shell script so that is would accept those other connections otherwise if the connection I was looking for wasn’t there then there would be no result to analyze, just an error type 1

but I’m still going to try your second script. Thank you

Now my next problem is if there isn’t a connection I want to connect to that same server but it requires a password and a shell definition. like so:
: ssh USERNAME@SERVER
password: USERPASSWORD
Terminal type?: vt100

but when I type code like this

 do shell script ("echo" & USERPASSWORD & "| ssh USERNAME@SERVER") 

it just returns something like this:
“Pseudo-terminal will not be allocated because stdin is not a terminal.
Permission denied, please try again.
Permission denied, please try again.
Permission denied (publickey,password,keyboard-interactive,hostbased).”

And even if that did work I don’t know what I would do to enter in that second information of “vt100”

any ideas?

it might be easier to interface with the terminal program directly. I am assuming that your not running an XTERM through X11 or XDarwin or something like that. If you are using just the main terminal program for OS X, you could do something like

property USERNAME : "your username"
property SERVER : "your server"
property PASSWORD : "your password"
set theWait to 3

tell application "Terminal"
	activate
end tell

tell application "Terminal"
	repeat with theWindow in window 1
		do script "ssh -l " & USERNAME & " " & SERVER in theWindow
delay theWait
do script PASSWORD in theWindow
	end repeat
end tell

i know that in the bash shell you can change the emulation by just typing

export TERM=vt100

but it only works with in the bash shell. if you are using tcsh, which is the default shell emulation that apple uses (i think) the change command is different.

so IF you are using the bash shell for your default with in terminal you could just have applescript change your shell before you ssh into the server.

hope this helps.