Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

brap

macrumors 68000
Original poster
May 10, 2004
1,705
3
Nottingham
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?
 
brap said:
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?
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).
 
wrldwzrd89 said:
...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
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.
 
brap said:
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.
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.
 
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
 
wrldwzrd89 said:
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.

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.
 
bousozoku said:
The following is an example of an AppleScript that has the Terminal application run shell scripts....
Code:
tell application "Terminal"
	activate
	do script "cd fahproc1; ./fah.sh"
	do script "cd fahproc2; ./fah.sh"
end tell
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 :)
 
brap said:
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 ;)

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.
 
brap said:
I'm looking for a way to send plain text to the cursor (IE, not the shell) which doesn't sound possible.
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()
 
rand() said:
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()
Actually the Mac OS X built in FTP client won't work as it doesn't support uploads.
 
kingjr3 said:
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.
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:
 
wrldwzrd89 said:
Actually the Mac OS X built in FTP client won't work as it doesn't support uploads.

Well jeez, man, I didn't even know that. I guess I'd never even tried the "easy way" myself!

:D

-rand()
 
brap said:
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:

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???
 
wrldwzrd89 said:
Actually the Mac OS X built in FTP client won't work as it doesn't support uploads.

That's only 50 percent accurate. The GUI version doesn't support uploads. The text-based version, as always, does everything necessary. ;)
 
bousozoku said:
That's only 50 percent accurate. The GUI version doesn't support uploads. The text-based version, as always, does everything necessary. ;)
I was referring to the GUI client. I guess I didn't qualify my post very well :)
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.