tags:

views:

145

answers:

4

Sorry I'm new to this sort of thing. I'm trying to write a bash script that uploads a file to a server. How can I achieve this? Is a bash script the right thing to use for this?

Solution:

I ended up using scp to copy the files securely.

scp <file to upload> <username>@<hostname>:<destination path>

I also set up public key authentication so I didn't have to enter my password every time I ran my script.

+4  A: 

Install ncftpput and ncftpget. They're usually part of the same package.

Paul Tomblin
http://www.ncftp.com/ncftp/doc/ncftpput.html . Part of "NcFTP" :)
Pascal Cuoq
+2  A: 
#/bin/bash
# $1 is the file name
# usage: this_script  <filename>
IP_address="xx.xxx.xx.xx"
username="username"
domain=my.ftp.domain
password=password

echo "
 verbose
 open $IP_address
 USER $username $password
 put $1
 bye
" | ftp -n > ftp_$$.log
ennuikiller
you mean echo "..." | ftp -n | ftp_$$.log ?
Yossarian
At the moment this writes to a file 'ftp' and doesn't spawn an ftp process
Brian Agnew
@Yossarian: Only the first `>` should be a `|`
Dennis Williamson
@ennuikiller: You have spaces around equal signs that Bash doesn't like. Also, hardcoding passwords in cleartext is a bad idea.
Dennis Williamson
I'm sure the password issue is for illustrative purposes
Brian Agnew
+6  A: 

You can use a heredoc to do this e.g.

ftp -n $Server <<End-Of-Session
# -n option disables auto-logon

user anonymous "$Password"
binary
cd $Directory
put "$Filename.lsm"
put "$Filename.tar.gz"
bye
End-Of-Session

so the ftp process is fed on stdin with everything up to End-Of-Session. A useful tip for spawning any process, not just ftp! Note that this saves spawning a separate process (echo, cat etc.). Not a major resource saving, but worth bearing in mind.

Brian Agnew
+1 This method is invaluable for so many scripts.
Paul Creasey
+1  A: 

You really should use SSH/SCP for this rather than FTP. SSH/SCP have the benefits of being more secure and working with public/preivate keys which allows it to run without a username or password.

For more details on setting up keys and moving files to the server with RSYNC, which is useful if you have a lot of files to move, or if you sometimes get just one new file among a set of random files, take a look at:

http://troy.jdmz.net/rsync/index.html

You can execute a single command after sshing into a server:

http://bashcurescancer.com/run%5Fremote%5Fcommands%5Fwith%5Fssh.html

greggles
I would like to do this! Can you please expand on how I can do this? I need to do some things with ssh after uploading the file. Can this be done in one session?
Andrew
sure, I updated my answer - any other questions?
greggles