FTP-like commands and remote osascript on Macs
Post ReplyFTP-like commands and remote osascript on MacsPosted: Saturday, November 29, 2014 [16:56:35] - 1
Sometimes you need to copy/delete file(s) on remote Mac and do in from the AppleScript. Fire-up an FTP could be a long process or just plain cumbersome. SCP is a copy over the SSH does it much faster but require a password. To run an AppleScript on remote Mac VIA SSH would require the same login password. A way around entering password would be a password prompt-free session. It is not as hard it may seem to do. On Mac that will control the other Mac do the following: mkdir .ssh Change permissions to read/write/execute for the user and cd to .ssh. cd .ssh Create your private and public key. The blank quotes at the end of the command gives the private key no password, so allowing for passwordless logins: check the dir content: -rw------- 1 username staff 887B Jun 1 11:35 id_rsa -rw-r--r-- 1 username staff 239B Jun 1 11:35 id_rsa.pub display your public key and copy it for the Mac you need to login to without a password: ssh-rsa AAAAB3NzaC1yc2EAAAADAQABA... ..MwQtFm5jNAe55AMGiP6qnkQu+j+Q== [email protected] Now, SSH to a remote Mac cd to home dir and create an .ssh directory if not present: create authorized_keys file: now edit authorized_keys file: paste your public key from above to authorized_keys file on remote Mac: control+x pico will ask you to save the file and overwrite existing data. Say "yes" to both. Just in-case, take away write permissions: now, both Macs configured. To login to remote Mac, just enter: and you'll be logged without password prompt. For Terminal sessions you may want to edit your local Mac's .profile file and add alias for SSH login as: once you enter "macrm" in Terminal - you'll be logged to a remote Mac. Please keep in-mind, content of the updated .profile file will be available to your Terminal session after quitting and opening a new Terminal. Passwordless SSH login part is done. |
RE: FTP-like commands and remote osascript on MacsPosted: Saturday, November 29, 2014 [17:24:06] - 2
Now, it's time to access remote Mac from AppleScript! Say, you need to upload local file to remote Mac, check if file exist on remote Mac or simply remove file from remote Mac. List of operations is as long as one can imagine. One thing to consider. Shortcut from .profile file will NOT work from AppleScript as ENV is different in AppleScript and Terminal. Check if directory (Folder) exist on remote Mac and if not - create one: on error end try user - SSH username on remote Mac remoteMac.local - SSH address for remote Mac, IP address could be used as well Now, check if JPEG image is present on remote Mac and if not present - copy one from the local Mac: end if if imgpres contains "drw" then ... end if Now, we know target JPEG file is present on remote Mac, we can run the AppleScript on remote Mac. We can also copy AppleScript from local Mac to remote Mac and run it there, i.e. and then run this AppleScript remotely and once done - remove the script from remote Mac: end try delay 2 do shell script "ssh [email protected] rm -rf /remoteDir/runThis.scpt" As can be seen, any (or almost any) Terminal command can be run remotely from AppleScript or any other programming language VIA SSH without password on remote Mac. |