Find the answer to your Linux question:
Results 1 to 10 of 10
Need the shell script which when executed should connect to the server having option ftp/sftp. script should able to connect the remote ftp / sftp server and transfer files. Please ...
  1. #1
    Just Joined!
    Join Date
    Mar 2008
    Posts
    4

    Shell script for automating ftp or secure ftp login

    Need the shell script which when executed should connect to the server having option ftp/sftp. script should able to connect the remote ftp / sftp server and transfer files.

    Please some one help me.


    Thanks,
    Shiva

  2. #2
    Linux User
    Join Date
    Jun 2007
    Posts
    318
    Look at the manpages for ftp & sftp for details. As an example of ftp within a script:

    Code:
    #!/bin/bash -vx
    
    ftp -u <host> <<++EOT++
    user <user> <password>
    bin
    put <file>
    quit
    ++EOT++

  3. #3
    Just Joined!
    Join Date
    Mar 2008
    Posts
    4
    when used the code

    #!/bin/bash -vx

    HOSTNAME='hostname'
    USERNAME='username'
    PASWORD='password'

    sftp $HOSTNAME@$USERNAME <<EOF
    put <file>
    quit
    EOF

    it asks the password to be given interactively, doesn't want the password to be given interactively when scripts executes it should connect and put the file into the server.

  4. #4
    Linux User
    Join Date
    Jun 2007
    Posts
    318
    You need to read the manpages. For sftp you can't specify the password in batch mode. The most common method is to use private/public keys. You generate those with ssh-keygen and then copy the public key into ~/.ssh/authorized_keys on the destination host. Then the sftp would use the batch option and would look something like this:

    Code:
    sftp -b /dev/stdin <<++EOT++ -oIdentityFile=~/.ssh/id_rsa <user>@<host>
    put a.a
    ++EOT++
    In this case I used key type RSA thus the file id_rsa. Refer to the ssh-keygen manpage for details.

  5. #5
    Just Joined!
    Join Date
    Mar 2008
    Posts
    4
    I agree we cannot hard cord the password for SFTP connection in the script.
    The rsa key has been generated and it is in the .ssh folder of my home direcotry.

    sftp -b /dev/stdin <<++EOT++ -o IdentityFile=~/.ssh/id_rsa <username>@<hostname>

    using this when script excutes asking for password to enter after entering password the file will get transferred.

    Is there any thing i am missing out here to specify?

  6. #6
    Linux User
    Join Date
    Jun 2007
    Posts
    318
    Sounds like you didn't copy ~/.ssh/id_rsa.pub to the destination server and append it to ~/.ssh/authorize_keys.

    You can also specify -vvv before the -b to put it in verify mode to get information on why it's failing.

  7. #7
    Just Joined!
    Join Date
    Mar 2008
    Posts
    4
    I am using the same server to get connect with the same username where script is running.

    When debugged what i found is that

    debug:authentication that can continueublickey,password,keyboard-interactive
    debug:next authentication trying is publiuc key
    debug: try publickey : /home/shiva/.ssh/id_rsa.pub
    debug:authentication that can continueublickey,password,keyboard-interactive
    debug:next authentication trying is keyboard-interactive
    debug:authentication that can continueublickey,password,keyboard-interactive
    debug:next authentication trying is password
    password:

    Now it asks for password. But don't know why its asking for password how can i avoid this could you please guide me?

  8. #8
    Linux User
    Join Date
    Jun 2007
    Posts
    318
    Typo, the file should be ~/.ssh/authorized_keys (the 'd' was missing).

  9. #9
    Just Joined!
    Join Date
    May 2008
    Posts
    1
    Hello vsemaska, thanks. It worked very fine.

    So, please tell me, what's the function of <<OEF like you typed in?

    I didn't understand.

    Thanks a lot.

  10. #10
    Linux User
    Join Date
    Jun 2007
    Posts
    318
    From the manpage for bash, Here Documents section:

    This type of redirection instructs the shell to read input from the
    current source until a line containing only word (with no trailing
    blanks) is seen. All of the lines read up to that point are then used
    as the standard input for a command.

    The format of here-documents is:

    <<[-]word
    here-document
    delimiter

    So the <<EOF specified to treat the lines below it as input to sftp until it reached the line with only EOF on it.

Posting Permissions

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