Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

An AppleScript to ssh to multiple hosts in iTerm Apps
I tried finding a simple script to ssh to multiple hosts within iTerm, and couldn't find one. So I wrote this little AppleScript that does the following:
  1. Parses a list of systems in $HOME/serverlist (create this file and put your hosts listed one per line)
  2. Spawns a new tab in the current window
  3. Changes tab title to the name of the host
  4. Spawns an ssh session (using your username) to the host. Note that you can easily edit this to connect as root or whomever by changing the ssh line
  5. Bank lines should be ignored (I haven't played with this much, but it seemed to work when I have tried it)
One thing to note: I found that iTerm tends to freak out if we spawn too many tabs/sessions at once. To compensate, I added an extremely stupid group of echo statements to slow things down (this was quicker than a full one-second pause). If anyone has an solution for this, feel free to fix my lousy code.
    •    
  • Currently 2.60 / 5
  You rated: 3 / 5 (5 votes cast)
 
[12,540 views]  

An AppleScript to ssh to multiple hosts in iTerm | 14 comments | Create New Account
Click here to return to the 'An AppleScript to ssh to multiple hosts in iTerm' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
An AppleScript to ssh to multiple hosts in iTerm
Authored by: ElvisThePelvis on Jul 12, '07 09:24:46AM

Hmmm, this didn't seem to work with iTerm Build 0.9.5.0611. I didn't investigate further as to why, but perhaps someone else can easily point out why.



[ Reply to This | # ]
An AppleScript to ssh to multiple hosts in iTerm
Authored by: nvrmore100 on Jul 12, '07 12:28:10PM

Hmm will have to try the new version I guess. I am running Build 0.9.5.0517, works fine on there.



[ Reply to This | # ]
An AppleScript to ssh to multiple hosts in iTerm
Authored by: garyaj on Jul 12, '07 09:39:48PM

I had some problems with the script as posted. Here's my version using examples from the iTerm Help page:

-- Launch iTerm and log into multiple servers using SSH
tell application "iTerm"
	activate
	-- Read serverlist from file path below
	set Servers to paragraphs of (do shell script "/bin/cat $HOME/serverlist")
	repeat with nextLine in Servers
		-- If line in file is not empty (blank line) do the rest
		if length of nextLine is greater than 0 then
			set server to "nextLine"
			set term to (current terminal)
			-- Open a new tab
			tell term
				launch session "Default Session"
				tell the last session
					write text "ssh " & nextLine
					-- sleep to prevent errors if we spawn too fast
					do shell script "/bin/sleep 0.01"
				end tell
			end tell
		end if
	end repeat
	-- Close the first tab since we do not need it
	terminate the first session of the current terminal
end tell

The only difference is the use of 'launch session' instead of 'make new session at the end of sessions' and 'write text' instead of 'exec command'. Works a treat on iTerm 0.9.5.0611



[ Reply to This | # ]
An AppleScript to ssh to multiple hosts in iTerm
Authored by: rasman1978 on Jul 12, '07 10:48:45AM
Yeah, and you could change the background image for each server too if they'd ever address my bug report. Go add a "Yes, please fix this!" comment on it if you think that would be cool.

[ Reply to This | # ]
An AppleScript to ssh to multiple hosts in iTerm
Authored by: foilpan on Jul 12, '07 11:39:32AM
haven't tried it on os x, but this may work for you: clusterssh

[ Reply to This | # ]
An AppleScript to ssh to multiple hosts in iTerm
Authored by: kbradnam on Jul 12, '07 11:47:35AM

Can't you just use the UNIX sleep command with a value less than 1? I.e. do shell script "/bin/sleep 0.25"

Sleep does accept very small time intervals (e.g. sleep 0.01).

Keith



[ Reply to This | # ]
An AppleScript to ssh to multiple hosts in iTerm
Authored by: delight1 on Jul 12, '07 01:20:32PM

i do believe that this should work...



[ Reply to This | # ]
An AppleScript to ssh to multiple hosts in iTerm
Authored by: nvrmore100 on Jul 12, '07 03:33:49PM

Interesting, I ran some tests and it does indeed let you do partial seconds. I have tried doing sleeps for less than a second in the past (quite a few years ago) and the systems back then wouldn't allow for it (really old Solaris and HP systems). Looks like that works though, doing a quarter second sleep seems to be just about right to keep it from hanging up.

Thanks for the tip!



[ Reply to This | # ]
An AppleScript to ssh to multiple hosts in iTerm
Authored by: garyaj on Jul 12, '07 08:09:47PM

'sleep' with fractions of seconds is a new one for me also. Just checked it on my MB and yay!, it works.

Many thanks for this.



[ Reply to This | # ]
An AppleScript to ssh to multiple hosts in iTerm
Authored by: garyaj on Jul 12, '07 08:17:48PM

Many thanks for this script. Your timing is excellent! After using iTerm for a while now I started looking this morning how to write a script to log me into the 7 machines I connect to each day.

I also discovered the iTerm Help which gives some AppleScripting examples similar to your script. Not sure when that appeared but it wasn't there a year ago.



[ Reply to This | # ]
An AppleScript to ssh to multiple hosts in iTerm
Authored by: JSwan on Jul 15, '07 01:36:47PM

You can do largely the same thing in iTerm using bookmarks. Create an iTerm bookmark for each connection then simply select "Open All" under the bookmarks window. It's not quite as versatile as the AppleScript method but results are similar.

I have a question regarding use of the AppleScript. I find it works as expected if iTerm in not running but fails if iTerm has already been started. Can anyone explain why and suggest a fix?



[ Reply to This | # ]
An AppleScript to ssh to multiple hosts in iTerm
Authored by: nvrmore100 on Jul 16, '07 08:32:47AM

Bookmarks are great, assuming you log into the same machines over and over. Unfortunately I log into various machines constantly (support a group of over 8000 servers), using bookmarks for this is a royal pain in the...

As for the errors, what exactly happens if it is already open? It should do essentially the same function open or closed.



[ Reply to This | # ]
An AppleScript to ssh to multiple hosts in iTerm
Authored by: JSwan on Jul 16, '07 12:41:31PM

When I execute the script using Script Editor prior to starting iTerm it works as expected. If I then close the iTerm window (including all tabs) and Run the script again it stops on the line tell term with an error of "The variable term is no defined".
I think I see what is going on. The script is expecting there to be a (current terminal) to provide a value to term in the statement "set term to (current terminal)". If no window is open in iTerm it cannot complete the set command thereby leaving term with no value. My problem is easily avoided by insuring there is an iTerm window open when the script is executed. There may also be a script command to would open one if none is open.



[ Reply to This | # ]
An AppleScript to ssh to multiple hosts in iTerm
Authored by: bankruggley on Jul 27, '07 07:52:52AM

I think the easiest way is to use screen(1) on the machine that you use most frequently. Keep that running (detached) and reattach when you need it; ssh to your machine and reattach there.

I use it all the time and it works perfectly for having multiple sessions open.

See the screen manpage: man 1 screen



[ Reply to This | # ]