{"id":1615,"date":"2015-05-02T22:00:25","date_gmt":"2015-05-02T20:00:25","guid":{"rendered":"https:\/\/www.fanjoe.be\/?p=1615"},"modified":"2015-05-04T19:06:23","modified_gmt":"2015-05-04T17:06:23","slug":"os-x-applescript-pour-automatiser-la-creation-dun-tunnel-ssh-pour-vnc","status":"publish","type":"post","link":"https:\/\/www.fanjoe.be\/?p=1615","title":{"rendered":"OS X : AppleScript pour automatiser la cr\u00e9ation d&rsquo;un tunnel SSH pour VNC"},"content":{"rendered":"<h1>Introduction<\/h1>\n<p>Dans un <a href=\"https:\/\/www.fanjoe.be\/?p=1561\">article pr\u00e9c\u00e9dent<\/a>, nous avons vu comment \u00e9tablir un tunnel SSH pour permettre une connexion VNC (Partage d&rsquo;\u00e9cran) s\u00e9curis\u00e9e.<\/p>\n<p>Malgr\u00e9 la simplicit\u00e9 des commandes utilis\u00e9es, la man\u0153uvre demande tout de m\u00eame un peu de temps.<\/p>\n<p>Nous allons voir ici comment automatiser tout \u00e7a gr\u00e2ce \u00e0 un petit AppleScript.<\/p>\n<h1>Le script<\/h1>\n<h2>Dans l&rsquo;\u00e9diteur<\/h2>\n<p>On commence par ouvrir l\u2019\u00c9diteur AppleScript :<\/p>\n<p style=\"text-align: center;\"><a href=\"https:\/\/www.fanjoe.be\/wp-content\/uploads\/2015\/05\/Auto-SSH-VNC-01.jpg\"><img loading=\"lazy\" decoding=\"async\" data-attachment-id=\"1616\" data-permalink=\"https:\/\/www.fanjoe.be\/?attachment_id=1616\" data-orig-file=\"https:\/\/www.fanjoe.be\/wp-content\/uploads\/2015\/05\/Auto-SSH-VNC-01.jpg\" data-orig-size=\"491,135\" data-comments-opened=\"1\" data-image-title=\"Auto-SSH-VNC-01\" data-image-description=\"\" data-image-caption=\"\" data-large-file=\"https:\/\/www.fanjoe.be\/wp-content\/uploads\/2015\/05\/Auto-SSH-VNC-01.jpg\" class=\"alignnone size-full wp-image-1616\" src=\"https:\/\/www.fanjoe.be\/wp-content\/uploads\/2015\/05\/Auto-SSH-VNC-01.jpg\" alt=\"Auto-SSH-VNC-01\" width=\"491\" height=\"135\" srcset=\"https:\/\/www.fanjoe.be\/wp-content\/uploads\/2015\/05\/Auto-SSH-VNC-01.jpg 491w, https:\/\/www.fanjoe.be\/wp-content\/uploads\/2015\/05\/Auto-SSH-VNC-01-175x48.jpg 175w, https:\/\/www.fanjoe.be\/wp-content\/uploads\/2015\/05\/Auto-SSH-VNC-01-300x82.jpg 300w\" sizes=\"auto, (max-width: 491px) 100vw, 491px\" \/><\/a><\/p>\n<p>Dans l&rsquo;\u00e9diteur, on colle ce script :<\/p>\n<p><a href=\"https:\/\/www.fanjoe.be\/wp-content\/uploads\/2015\/05\/Auto-SSH-VNC-02.jpg\"><img loading=\"lazy\" decoding=\"async\" data-attachment-id=\"1618\" data-permalink=\"https:\/\/www.fanjoe.be\/?attachment_id=1618\" data-orig-file=\"https:\/\/www.fanjoe.be\/wp-content\/uploads\/2015\/05\/Auto-SSH-VNC-02.jpg\" data-orig-size=\"620,471\" data-comments-opened=\"1\" data-image-title=\"Auto-SSH-VNC-02\" data-image-description=\"\" data-image-caption=\"\" data-large-file=\"https:\/\/www.fanjoe.be\/wp-content\/uploads\/2015\/05\/Auto-SSH-VNC-02-620x471.jpg\" class=\"alignnone size-full wp-image-1618\" src=\"https:\/\/www.fanjoe.be\/wp-content\/uploads\/2015\/05\/Auto-SSH-VNC-02.jpg\" alt=\"Auto-SSH-VNC-02\" width=\"620\" height=\"471\" srcset=\"https:\/\/www.fanjoe.be\/wp-content\/uploads\/2015\/05\/Auto-SSH-VNC-02.jpg 620w, https:\/\/www.fanjoe.be\/wp-content\/uploads\/2015\/05\/Auto-SSH-VNC-02-175x133.jpg 175w, https:\/\/www.fanjoe.be\/wp-content\/uploads\/2015\/05\/Auto-SSH-VNC-02-300x228.jpg 300w, https:\/\/www.fanjoe.be\/wp-content\/uploads\/2015\/05\/Auto-SSH-VNC-02-460x350.jpg 460w\" sizes=\"auto, (max-width: 620px) 100vw, 620px\" \/><\/a><\/p>\n<pre>do shell script \"ssh -A -N -L 5900:localhost:5900 user@domaine.com vncserver -geometry 1024x768 :0 &amp;&gt; \/dev\/null &amp; echo $!\"\r\nset sshpid to the result\r\ndelay 2\r\ndo shell script \"open vnc:\/\/localhost:5900\"\r\ndelay 0.5\r\nactivate\r\ndisplay dialog \"Lorsque vous avez termin\u00e9 votre session VNC, cliquez sur &lt;Reboucher le Tunnel&gt;.\r\n\r\nSSH PID : \" &amp; sshpid buttons \"Reboucher le Tunnel\" default button \"Reboucher le Tunnel\" with icon caution\r\ndo shell script \"kill -9 \" &amp; sshpid\r\ntell application \"Screen Sharing\"\r\n\u00a0\u00a0 \u00a0quit\r\nend tell<\/pre>\n<h2>Explications<\/h2>\n<p>NB : Nous nous <a href=\"https:\/\/www.fanjoe.be\/?p=1600\">connecterons en SSH gr\u00e2ce \u00e0 un certificat<\/a>.<\/p>\n<pre>do shell script \"ssh -A -N -L 5900:localhost:5900 user@domaine.com <span style=\"color: #ff0000;\">&lt;&lt;&gt;&gt;<\/span><\/pre>\n<p>On ex\u00e9cute un tunnel SSH avec le port 5900 en entr\u00e9e sur le client et le port 5900 en sortie sur le serveur. Ce sont les ports standards de VNC.<\/p>\n<p><code>user<\/code> est le nom d&rsquo;un utilisateur. <code>pi<\/code> par exemple.<\/p>\n<p><code>domain.com<\/code> est le domaine par lequel vous acc\u00e9dez \u00e0 votre serveur ou son adresse IP.<\/p>\n<pre><span style=\"color: #ff0000;\">&lt;&lt;&gt;&gt;<\/span> vncserver -geometry 1024x768 :0 <span style=\"color: #ff0000;\">&lt;&lt;&gt;&gt;<\/span><\/pre>\n<p>On poursuit la commande avec la partie qui sera ex\u00e9cut\u00e9e sur le client. On y d\u00e9marre le serveur VNC avec une dimension d&rsquo;\u00e9cran de 1024&#215;768 sur le port 0, c-\u00e0-d 5900.<\/p>\n<pre><span style=\"color: #ff0000;\">&lt;&lt;&gt;&gt;<\/span> &amp;&gt; \/dev\/null &amp; echo $!<\/pre>\n<p>Et on envoi la r\u00e9ponse du serveur qui nous dit que tout s&rsquo;est bien pass\u00e9 \u00e0 la poubelle.<\/p>\n<pre>set sshpid to the result<\/pre>\n<p>Le retour du PID, c&rsquo;est \u00e0 dire le num\u00e9ro du processus qui vient d&rsquo;\u00eatre d\u00e9marr\u00e9 est stock\u00e9 dans la variable <code>sshpid<\/code>.<\/p>\n<pre>delay 2<\/pre>\n<p>On fait une petite pause de 2 sec en attendant que le processus VNC sur le serveur ait fini de booter.<\/p>\n<pre>do shell script \"open vnc:\/\/localhost:5900\"<\/pre>\n<p>On d\u00e9marre le \u00ab\u00a0Partage d&rsquo;\u00e9cran\u00a0\u00bb (le client VNC) sur le Mac.<\/p>\n<pre>delay 0.5\r\nactivate\r\ndisplay dialog \"Lorsque vous avez termin\u00e9 votre session VNC, cliquez sur &lt;Reboucher le Tunnel&gt;.<\/pre>\n<p>On attend une demi-seconde, le temps que \u00ab\u00a0Partage d&rsquo;\u00e9cran\u00a0\u00bb soit affich\u00e9.<br \/>\nEt on place la bo\u00eete de dialogue qui va permettre de tout stopper au premier plan.<\/p>\n<pre>SSH PID : \" &amp; sshpid buttons \"Reboucher le Tunnel\" default button \"Reboucher le Tunnel\" with icon caution<\/pre>\n<p>D\u00e8s que l&rsquo;on va cliquer sur \u00ab\u00a0Reboucher le tunnel\u00a0\u00bb on va :<\/p>\n<pre>do shell script \"kill -9 \" &amp; sshpid<\/pre>\n<p>Tuer le process dont on a m\u00e9moris\u00e9 le num\u00e9ro plus haut.<\/p>\n<pre>tell application \"Screen Sharing\"\r\n\u00a0\u00a0\u00a0 quit\r\nend tell<\/pre>\n<p>Quitter le \u00ab\u00a0Partage d&rsquo;\u00e9cran\u00a0\u00bb.<\/p>\n<h2>En faire une App<\/h2>\n<p>L&rsquo;avantage d&rsquo;AppleScript, c&rsquo;est qu&rsquo;on peut en faire une App simple \u00e0 ex\u00e9cuter&#8230;<\/p>\n<p><a href=\"https:\/\/www.fanjoe.be\/wp-content\/uploads\/2015\/05\/Auto-SSH-VNC-03.jpg\"><img loading=\"lazy\" decoding=\"async\" data-attachment-id=\"1619\" data-permalink=\"https:\/\/www.fanjoe.be\/?attachment_id=1619\" data-orig-file=\"https:\/\/www.fanjoe.be\/wp-content\/uploads\/2015\/05\/Auto-SSH-VNC-03.jpg\" data-orig-size=\"620,471\" data-comments-opened=\"1\" data-image-title=\"Auto-SSH-VNC-03\" data-image-description=\"\" data-image-caption=\"\" data-large-file=\"https:\/\/www.fanjoe.be\/wp-content\/uploads\/2015\/05\/Auto-SSH-VNC-03-620x471.jpg\" class=\"alignnone size-full wp-image-1619\" src=\"https:\/\/www.fanjoe.be\/wp-content\/uploads\/2015\/05\/Auto-SSH-VNC-03.jpg\" alt=\"Auto-SSH-VNC-03\" width=\"620\" height=\"471\" srcset=\"https:\/\/www.fanjoe.be\/wp-content\/uploads\/2015\/05\/Auto-SSH-VNC-03.jpg 620w, https:\/\/www.fanjoe.be\/wp-content\/uploads\/2015\/05\/Auto-SSH-VNC-03-175x133.jpg 175w, https:\/\/www.fanjoe.be\/wp-content\/uploads\/2015\/05\/Auto-SSH-VNC-03-300x228.jpg 300w, https:\/\/www.fanjoe.be\/wp-content\/uploads\/2015\/05\/Auto-SSH-VNC-03-460x350.jpg 460w\" sizes=\"auto, (max-width: 620px) 100vw, 620px\" \/><\/a><\/p>\n<p>Fichier &gt; (Alt) Enregistrer sous&#8230;<\/p>\n<p>Format de fichier : Application<\/p>\n<h2>V\u00e9rifications dans le Terminal<\/h2>\n<p>La commande <code>ps -ax<\/code> permet de lister tous les process en cours.<\/p>\n<p>La commande <code>kill -9 &lt;num process&gt;<\/code> permet d&rsquo;arr\u00eater un process.<\/p>\n<p>On obtient quelque chose comme ceci :<\/p>\n<pre>10995 ??\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 0:00.05 ssh -A -N -L 5900:localhost:5900 user@domain.com vncserver -geometry 1024x768 :0<\/pre>\n<p>O\u00f9, ici, <code>10995<\/code> est le num\u00e9ro du process.<\/p>\n<h2>T\u00e9l\u00e9charger le script<\/h2>\n<p><a href=\"https:\/\/www.fanjoe.be\/wp-content\/uploads\/2015\/05\/Start_Secure_VNC.scpt\" target=\"_blank\">T\u00e9l\u00e9charger le script<\/a><\/p>\n<p><strong>Edit 04 mai 2015 :<\/strong><\/p>\n<p>Apr\u00e8s quelques ex\u00e9cutions, le script qui semblait fonctionner correctement s&rsquo;est mis \u00e0 refuser de de lancer VNC c\u00f4t\u00e9 serveur.<\/p>\n<p>Tout rentre dans l&rsquo;ordre en modifiant les premi\u00e8res lignes comme suit :<\/p>\n<pre>do shell script \"ssh -A -N -L 5900:localhost:5900 user@domain.com &amp;&gt; \/dev\/null &amp; echo $!\"\r\nset sshpid to the result\r\ndo shell script \"ssh user@domain.com vncserver -geometry 1024x768 :0 &amp;&gt; \/dev\/null &amp; echo $!\"\r\ndelay 2<\/pre>\n<p><strong>R\u00e9f\u00e9rences :<\/strong><\/p>\n<p><a href=\"https:\/\/theory14.wordpress.com\/2010\/04\/24\/automating-ssh-tunnels-on-mac-os-x\/\" target=\"_blank\">Automating SSH Tunnels on Mac OS X<\/a><br \/>\n<a href=\"http:\/\/www.chriswrites.com\/how-to-view-and-kill-processes-using-the-terminal-in-mac-os-x\/\" target=\"_blank\">How to View and Kill Processes Using the Terminal in Mac OS X<\/a><br \/>\n<a href=\"https:\/\/stackoverflow.com\/questions\/4067176\/quit-an-application-using-applescript\" target=\"_blank\">Quit an application using applescript<\/a><br \/>\n<a href=\"http:\/\/alvinalexander.com\/blog\/post\/mac-os-x\/applescript-customizing-dialog-boxes\" target=\"_blank\">AppleScript dialog tip: customizing dialog boxes<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Introduction <\/p>\n<p>Dans un <a href=\"https:\/\/www.fanjoe.be\/?p=1561\">article pr\u00e9c\u00e9dent<\/a>, nous avons vu comment \u00e9tablir un tunnel SSH pour permettre une connexion VNC (Partage&#8230;<\/p>\n","protected":false},"author":1,"featured_media":1620,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":"","jetpack_post_was_ever_published":false},"categories":[10,3,178,12],"tags":[171,172,152,170,168,39],"class_list":["post-1615","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-geek","category-informatique","category-mac","category-raspberrypi","tag-applescript","tag-auto","tag-os-x","tag-ssh","tag-tunnel","tag-vnc","end"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"https:\/\/www.fanjoe.be\/wp-content\/uploads\/2015\/05\/Auto-SSH-VNC-00.png","_links":{"self":[{"href":"https:\/\/www.fanjoe.be\/index.php?rest_route=\/wp\/v2\/posts\/1615","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.fanjoe.be\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.fanjoe.be\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.fanjoe.be\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.fanjoe.be\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1615"}],"version-history":[{"count":0,"href":"https:\/\/www.fanjoe.be\/index.php?rest_route=\/wp\/v2\/posts\/1615\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.fanjoe.be\/index.php?rest_route=\/wp\/v2\/media\/1620"}],"wp:attachment":[{"href":"https:\/\/www.fanjoe.be\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1615"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.fanjoe.be\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1615"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.fanjoe.be\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1615"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}