• Apfeltalk ändert einen Teil seiner Allgemeinen Geschäftsbedingungen (AGB), das Löschen von Useraccounts betreffend.
    Näheres könnt Ihr hier nachlesen: AGB-Änderung
  • Was gibt es Schöneres als den Mai draußen in der Natur mit allen Sinnen zu genießen? Lasst uns teilhaben an Euren Erlebnissen und macht mit beim Thema des Monats Da blüht uns was! ---> Klick

Droplet/Folder Action zum übertragen von Dateien über SSH

fyysh

Schweizer Glockenapfel
Registriert
25.01.10
Beiträge
1.386
Hier ein kleines -Script zum übertragen von Dateien über SSH auf andere Rechner.

Das Script muss alls app gespeichert, um als Droplet verwendet werden zu können.
Um es als Folder Action zu verwenden, kann man es als Script speichern.
Ausführen des Scripts im Applescript Editor oder als App wird nach Dateien oder Ordnern fragen (kann man zum testen verwenden).

Es funktioniert allerdings nur, wenn man sich mit Key, also ohne Passworteingabe, auf dem Zielrechner anmelden kann.
Wie das einrichtet wird, ist bspw. hier beschrieben.

Das Script verwendet so, wie es unten steht, rsync (gibt brauchbaren output), kann aber auch scp verwenden.
Wenn growlnotify installiert ist, dann wird es eine Ausgabe über Growl vornehmen. Ansonsten gibt's ein Dialogfenster.

Ich hoffe es ist jemandem von Nutzen. :)


Code:
(* SSH UPLOAD APP/DROPLET/FOLDER ACTION *)
------------------------------------------------------------------------------------------
[B]property[/B] sshDestAdress : "host"
[B]property[/B] sshDestPort : "22" --standard port is 22
[B]property[/B] sshDestUserName : "user"
[B]property[/B] sshDestPath : "/some/path"


[B]property[/B] sshOpt : "-o 'BatchMode=yes' -o 'Port=" & sshDestPort & "'" --see man ssh


-- with rsync (see man rsync)
[B]property[/B] shCmd : "rsync"
[B]property[/B] shCmdOpt : "-ah --stats -e " & quoted form [B]of[/B] ("ssh " & sshOpt)


(*
--should also work with scp (see man scp)
property shCmd : "scp"
property shCmdOpt : "-r " & sshOpt
*)


[B]property[/B] testTimeout : 5 --seconds for ping & ssh connection test timeout
------------------------------------------------------------------------------------------




[B]on[/B] [B]run[/B]
    --app opened or script editor
    [B]set[/B] transferWhat [B]to[/B] ""
    [B]set[/B] transferWhat [B]to[/B] button returned [B]of[/B] ([B]display dialog[/B] "Test file or folder?" buttons {"Files", "Folders"} default button 1 with title "Test" giving up after 30)
    [B]if[/B] transferWhat = "Files" [B]then[/B]
        [B]set[/B] aliasList [B]to[/B] ([B]choose file[/B] [B]with[/B] multiple selections allowed)
    [B]else[/B] [B]if[/B] transferWhat = "Folders" [B]then[/B]
        [B]set[/B] aliasList [B]to[/B] ([B]choose folder[/B] [B]with[/B] multiple selections allowed)
    [B]else[/B]
        [B]return[/B]
    [B]end[/B] [B]if[/B]
    
    main(aliasList)
[B]end[/B] [B]run[/B]




[B]on[/B] [B]open[/B] (droppedFiles)
    --droplet
    main(droppedFiles)
[B]end[/B] [B]open[/B]




[B]on[/B] [B]adding folder items to[/B] this_folder after receiving added_items
    --folder action
    main(added_items)
[B]end[/B] [B]adding folder items to[/B]




[B]on[/B] main(aliasList)
    [B]set[/B] sshDest [B]to[/B] sshDestUserName & "@" & sshDestAdress & ":" & sshDestPath -- sshDest = user@host:/some/path
    [B]set[/B] notification [B]to[/B] (sshTransfer([B]my[/B] shCmd, [B]my[/B] shCmdOpt, getQuetedFileList(aliasList), sshDest))
    notify(notification)
    [B]return[/B]
[B]end[/B] main








[B]to[/B] notify(notification)
    -- if growlnotify is installed, use it, else display dialog
    [B]set[/B] nTitle [B]to[/B] "SSH Upload Succeeded"
    [B]try[/B]
        [B]set[/B] notification [B]to[/B] ([B]my[/B] name & return & return & nTitle & return & notification)
        --error --uncomment to always show dialog instead of using growlnotify
        [B]do shell script[/B] "test -f '/usr/local/bin/growlnotify' && printf " & quoted form [B]of[/B] notification & "|/usr/local/bin/growlnotify"
    [B]on[/B] [B]error[/B]
        [B]tell[/B] [B]me[/B] [B]to[/B] [B]activate[/B]
        [B]display alert[/B] nTitle message notification buttons {"OK"} default button 1 as informational giving up after 15
    [B]end[/B] [B]try[/B]
[B]end[/B] notify




[B]to[/B] sshTransfer(shCmd, shCmdOpt, quotedPosixFileList, sshDest)
    [B]try[/B]
        [B]if[/B] cannotConnect() [B]then[/B] ¬
            [B]error[/B] "Cannot connect to " & sshDestAdress & return & ¬
                "Ping failed"
        
        [B]if[/B] cannotLogin() [B]then[/B] ¬
            [B]error[/B] "Cannot login to " & sshDestAdress & return & ¬
                "or cannot access " & sshDestPath & "."
        
        [B]set[/B] cmdOutput [B]to[/B] ([B]do shell script[/B] (shCmd & space & shCmdOpt & space & quotedPosixFileList & space & sshDest & " 2>&1"))
        [B]return[/B] cmdOutput
    [B]on[/B] [B]error[/B] e
        [B]set[/B] alertMsg [B]to[/B] "shCmd=" & shCmd & return & ¬
            "shCmdOpt=" & shCmdOpt & return & ¬
            "sshDestAdress=" & sshDestAdress & return & ¬
            "sshDestPort=" & sshDestPort & return & ¬
            "sshDestUserName=" & sshDestUserName & return & ¬
            "sshDestPath=" & sshDestPath & return & return & ¬
            "Error:" & return & ¬
            e
        [B]tell[/B] [B]me[/B] [B]to[/B] [B]activate[/B]
        [B]display alert[/B] "SSH Upload failed" message alertMsg buttons {"OK"} default button 1 cancel button 1 as warning giving up after 30
    [B]end[/B] [B]try[/B]
[B]end[/B] sshTransfer




[B]to[/B] cannotLogin()
    --runs: ssh user@host 'ls /some/path'
    --returns true if ssh command doesnt return 0 or sshDestPath cannot be listed within testTimeout seconds
    [B]with[/B] [B]timeout[/B] [B]of[/B] [B]my[/B] testTimeout [I]seconds[/I]
        [B]try[/B]
            [B]do shell script[/B] ¬
                "ssh " & [B]my[/B] sshOpt & space & [B]my[/B] sshDestUserName & "@" & [B]my[/B] sshDestAdress & space & quoted form [B]of[/B] ("ls " & sshDestPath)
            [B]return[/B] false
        [B]end[/B] [B]try[/B]
    [B]end[/B] [B]timeout[/B]
    [B]return[/B] true
[B]end[/B] cannotLogin




[B]to[/B] cannotConnect()
    --runs: ping -c1 host
    --returns true if ping doesnt return 0 or sshDestAdress doesnt answer within testTimeout seconds
    [B]with[/B] [B]timeout[/B] [B]of[/B] [B]my[/B] testTimeout [I]seconds[/I]
        [B]try[/B]
            [B]do shell script[/B] "ping -c1 " & [B]my[/B] sshDestAdress
            [B]return[/B] false
        [B]end[/B] [B]try[/B]
    [B]end[/B] [B]timeout[/B]
    [B]return[/B] true
[B]end[/B] cannotConnect




[B]to[/B] getQuetedFileList(aliasList)
    --get every alias from the file list and convert it to a string of quoted posix paths
    -- gets: fileList={"some:path:to:item1","some:path:to:item2"}
    --returns: myResult="'some/path/to/item1' 'some/path/to/item2'"
    
    [B]set[/B] myResult [B]to[/B] {}
    [B]repeat[/B] [B]with[/B] aFile [B]in[/B] aliasList
        [B]if[/B] ([B]last[/B] [I]text item[/I] [B]of[/B] (aFile [B]as[/B] [I]string[/I])) = ":" [B]then[/B] [B]set[/B] aFile [B]to[/B] ([I]text[/I] 1 [B]thru[/B] -2 [B]of[/B] (aFile [B]as[/B] [I]string[/I])) --ensure folders dont have a trailing ":", important for rsync
        [B]set[/B] myResult [B]to[/B] myResult & (quoted form [B]of[/B] POSIX path [B]of[/B] aFile)
    [B]end[/B] [B]repeat[/B]
    [B]set[/B] ATIDs [B]to[/B] AppleScript's text item delimiters
    [B]set[/B] AppleScript's text item delimiters [B]to[/B] space
    [B]set[/B] myResult [B]to[/B] (myResult [B]as[/B] [I]string[/I])
    [B]set[/B] AppleScript's text item delimiters [B]to[/B] ATIDs
    [B]return[/B] myResult
[B]end[/B] getQuetedFileList
 
Zuletzt bearbeitet: