Skip to content

Instantly share code, notes, and snippets.

@rsperl
Last active July 25, 2022 13:08
Show Gist options
  • Save rsperl/bcc7fbb845b2cf5c260a to your computer and use it in GitHub Desktop.
Save rsperl/bcc7fbb845b2cf5c260a to your computer and use it in GitHub Desktop.
Reads in ~/.ssh/config and generates iterm2 dynamic profiles #perl #iterm2 #macos #ssh #snippet
#!/usr/bin/env perl
#
# licensed under GPL v2, same as iTerm2 https://www.iterm2.com/license.txt
#
use strict;
use JSON;
my $output = $ENV{HOME} . "/Library/Application\ Support/iTerm2/DynamicProfiles/profiles.json";
print STDERR "profiles will be written to '$output'\n";
my @profiles;
my $ssh_config = $ENV{HOME} . "/.ssh/config";
open my $fh, '<', $ssh_config;
my @contents = <$fh>;
close $fh;
my $name;
my $custom_command;
my @tags;
for(my $i=0; $i<@contents; $i++) {
my $line = $contents[$i];
chomp $line;
next if index($line, '*') >= 0;
if( $line =~ /^Host\s+(.+?)$/ ) {
my $match = $1;
if( $match =~ /(.+?)\s+#\s+Tags=(.+?)$/ ) {
$name = $1;
@tags = split(/\s*,\s*/, $2);
} else {
$name = $match;
@tags = ();
}
} elsif( $line =~ /^\s*$/ ) {
next unless $name;
print STDERR "adding profile for $name\n";
$custom_command = "ssh $name";
my $p = {
Name => $name,
Guid => $name,
Shortcut => "",
"Custom Command" => "Yes",
Command => $custom_command,
};
$p->{Tags} = [@tags] if( @tags );
$name = "";
$custom_command = "";
@tags = ();
push(@profiles, $p);
};
}
my $json = to_json({ Profiles => \@profiles }, { pretty => 1 });
open my $fh, '>', $output;
print $fh $json;
close $fh;
print STDERR "finished\n";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment