Results 1 to 5 of 5

Thread: Automate sftp using unix script

  1. #1
    Senior Member
    Join Date
    Apr 2011
    Posts
    113
    Thanks
    18
    Thanked 0 Times in 0 Posts
    Rep Power
    4

    Default Automate sftp using unix script

    Hi All,


    I need to write a UNIX script that automates the sftp process. I should be able to do a sftp to a secure box and get a file from there. I am having a problem doing this because no matter what I do, when I run my script, I get a prompt at command line asking for a password. How could I automate this? Could someone give me a sample script?plzzz


    Thanks in advance.
    Priyanka

  2. #2
    Junior Member
    Join Date
    Jun 2011
    Posts
    8
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Rep Power
    0

    Default

    Code:
    #!/usr/local/bin/expect
    spawn  sftp  -b cmdFile user@yourserver
    expect  "password:"
    send "shhh!\n";
    interact
    This will spawn sftp in batch mode and pass in the password shhh! to the program. SFTP will than execute all the commads in cmdFile


    cmdFile
    Code:
    lcd /home/ftp/test
    cd  /home/ftp/somedir
    mput *.dat
    lcd /home/recieving
    cd /home/someotherdir
    mget *.dat
    Another alternative would be to use Perl to automate the SCP. You could do this with the following example

    Code:
    #!/usr/bin/perl -w
    use Net::SFTP;
    use strict;
    my $host = "host.ssh.com";
    my %args = (
        user =>  'your_user_name,
        password =>  'your_password',
        debug => 'true'
    );
    my $sftp = Net::SFTP->new($host,  %args);
    $sftp->get("/home/user/something.txt",  "/home/user/hey.txt");
    $sftp->put("bar", "baz");
    Last edited by master-of-puppets; 3rd June 2011 at 12:01 AM.

  3. #3
    Junior Member
    Join Date
    May 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Rep Power
    0

    Default

    Hi Master

    Thanks for the scripts.
    I will try them tomorrow and let you know if I've any issues. I'm going to test my scripts in linux.

    Thanks
    Ripi

  4. #4
    Senior Member
    Join Date
    Aug 2011
    Posts
    454
    Thanks
    0
    Thanked 62 Times in 58 Posts
    Rep Power
    10

    Default

    expect is not secure. that's silly when using secure_ftp.
    generate ssh public keys, and share them with the server!
    the -b option seems neat, if you don't want to deal with a command file, you could use here-strings
    Code:
    sftp user@server <<EOF
    lcd /home/ftp/test
    cd  /home/ftp/somedir
    mput *.dat
    lcd /home/recieving
    cd /home/someotherdir
    mget *.dat
    EOF
    Last edited by Watael; 11th May 2012 at 11:15 AM.

  5. The Following 2 Users Say Thank You to Watael For This Useful Post:

    nixcraft (11th May 2012), soonhong (20th July 2012)

  6. #5
    Never say die nixcraft's Avatar
    Join Date
    Jan 2005
    Location
    BIOS
    Posts
    4,515
    Thanks
    17
    Thanked 814 Times in 511 Posts
    Rep Power
    10
    All [Solved] threads are closed by mods / admin to avoid spam issues. See Howto mark a thread as [Solved]


Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Urjent need reply-how to do automate sftp?
    By aish in forum Shell scripting
    Replies: 1
    Last Post: 14th July 2011, 12:33 AM
  2. Howto automate ssh scp sftp Password Connection
    By chinalinix in forum Networking, Firewalls and Security
    Replies: 1
    Last Post: 15th August 2009, 12:16 AM
  3. Script for backup and automate it
    By skumar704 in forum Shell scripting
    Replies: 2
    Last Post: 11th May 2009, 10:53 PM
  4. script to automate download and rename
    By wademac in forum Shell scripting
    Replies: 0
    Last Post: 14th April 2008, 11:51 PM
  5. Linux UNIX SFTP in a Shell Script
    By Nishanthhampali in forum Shell scripting
    Replies: 1
    Last Post: 30th January 2008, 01:16 PM

Tags for this Thread

Posting Permissions

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