AppleScript C Perl Shell Xcode Other

FTP-like commands and remote osascript on Macs

Post Reply
coding / applescript     Views: 865Prev .. Next
FTP-like commands and remote osascript on MacsPosted: Saturday, November 29, 2014 [16:56:35] - 1
rootPosted by:rootMember Since:
June 16 2010
Posts: 168
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:
View Codecd ~
mkdir .ssh

Change permissions to read/write/execute for the user and cd to .ssh.
View Codechmod go-rwx .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:
View Codessh-keygen -b 1024 -t rsa -f id_rsa -P ""

check the dir content:
View Codels -la

-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:
View Codecat id_rsa.pub

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:
View Codessh remoteMac.local -l username

cd ~
mkdir .ssh
cd .ssh

create authorized_keys file:
View Codetouch authorized_keys

now edit authorized_keys file:
View Codesudo pico authorized_keys

paste your public key from above to authorized_keys file on remote Mac:
View Codecommand+v
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:
View Codechmod u-w authorized_keys


now, both Macs configured. To login to remote Mac, just enter:
View Codessh remoteMac.local -l username
or
ssh 192.168.X.XX -l username

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:
View Codealias macrm="ssh 192.168.X.XX -l username"

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.There's no place like ~
RE: FTP-like commands and remote osascript on MacsPosted: Saturday, November 29, 2014 [17:24:06] - 2
rootPosted by:rootMember Since:
June 16 2010
Posts: 168
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:
View Codetry
set dPresent to do shell script "ssh [email protected] ls -la /testDir"
on error
do shell script "ssh [email protected] mkdir -p /testDir"
delay 2
set dPresent to do shell script "ssh [email protected] ls -la /testDir"
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:
View Codeif dPresent contains "drw" then
try
set imgpres to do shell script "ssh [email protected] ls -la /testDir/sunny.jpg"
on error
do shell script "scp /localDir/sunny.jpg [email protected]:/testDir/sunny.jpg"
delay 1
set imgpres to do shell script "ssh [email protected] ls -la /testDir/sunny.jpg"
end try
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.
View Codedo shell script "scp /localDir/myScript.scpt [email protected]:/remoteDir/runThis.scpt"

and then run this AppleScript remotely and once done - remove the script from remote Mac:
View Codetry
do shell script "ssh [email protected] osascript /remoteDir/runThis.scpt"
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.There's no place like ~
coding / applescriptPrev .. Next
 
Post Reply
Home - Coding: AppleScript C Perl Shell Xcode Other