Exemple d'un script pour un tunnel de courrier électronique

Ci-dessous est donné en exemple (pour MacOS 10.5) un script montant un tunnel entre l'ordinateur local et un serveur de mail distant.


use warnings;
use strict;

# Change the value of $mailserver to your mailserver's address
my $bastion = "bfricottin\@bastion.imag.fr";

# $server_port should be set to whatever TCP port on your mailhost that
# your mail program connects to.
# Typically, IMAP connects to port 143 and POP uses port 110.
# If your mailhost is Very Silly they might use some other port for these services,
# but this is likely not the case.
my $cible_pop_port = 110;
my $cible_smtp_port = 25;

# $local_port is the port on your Macintosh that will serve as the near end
# of the SSH tunnel. It has to be a number greater than 1024 (anything less
# than that is reserved for system use). Here, I'm just tacking a '0' onto
# the server's port number.
# client = mon ordinateur
my $client_pop_port = 1110;
my $client_smtp_port = 1025;

# Set $identity_file to the private RSA key to use for authentication.
# Beware, it must be an absolute path, otherwise, the grep does not work.
# Permissions for '/Users//.ssh/tunnel_isis' must not be too open.
# It is recommended that your private key files are NOT accessible by others.
# The too open private key will be ignored.
my $identity_file = "~/.ssh/id_rsa";

# You shouldn't need to change anything after this line.
my $ssh_pop_cmd = "ssh -i $identity_file -C -f -N -L $client_pop_port:$cible_pop:$cible_pop_port $bastion";
my $ssh_smtp_cmd = "ssh -i $identity_file -C -f -N -L $client_smtp_port:$cible_smtp:$cible_smtp_port $bastion";

my $ssh_string = "ssh -i $identity_file -C -f -N -L";
my $ssh_grep = "ps -ax | grep '$ssh_string' | grep -v grep";

my @ssh_cmds = `$ssh_grep`;

foreach my $command (@ssh_cmds) {
	my @line = split(/ +/, $command);
	my $pid = $line[0];
	if (!$pid =~ /[0-9]/) {
		$pid = $line[1];
		print "Killing existing command:\n $command";
		`kill -9 $pid`;

print "Command is:\n$ssh_pop_cmd\n";
print "Command is:\n$ssh_smtp_cmd\n";

# Only start a new ssh tunnel if we don't have a previous connection
# if (`$ssh_pop_count` == 0) { 
# system("open /Applications/Mail.app");

Une fois ce script enregistré dans un fichier avec l'extension .prl, il est possible de lancer son exécution depuis un item du menu des scripts dont on peut configurer qu'il s'affiche dans la barre des menus. Pour cela, il suffit de cocher la case appropriée dans l'application Utilitaire AppleScript.