0

I just wrote an Automator app using AppleScript (along with a couple of bash lines) to start FTP with a single click. However it runs more than a tad slow. Here's the code:

set ftpstatus to "off"
try
    do shell script "echo \"QUIT\" | telnet 127.0.0.1" & " ftp 2>&1 | grep  \"Escape character is\" > /dev/null"
    set ftpstatus to "on"
on error
    set ftpstatus to "off"
end try
if (ftpstatus = "off") then
    set ipaddr to IPv4 address of (get system info)
    set sun to short user name of (get system info)
    do shell script "sudo -s launchctl load -w /System/Library/LaunchDaemons/ftp.plist" with administrator privileges
    tell application "Finder" to display alert "FTP Launched and ready for file-transfer" & character id 8233 & character id 8233 & character id 8233 & "Address: ftp://" & ipaddr & ":21" & character id 8233 & "User Name: " & sun
else
    do shell script "sudo -s launchctl unload -w /System/Library/LaunchDaemons/ftp.plist" with administrator privileges
    tell application "Finder" to display alert "FTP session closed"
end if

Here's what the code does:

  1. Check if FTP server is running
  2. If yes, turn it off and throw a message box saying "FTP session closed"
  3. If no, turn it on and throw a message box saying "FTP session open" along with my IP address and username

This script works like charm but since it's too slow, I am wondering if there's any way to convert it to a bash shell script. That should speed up things a lot. Any suggestions?

2

I'm not sure I understand correctly - if in the third line of Your script You're checking whether ftp service is running You can use following script:

#!/bin/bash                                                                                                                   

launchctl list | grep ftpd

if [ $? != 0 ]; then
    IPADDR=$(ifconfig -a | perl -nle'/(\d+\.\d+\.\d+\.\d+)/ && print $1' | grep -v 127.0.0.1)
    launchctl load "/System/Library/LaunchDaemons/ftp.plist"
    osascript -e "tell application \"Finder\" to display alert \"FTP Launched and ready for file-transfer\" & character id 8233 & character id 8233 & character id 8233 & \"Address: ftp://\" & \"$IPADDR\" & \":21\" & character id 8233 & \"User Name: \" & \"$USER\""
else
    launchctl unload -w "/System/Library/LaunchDaemons/ftp.plist"
    osascript -e 'tell application "Finder" to display alert "FTP session closed"'
fi

Run this a sudo. Explanation:

  • To check if job is running use launchctl list.
  • To get ip address quickly use my perl script (You may want to grep it differently in order to get rid of additional IP addresses.
  • The only problem now is that Finder app is not brought to front but this can be solved if needed.
  • The assignment to IPADDR assumes that there is only one active network interface besides localhost – nohillside Jul 29 '14 at 14:31
  • 1
    @patrix that is correct. I've mentioned about that in my answer. To get one specific interface IP script would need to be changed. – Mateusz Szlosek Jul 29 '14 at 14:34
  • Upps, right, didn't notice that – nohillside Jul 29 '14 at 14:35
  • 1
    @AmitSchandillia You may insert this script to AppleScript editor, add line like: do shell script ... with administrator privileges You'll be prompted to input Your password. Another way is to make an application which will launch Your script with sudo. Generally executables properly enclosed inside *.app bundle does not have to launch Terminal. – Mateusz Szlosek Jul 29 '14 at 15:45
  • 1
    So it's a bit risky to let apps do the work with elevated privilages. However I can help You compile ObjC application to launch bashscript. We may try to ask for passowrd with a prompt, or something with setuid/setgid. Here are some informations about differences: developer.apple.com/library/mac/documentation/security/… – Mateusz Szlosek Jul 29 '14 at 16:32

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .