Scripting the Terminal

Discussion in 'Mac Apps and Mac App Store' started by brap, Sep 28, 2004.

  1. brap macrumors 68000

    Joined:
    May 10, 2004
    Location:
    Nottingham
    #1
    Hi all...

    Perhaps there's some UNIX/Terminal/AppleScript guru who can help me; my university recently cut off VPN access for students, and the only way I can now get to my storage is through ftp in the terminal. Well, they say use PuTTY, but hey, we're elite ;)

    So, I want to automate my login. It'll be the same every time - ftp [host], [password], change directory, [password], ls. I can't seem to do this with the
    Code:
    do shell script
    thing, because i get an EOF error. Anyone done this before themselves?
     
  2. wrldwzrd89 macrumors G5

    wrldwzrd89

    Joined:
    Jun 6, 2003
    Location:
    Solon, OH
    #2
    My suggestion is to open Script Editor, choose Open Dictionary, navigate to Terminal, and select Terminal as the application to open the dictionary for. Look around in there - you should find the command(s) you need. I've never scripted Terminal before, but I know it can be done (in fact I haven't written very many AppleScripts).
     
  3. brap thread starter macrumors 68000

    Joined:
    May 10, 2004
    Location:
    Nottingham
    #3
    Me either, and I have done rudimentary googling and searching. The dictionary just gave a deprecated command "with command" which now refuses to compile - hence my asking.
     
  4. wrldwzrd89 macrumors G5

    wrldwzrd89

    Joined:
    Jun 6, 2003
    Location:
    Solon, OH
    #4
    Actually, you're better off writing a shell script to automate this task and putting it in "~/.profile" so your shell loads it automatically. Don't forget the hashpling for the script's first line (for example: "#!/bin/bash" if you want the bash shell to run the script). You can background a task if you want to by putting an ampersand (&) after it. For example: "top" runs the top command in the foreground; "top &" runs the top command in the background.
     
  5. bousozoku Moderator emeritus

    Joined:
    Jun 25, 2002
    Location:
    Gone but not forgotten.
    #5
    The following is an example of an AppleScript that has the Terminal application run shell scripts. The way I've done it (do script) makes everything visible, which is different that the way (do shell script) you've done it that keeps it invisible.

    Code:
    tell application "Terminal"
    	activate
    	do script "cd fahproc1; ./fah.sh"
    	do script "cd fahproc2; ./fah.sh"
    end tell
    
     
  6. rand() macrumors regular

    Joined:
    Jul 15, 2004
    Location:
    Michigan
    #6
    Exactly. Here's a shell script I've written which you could modify to suit this purpose. It uses a pipe and a temporary file, which makes the ftp thing work - otherwise it's near impossible (in my limited experience) to correctly script the ftp command. The script as is takes user arguments in this form:

    Code:
    uploader.sh file1 uploadname1 file2 uploadname2 
    where file1 is the name of the file, and uploadname is the file's name as it is uploaded to the server.

    Code:
    #!/bin/sh
    HOST='www.yourhost.com'
    USER='user'
    PASSWD='password'
    echo quote USER $USER > .tmpScript
    echo quote PASS $PASSWD >> .tmpScript
    echo 'cd directory' >> .tmpScript
    
    while [ "$1" ]
    do
            FILE="$1"
            AS="$2"
            echo put $FILE $AS >> .tmpScript
            shift 2
    done
    
    echo 'quit' >> .tmpScript
    cat .tmpScript | ftp -n $HOST
    rm .tmpScript
    exit 0 
    This could be fairly easily adapted for use in Applescript with the 'do shell script' command as well.
     
  7. brap thread starter macrumors 68000

    Joined:
    May 10, 2004
    Location:
    Nottingham
    #7
    OK, this looks to be the way to go. Basically, I'm cool with using the shell to upload and download files manually, I just wanted to automate the login process - ideally, wrap the script into an application I can just run from my dock... but my code, so far, opens multiple windows when I really want to send more text to the one Terminal window.
    Now that I've thought about it, I'm looking for a way to send plain text to the cursor (IE, not the shell) which doesn't sound possible. Example, script starts by connecting to the server (succesfully), which asks for my username and destination server; it's a gateway to the internal network. I need to send text there and fake an 'enter' being pressed... something I'm not even so sure I could do with the shell script option.

    ...shell script which, incidentally could prove quite useful. I'd not even considered automating the upload/download, so saving time there might just make the manual login bearable.

    Thanks for the responses so far, it's been a long time since I did any scripting, and you're dredging up all kinds of old memories :)
     
  8. jeremy.king macrumors 603

    jeremy.king

    Joined:
    Jul 23, 2002
    Location:
    Fuquay Varina, NC
    #8
    Sounds like they also support SFTP which means you can use Fugu to get your files using a GUIed app. PuTTy is a PC app with SSH implementation.
     
  9. coconn06 macrumors regular

    Joined:
    Jun 14, 2003
    Location:
    King of Prussia, PA
    #9
    Transmit is another GUI FTP app which supports SFTP. A great app, I might add.
     
  10. rand() macrumors regular

    Joined:
    Jul 15, 2004
    Location:
    Michigan
    #10
    Yeah, unfortunately sending keystrokes to various applications isn't implemented in basic applescript yet, although I think there are Scripting Extensions floating around somewhere that can enable this.

    Also, if you're looking to make life super easy (if this will even work w/ your university's setup) you could just type in the ftp://www.address.edu into the Connect to Server...(Cmd-K) box. OS X can mount any FTP or SFTP with login like a regular filesystem. Also, you could save your USER/PASS in the Keychain and then things are REALLY simple.

    But who likes the easy way out? :D

    -rand()
     
  11. wrldwzrd89 macrumors G5

    wrldwzrd89

    Joined:
    Jun 6, 2003
    Location:
    Solon, OH
    #11
    Actually the Mac OS X built in FTP client won't work as it doesn't support uploads.
     
  12. brap thread starter macrumors 68000

    Joined:
    May 10, 2004
    Location:
    Nottingham
    #12
    I'd love to have that option, but I need to login using firstly my username and destination, then with my UKC password in order to have access to the directories. If the ftp server were visible from the outside world without these steps, all would be well... but it's not. I may be able to use Fugu after I've logged in, but the ftp bit isn't the problem :eek:
     
  13. rand() macrumors regular

    Joined:
    Jul 15, 2004
    Location:
    Michigan
    #13
    Well jeez, man, I didn't even know that. I guess I'd never even tried the "easy way" myself!

    :D

    -rand()
     
  14. jeremy.king macrumors 603

    jeremy.king

    Joined:
    Jul 23, 2002
    Location:
    Fuquay Varina, NC
    #14
    I guess I am confused as to what you trying to accomplish
    :confused:

    How exactly is this network setup? You first have to telnet to one machine and then use it to FTP to another???
     
  15. bousozoku Moderator emeritus

    Joined:
    Jun 25, 2002
    Location:
    Gone but not forgotten.
    #15
    That's only 50 percent accurate. The GUI version doesn't support uploads. The text-based version, as always, does everything necessary. ;)
     
  16. wrldwzrd89 macrumors G5

    wrldwzrd89

    Joined:
    Jun 6, 2003
    Location:
    Solon, OH
    #16
    I was referring to the GUI client. I guess I didn't qualify my post very well :)
     

Share This Page