Find the answer to your Linux question:
Results 1 to 10 of 10
I want to create a script that does the following: 1. login to an FTP server 2. upload a file from the computer to a specific folder on the FTP ...
  1. #1
    Linux User Agent-X's Avatar
    Join Date
    May 2005
    Location
    Dimension X
    Posts
    261

    Automatic FTP upload via script

    I want to create a script that does the following:

    1. login to an FTP server
    2. upload a file from the computer to a specific folder on the FTP server

    It seems like a simple task, but I'm guessing it's not.
    I totally thought Linux was made for this stuff, but I guess I was wrong.
    I've seen some examples, but I can't make sense of them.

    Linux/BSD Gangster - fsck Off! - Simple ftp shell script

    That seems to be in my interest, but I can't put files into designated folders inside of the FTP's root.

    I can only put the file inside of the root directory, and not a subdirectory.
    That's annoying.
    I don't know what I'm doing wrong, though.

    Code:
    #!/bin/bash
    cd $HOME/foo/bar
    HOST='www.*.com'
    USER='********'
    PASSWD='*******'
    
    ftp -n -v $HOST << EOT
    ascii
    user $USER $PASSWD
    prompt
    mput /home/user/foo/bar/testing1.x ./
    #mput /home/user/foo/bar/testing1.x /linux/
    #mput /home/user/foo/bar/testing1.x /linux/
    #mput /home/user/foo/bar/testing1.x /linux/*
    # rm ./linux/
    #mkdir ./linux/
    #mput /home/user/foo/bar/testing1.x ./linux
    #mput /home/user/foo/bar/testing1.x ./linux/
    #mput /home/user/foo/bar/testing1.x ./linux/
    #mput /home/user/foo/bar/testing1.x ./linux/*
    bye
    bye
    EOT
    sleep 12
    Hmm, if I have the file in a folder with the same name, it tends to work..

    mput ./linux/testing1.x ./linux

    of course, I don't want things to work that way, because I'm picky

  2. #2
    Linux Engineer wje_lf's Avatar
    Join Date
    Sep 2007
    Location
    Mariposa
    Posts
    1,192
    This should put local file foo/bar/testing1.x into two directories on the remote system: foo/bar and foo/bar/linux. Remove the colored line if directory linux already exists on the remote machine.
    Code:
    #!/bin/bash
    cd $HOME/foo/bar
    HOST='www.*.com'
    USER='********'
    PASSWD='*******'
    
    ftp -n -v $HOST << EOT
    ascii
    user $USER $PASSWD
    prompt
    cd foo/bar
    put testing1.x
    mkdir linux
    cd linux
    put testing1.x
    bye
    EOT
    sleep 12
    Incidentally, ftp does not ignore command lines that begin with "#". It tells you that you have a command error, and continues with the next command line.

    Hope this helps.

  3. #3
    Linux User Agent-X's Avatar
    Join Date
    May 2005
    Location
    Dimension X
    Posts
    261
    Update:

    Well, I'll be. It worked.
    Perhaps it was the slash infront of the directories that made the problem?

    Ok, well that was nice.

    Now, why isn't this working?

    Code:
    #!/bin/bash
    cd $HOME/.mozilla/firefox/
    cd ./*.default/
    UDIRX=`pwd`
    HOST='www.*.com'
    USER='*************'
    PASSWD='*********'
    
    ftp -n -v $HOST << EOT
    ascii
    user $USER $PASSWD
    prompt
    mkdir .mozilla
    cd .mozilla
    mkdir firefox
    cd firefox
    mkdir $UDIRX
    cd $UDIRX
    put testing1.x $UDIRX
    bye
    EOT
    sleep 12
    I remember someone teaching me the `pwd` command a while ago. Print working directory, if I remember correctly. Supposedly, it becomes a recorded variable this way. Unfortunately, it doesn't seem to be working out too well.

  4. #4
    Linux Engineer wje_lf's Avatar
    Join Date
    Sep 2007
    Location
    Mariposa
    Posts
    1,192
    I guess by now you've fixed the post but forgot to remove the "oops".

    Now, why isn't this working?
    Whazzat?

    Please do this:
    1. Type this at the command line:
      Code:
      man script
    2. Use script to record a session in which you try to do what you just posted. It will fail, just as you expected.
    3. To get out of your running of script:
      Code:
      exit
    4. Paste the output from from script into a reply on this thread. Put it in CODE markers just as you did the bash script.
    5. In that same reply, tell us what you expected to see instead.
    6. Of course, if you used a bash script which differs even in the slightest way from what you posted in reply #3 (except, of course, the specification of HOST, USER, and PASSWD), be sure to post that modified script in the same reply.

    There are two reasons for you to specify exactly what's going on.
    1. The more specific your description of the problem, the more helpful the answers can be.
    2. It's simple courtesy. Those who help you in this and similar forums are volunteers. This way, you minimize their work.

  5. #5
    Linux User Agent-X's Avatar
    Join Date
    May 2005
    Location
    Dimension X
    Posts
    261
    The put command is what kept messing me up. I kept using it like move. That's a bad habit, and I've learned from it. Yet I don't know why I couldn't get the current directory to print and be made.

    Hmm, script is a nice program/executable.

    Code:
    #!/bin/bash
    cd $HOME/.mozilla/firefox/
    cd ./*.default/
    UDIRX=`pwd`
    HOST='www.linuxforums.org'
    USER='core-wizard'
    PASSWD='leethaxor'
    
    ftp -n -v $HOST << EOT
    ascii
    user $USER $PASSWD
    prompt
    mkdir .mozilla
    cd .mozilla
    mkdir firefox
    cd firefox
    mkdir $UDIRX
    cd $UDIRX
    put bookmarks.html
    bye
    EOT
    sleep 12
    Code:
    GNU nano 2.0.2               File: noob.x                 
    
    230 Restricted user logged in.
    Remote system type is UNIX.
    Using binary mode to transfer files.
    Interactive mode off.
    521 Directory already exists.
    250 "/.mozilla" is new cwd.
    521 Directory already exists.
    250 "/.mozilla/firefox" is new cwd.
    521 Directory already exists.
    250 "/home/ozar/.mozilla/firefox/6h2zlfeb.default" is new cwd.
    local: bookmarks.html remote: bookmarks.html
    200 PORT command successful.
    150 Opening BINARY mode data connection.
    226 Transfer completed.
    59318 bytes sent in 0.65 secs (89.4 kB/s)
    221 Goodbye.
    
    Script done on Wed 24 Oct 2007 04:41:58 PM UTC
    I expected it to create the directory 6hzlfeb.default inside of the FTP server. It didn't.

  6. #6
    Linux Engineer wje_lf's Avatar
    Join Date
    Sep 2007
    Location
    Mariposa
    Posts
    1,192
    This is puzzling.

    For a start, where did it put bookmarks.html?

  7. #7
    Linux User Agent-X's Avatar
    Join Date
    May 2005
    Location
    Dimension X
    Posts
    261
    I'm thinking it's either invisible, or I'm missing something.
    Maybe I'll go look through the web wysiwyg editor ftp-thingy, been a few years...
    Upload manager? Yeah, but anyway, I don't know where it's going... Area 51 for all I know.

    Subfolders of /.mozilla/firefox/:
    Select Name Actions # Files KB Stored
    There were no folders found.
    I can create the folder, however. I just created one with lftp via typing commands in manually.
    Odd. Perhaps it has to do with what I'm using?

  8. #8
    Linux Engineer wje_lf's Avatar
    Join Date
    Sep 2007
    Location
    Mariposa
    Posts
    1,192
    You'll notice that your final ftp mkdir command and your final ftp cd command specify a full pathname, starting with slash. I've never done that in ftp, and I'd recommend you do it only after careful reading of the ftp man page and some experimentation.

    You're doing that experimentation now, of course, so all that remains is reading of the man page.

    But if you just want to get this off the ground, avoid anything beyond simple directory names, including no slashes. To do so, try this. The changes are colored.
    Code:
    #!/bin/bash
    cd $HOME/.mozilla/firefox/
    cd ./*.default/
    UDIRX=`pwd`
    HOST='www.linuxforums.org'
    USER='core-wizard'
    PASSWD='leethaxor'
    BASENAME=$(basename $UDIRX)
    echo '$UDIRX is' $UDIRX
    echo '$BASENAME is' $BASENAME
    
    ftp -n -v $HOST << EOT
    ascii
    user $USER $PASSWD
    prompt
    mkdir .mozilla
    cd .mozilla
    mkdir firefox
    cd firefox
    mkdir $BASENAME
    cd $BASENAME
    put bookmarks.html
    bye
    EOT
    sleep 12
    Does that work?

  9. #9
    Linux User Agent-X's Avatar
    Join Date
    May 2005
    Location
    Dimension X
    Posts
    261
    Yes, it does.

    What exactly is BASENAME=$(basename $UDIRX)

    Is there a page with all these ... I wouldn't even know what they are called.
    They seem like integer aliases with redirecting points and some type of integration properties. I know I need to know more of them, though.

  10. #10
    Linux Engineer wje_lf's Avatar
    Join Date
    Sep 2007
    Location
    Mariposa
    Posts
    1,192
    What exactly is BASENAME=$(basename $UDIRX)
    That's why I added the two echo statements, so the output would show you clearly what basename does.

    Try this on the command line:
    Code:
    basename /a/b/c/d/e
    and you'll see what basename does.
    Is there a page with all these ... I wouldn't even know what they are called.
    They're called (among other things) "user commands". That's the name I found when I did
    Code:
    man 1 intro
    Try this at the command line:
    Code:
    echo $PATH
    and you'll get a list of all the directories which contain programs you can run (assuming that a given program has the right file permissions). Within all of those directories, you can find the list of executable files (given the proper permissions) by running this fun shell script I just wrote:
    Code:
    #!/bin/bash
    
    TRAIL=$(echo $PATH | sed -e 's/:/ /g')
    for STEP in $TRAIL
    do
      find $STEP -maxdepth 1 | xargs file | grep executable | sed -e 's/:.*$//'
    done
    They seem like integer aliases with redirecting points and some type of integration properties.
    I have no idea what an integer alias is. I have no idea what a redirecting point is. I have no idea what an integration property is.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...