Skip to content

Instantly share code, notes, and snippets.

@rlister
Created October 15, 2014 02:12
Show Gist options
  • Save rlister/e53dc90494f7b4657289 to your computer and use it in GitHub Desktop.
Save rlister/e53dc90494f7b4657289 to your computer and use it in GitHub Desktop.
open ssh to list of hosts in iterm tabs
#!/usr/bin/env perl
use strict;
use warnings;
## use applescript to start multiple iTerm tabs with ssh into listed hosts
##
## usage:
##
## iterm.pl host1 host2
## echo "host1 host2" | iterm.pl
## cat file_with_list_of_hosts | iterm.pl
use strict;
use warnings;
## input can come from either args or stdin
my @args = @ARGV ? @ARGV : do { my @stdin = <STDIN>; split(/\s+/, "@stdin"); };
## applescript 'words' doesn't parse things with periods correctly, so put one host
## per line and parse with 'paragraphs' instead
my @hosts = join("\n", @args);
## appscript code
my $osa =<< "EOF";
set hostlist to "HOSTLIST"
tell application "iTerm"
activate
set Servers to paragraphs of ("@hosts")
repeat with nextLine in Servers
if length of nextLine is greater than 0 then
set server to "nextLine"
set term to (current terminal)
tell term
launch session "Default Session"
tell the last session
write text "ssh " & nextLine
do shell script "/bin/sleep 0.01"
end tell
end tell
end if
end repeat
terminate the first session of the current terminal
end tell
EOF
## run the script
qx[osascript -e '$osa'];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment