How to open sftp connection and execute sftp command in the same shell command

12,005

Approach 1:

echo "put /tmp/test.txt /tmp/" | sftp [email protected]

Approach 2:

Use the -b option:

-b batchfile

Batch mode reads a series of commands from an input batchfile instead of stdin. Since it lacks user interaction it should be used in conjunction with non-interactive authentication. A batchfile of -' may be used to indicate standard input. sftp will abort if any of the following commands fail: get, put, rename, ln, rm, mkdir, chdir, ls, lchdir, chmod, chown, chgrp, lpwd, df, symlink, and lmkdir. Termination on error can be suppressed on a command by command basis by prefixing the command with a-' character (for example, -rm /tmp/blah*).

Share:
12,005

Related videos on Youtube

ibedelovski
Author by

ibedelovski

DevOps Engineer

Updated on September 18, 2022

Comments

  • ibedelovski
    ibedelovski over 1 year

    Is there any possibility for opening sftp connection to remote server and execute sftp command in same line from shell.

    Something like this:

    sftp [email protected] && put /tmp/test.txt /tmp/
    

    where put is sftp command.

    My idea is to put file from local server to the remote server from bash script where only sftp is allowed on the remote server.

  • Falcon Momot
    Falcon Momot over 9 years
    The point here being that && is evaluated by the shell, and not merely a generic continuation. This gets the sftp command into sftp.
  • FooBee
    FooBee over 9 years
    @FalconMomot: Sorry, I really dont' get what you want to say.
  • Falcon Momot
    Falcon Momot over 9 years
    Just saying why && isn't the right thing to use.
  • ibedelovski
    ibedelovski over 9 years
    I typed && just like an example (I know that put is not a shell command), I think only @Sven understands me. Thank you @Sven.
  • Tarek Kalaji
    Tarek Kalaji almost 4 years
    You made my day, thank you so much :D