Script to get file from SFTP and run it

7,563

Best and easiest is to use scp instead of sftp.

If scp is really not an option, you can read sftp commands using a 'here document', but it is very hard to script around errors.

sftp username@server << %EOF%
ls
bye
%EOF%

%EOF% can be any word you want, as long as it starts and ends with a % and the last %EOF% must be at the start of a line.

For passwordless login, read up on 'ssh key authentication'.

Using sftp from within an ssh session sounds like overkill, sounds like doing double things. Why not initiate the copy from the local machine?

Share:
7,563

Related videos on Youtube

Novice User
Author by

Novice User

Updated on September 18, 2022

Comments

  • Novice User
    Novice User over 1 year

    We have 5 servers running our code: A, B , C, D and E.

    From a local machine, we sftp the file to A. Then

    ssh id@A
    su - applicationname
    sftp id@A
    get <file>
    bye
    tar -xzf <file>
    logout
    logout
    

    The same procedure we apply for others B to E.

    Is there any way we can simplify this using script?

    We have requested linux team to provide passwordless authentication. But we don't know how to do sftp inside a SSH session using a script.

    • jordanm
      jordanm over 11 years
      Use scp instead.
  • Jakob Bennemann
    Jakob Bennemann almost 8 years
    Welcome to U&L! Please take note of the formatting changes I made (they were rather significant). Quote blocks are not well-suited for code, and bold (if it should even be used at all) should not be used for inlined commands.