{"id":1488,"date":"2012-12-17T16:33:24","date_gmt":"2012-12-17T16:33:24","guid":{"rendered":"http:\/\/blog.designed79.co.uk\/?p=1488"},"modified":"2012-12-20T09:48:18","modified_gmt":"2012-12-20T09:48:18","slug":"1488","status":"publish","type":"post","link":"https:\/\/blog.designed79.co.uk\/?p=1488","title":{"rendered":"Screensaver lots of Macs at once"},"content":{"rendered":"<p>My work setup in the lab consists of two Mac Minis, one Mac Pro, and my Macbook Pro. I do all my typing on my Macbook and use teleport to remotely control the other computers. Whenever I leave my desk, I make it a habit to lock all my computers using the screensaver. It has become a bit of a pain to do this manually, so I came up with a way to lock all my computers with a single command from my Macbook. <\/p>\n<p>The idea is to use ssh to remotely run a command that will activate the screensaver on the target computer. In order for this to work properly, SSH keys should be setup between the computer sending the lock request (my Macbook) and the targets (Mac Minis and Mac Pro) so that no password input is required. Read up on ssh-keygen and ssh-agent for details on how to do this.<\/p>\n<p>Here&#8217;s the script:<\/p>\n<div class=\"codecolorer-container bash default\" style=\"overflow:auto;white-space:nowrap;\"><table cellspacing=\"0\" cellpadding=\"0\"><tbody><tr><td class=\"line-numbers\"><div>1<br \/>2<br \/>3<br \/>4<br \/>5<br \/>6<br \/>7<br \/>8<br \/>9<br \/>10<br \/>11<br \/>12<br \/>13<br \/>14<br \/>15<br \/>16<br \/>17<br \/>18<br \/>19<br \/><\/div><\/td><td><div class=\"bash codecolorer\"><span class=\"co0\">#!\/bin\/bash<\/span><br \/>\n<span class=\"kw1\">function<\/span> lock_request <span class=\"br0\">&#123;<\/span><br \/>\n&nbsp; &nbsp; <span class=\"re2\">user<\/span>=<span class=\"re4\">$1<\/span><br \/>\n&nbsp; &nbsp; <span class=\"re2\">target<\/span>=<span class=\"re4\">$2<\/span><br \/>\n&nbsp;<br \/>\n&nbsp; &nbsp; <span class=\"co0\"># lock the screen only if the target is alive.<\/span><br \/>\n&nbsp; &nbsp; <span class=\"kw2\">ping<\/span> <span class=\"re5\">-c1<\/span> <span class=\"re1\">$target<\/span> <span class=\"nu0\">2<\/span><span class=\"sy0\">&gt;&amp;<\/span><span class=\"nu0\">1<\/span> <span class=\"sy0\">&gt;<\/span> <span class=\"sy0\">\/<\/span>dev<span class=\"sy0\">\/<\/span>null<br \/>\n&nbsp; &nbsp; <span class=\"kw1\">if<\/span> <span class=\"br0\">&#91;<\/span><span class=\"br0\">&#91;<\/span> <span class=\"re4\">$?<\/span> <span class=\"re5\">-eq<\/span> <span class=\"nu0\">0<\/span> <span class=\"br0\">&#93;<\/span><span class=\"br0\">&#93;<\/span>; <span class=\"kw1\">then<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; <span class=\"kw2\">ssh<\/span> <span class=\"co1\">${user}<\/span><span class=\"sy0\">@<\/span><span class=\"co1\">${target}<\/span> open <span class=\"sy0\">\/<\/span>System<span class=\"sy0\">\/<\/span>Library<span class=\"sy0\">\/<\/span>Frameworks<span class=\"sy0\">\/<\/span>ScreenSaver.framework<span class=\"sy0\">\/<\/span>Versions<span class=\"sy0\">\/<\/span>A<span class=\"sy0\">\/<\/span>Resources<span class=\"sy0\">\/<\/span>ScreenSaverEngine.app<br \/>\n&nbsp; &nbsp; <span class=\"kw1\">fi<\/span><br \/>\n<span class=\"br0\">&#125;<\/span><br \/>\n&nbsp;<br \/>\n<span class=\"co0\"># lock remote targets<\/span><br \/>\nlock_request johndoe 192.168.123.181 <span class=\"sy0\">&amp;<\/span><br \/>\nlock_request johndoe 192.168.123.168 <span class=\"sy0\">&amp;<\/span><br \/>\nlock_request johndoe 192.168.123.146 <span class=\"sy0\">&amp;<\/span><br \/>\n&nbsp;<br \/>\n<span class=\"co0\"># lock local screen<\/span><br \/>\nopen <span class=\"re5\">-a<\/span> ScreenSaverEngine<\/div><\/td><\/tr><\/tbody><\/table><\/div>\n<p>The lock_request function expects two parameters, the login name and the IP address of the target. The first thing the function does is to ping the target to see if it&#8217;s alive. If it is, it connects to the target using ssh and executes the open command on ScreenSaverEngine.app which is what launches the screensaver. Once all targets are locked, it locks the local computer as well. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>My work setup in the lab consists of two Mac Minis, one Mac Pro, and my Macbook Pro. I do all my [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-1488","post","type-post","status-publish","format-standard","hentry","category-info-on-tech"],"_links":{"self":[{"href":"https:\/\/blog.designed79.co.uk\/index.php?rest_route=\/wp\/v2\/posts\/1488","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.designed79.co.uk\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.designed79.co.uk\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.designed79.co.uk\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.designed79.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1488"}],"version-history":[{"count":0,"href":"https:\/\/blog.designed79.co.uk\/index.php?rest_route=\/wp\/v2\/posts\/1488\/revisions"}],"wp:attachment":[{"href":"https:\/\/blog.designed79.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1488"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.designed79.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1488"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.designed79.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1488"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}