Terminal FTP Script

I need to write a script that when called on, will log into an FTP site, with usernames, and passwords passed as variables.

Any ideas?

Roy

Need more info here. What task(s) are you trying to accomplish?

I will be wanting to effectivally type:
ftp rpm-repro.ftpsteam.com

and then at the prompt enter the username (conatined as a variable)
and likewise for the password.

When a connection is established, I need to use the ‘put’ command with a variable file name.

Is this enough info? I will happily give any more details if needed.

Cheers

Roy

What kind of workflow are you trying to achieve? Do you just need to upload a bunch of files without interacting, or does the user need to interact with the upload process?

The end-user workflow is key to pointing you in the right direction. :slight_smile:

hi roy,

regular readers will know that i am on an ‘Expect’ kick for this kind of stuff:

http://bbs.applescript.net/viewtopic.php?id=17779
http://bbs.applescript.net/viewtopic.php?id=17733

and here is a good reference on Expect:

http://en.wikipedia.org/wiki/Expect

Expect comes installed on every OS X Macintosh, and it allows you to script an interactive session when you know what to ‘expect’ from the program you are trying to interact with.

here is a generalized FTP client using Expect to log in, and AppleScript as a GUI wrapper. you should be able to see from this what is going on (and easily hard code the server, username and password and get rid of the ‘display dialog’s’):


property myExpect : "#!/usr/bin/expect 
spawn /usr/bin/ftp [lindex \\$argv 0]
set user [lindex \\$argv 1]
expect \"login: \"
send \"\\$user
\"
set password [lindex \\$argv 2]
expect \"Password:\"
send \"\\$password
\"
interact"

set myServer to text returned of (display dialog "What Server would you like to connect to?" default answer "")

set myUser to text returned of (display dialog "What is the username for this ftp session?" default answer "")

set myPass to text returned of (display dialog "What is the password for this ftp session?" default answer "" with hidden answer)

do shell script "/bin/cat > /tmp/expectFTP << EOF 
" & myExpect & "
EOF" with administrator privileges
do shell script "/bin/chmod +x /tmp/expectFTP" with administrator privileges

tell application "Terminal"
	activate
	do script with command ¬
		"/tmp/expectFTP " & myServer & space & quoted form of myUser & space & quoted form of myPass --in window 1
end tell

delay 10
do shell script "/bin/rm -rf /tmp/expectFTP" with administrator privileges

this script creates the Expect script in the /tmp directory, and then later deletes the file, but you could just leave it in a /bin directory somewhere and then call it with an AppleScript that gives the arguments it is looking for. you could add your ‘put’ statements to this at the end and i think you’d have a very workable solution.

questions welcome!

EDITED: bad grammar!

ah, one more thing i’d like to note:

consider the above script to be a ‘debug’ version, so that you can actually see the FTP commands in the Terminal window. you do not have to use the Terminal to do this, and if you get it to the point where you’ve scripted a whole session, replace the Terminal block as follows:


property myExpect : "#!/usr/bin/expect 
spawn /usr/bin/ftp [lindex \\$argv 0]
set user [lindex \\$argv 1]
expect \"login: \"
send \"\\$user
\"
set password [lindex \\$argv 2]
expect \"Password:\"
send \"\\$password
\"
interact"

set myServer to text returned of (display dialog "What Server would you like to connect to?" default answer "")

set myUser to text returned of (display dialog "What is the username for this ftp session?" default answer "")

set myPass to text returned of (display dialog "What is the password for this ftp session?" default answer "" with hidden answer)

do shell script "/bin/cat > /tmp/expectFTP << EOF 
" & myExpect & "
EOF" with administrator privileges
do shell script "/bin/chmod +x /tmp/expectFTP" with administrator privileges


do shell script "/tmp/expectFTP " & myServer & space & quoted form of myUser & space & quoted form of myPass with administrator privileges


delay 10
do shell script "/bin/rm -rf /tmp/expectFTP" with administrator privileges

CAVEAT SCRIPTER: if you run the above as written you will open an FTP session that you CANNOT control. you’ll have to go to the server and end the connection. this is only an example for when you’ve scripted and entire session, including a ‘quit’.

Before posting all of that expect code, I probably would have recommended waiting to see what the OP actually needed to do. If he doesn’t need an interactive session, and just needs to put files somewhere, he may be inadvertently led down a far more complex path than necessary.

He may simply need to put a file somewhere, which curl can do in a single line, after all. (Not saying, of course, that this is his need.)

hi Mikey,

true enough. i’m guilty of doing this more as an excercise for myself than anything.

do you know if there is a “Scripters Anonymous” out there somewhere? i find this stuff so compulsive sometimes.

Thanks for the input.

I am not needing to see a treminal screen for this process, but it wouldn’t hurt if it was there.
The workflow I am trying to acheive is I have an automated Page layout system going on (using JavaScript inside InDesign) which will spit out a PDF file. This file will then (presumably) go into a hotfolder that will trigger off an applescript to upload the PDF file to the customers area on our FTP Server. Ideally with an automatically generated email message too.

I know it is all possible, it is just putting the jigsaw together!

Any more points would be willingly recieved

Cheers

Roy

I have had a play, and put in the name and password (as this is the way I will be working)
How can I make the script work without asking for the administrator password?
I can see what is happening in Terminal, but I cannot figure where I need to put the line “put filepath/filename filename”
Also, and I know I am being a real pain, but if someone could comment on the lines, so I know what and why tinks are needed. I am not a person who is happy just doing it, I need to know why.

Thanks for the time,

Roy

adjusted code below

property myExpect : “#!/usr/bin/expect
spawn /usr/bin/ftp [lindex \$argv 0]
set user [lindex \$argv 1]
expect "login: "
send "\$user
"
set password [lindex \$argv 2]
expect "Password:"
send "\$password
"
interact”

do shell script “/bin/cat > /tmp/expectFTP << EOF
" & myExpect & "
EOF” with administrator privileges
do shell script “/bin/chmod +x /tmp/expectFTP” with administrator privileges

tell application “Terminal”
activate
do script with command ¬
"/tmp/expectFTP " & “rpm-repro.ftpstream.com” & space & quoted form of “Name” & space & quoted form of “Password” --in window 1

end tell

delay 10
do shell script “/bin/rm -rf /tmp/expectFTP” with administrator privileges

I agree with Mikey-San - seems to me like curl is the easier way. Especialy if he requires no user interaction…

curl -u user:pass  ftp://ftp.myftpsite.com/ -T /Users/username/Desktop/testfile.pdf 

yes, now seeing that the file doesn’t change name, no user interaction, etc., ‘curl’ does seem like a better option. in AppleScript:


set myName to "Name"
set myPass to "Password"
set myFile to "/POSIX/path/to/my/file"
set myCurl to "curl -u " & myName & ":" & myPassword & "  ftp://rpm-repro.ftpstream.com/ -T " & myFile

do shell script myCurl

i still enjoyed scripting this in Expect :stuck_out_tongue:

Hi Guys

Thanks heaps for the valuable input. I am ending up using curl, as it is easier for me to understand!!!

Thanks again,

Roy