Store output of command in sftp to variable and list

Store output of command in sftp to variable and list

sftp commands
sftp directory listing to local file
sftp command with password example
sftp multiple files using shell script
sftp> ls command
sftp ls-options
shell script to download files from sftp server
sftp -b

My aim is to create a shell script such that it logins and filter the list of files available and select a file to get. Here I need to run commands like in bash. My sample code is:

sshpass -p password sftp user@10.10.10.10 <<EOF
cd /home/
var=$(ls -rt)
echo $var
echo "select a folder"
read folder
cd $folder
filen=&(ls -rt)
echo $filen
echo "select a file"
read name
get $name
bye
EOF

After some research and experimentation, found a way to create batch/interactive sessions with sftp. Posting as separate answer, as I still believe the easier way to go is with lftp (see other answer). Might be used on system without lftp

The initial exec create FD#3 - pointing to the original stdout - probably user terminal. Anything send to stdout will be executed by the sftp in the pipeline.

The pipe is required to allow both process to run concurrently. Using here doc will result in sequential execution. The sleep statement are required to allow SFTP to complete data retrieval from remote host.

exec 3>&1
(
   echo "cd /home/"
   echo "ls"
   sleep 3    # Allow time for sftp 
   echo "select a folder" >&3
   read folder
   echo "cd $folder"
   echo "ls"
   sleep 3     # Allow time for sftp
   echo "select a file" >&3
   read name
   echo "get $name"
   echo "bye"
) | sshpass -p password sftp user@10.10.10.10

Save a list of files over SFTP to a text file, The sftp command is very limited. If you can't make it do what you want, you can use another approach, which is to mount the remote directory with the SSHFS� >I want to take output of modtime command to a variable so that I can refer that variable in the script. You can get the output of ls into a file: ls [remote-directory] [local-file] modtime doesn't seem to be able to redirect the output. With your above message1=$(ftp ) you can capture that output and the parse it.


The above approach will not work. Remember that the 'here document' (<<EOF ... EOF) is evaluate as input to the sftp session. Prompts will be displayed, and user input will be requested BEFORE any output (ls in this case) will be available from sftp.

Consider using lftp, which has more flexible construct. In particular, it will let you use variables, create command dynamically, etc.

lftp sftp://user@host <<EOF

cd /home
ls
echo "Select Folder"
shell 'read folder ; echo "cd $folder" >> temp-cmd'
source temp-cmd

ls
echo "Select Folder"
shell 'read file ; echo "get $file" >> temp-cmd'
source temp-cmd

EOF

In theory, you can create similar constructs with pipes and sftp (may be a co-process ?), but this is much harder.

Of course, the other alternative is to create different sftp sessions for listing, but this will be expensive/inefficient.

Redirect output from sFTP - UNIX and Linux Forums, Hey all, I have a ksh script which used ftp to transfter certain files to a AIX server, put $REPORT file or similar file under .ssh directory in your home directory. in a variable ($name)? i thing how put the result of the command (grep , cut,� 2. SFTP Command Line Example #2 – List all files in the current directory; 3. Linux SFTP Command Example #3 – Navigate through directories; 4. Linux SFTP Command Line Example #4 – Upload files and directories using the put command; 5. SFTP Command Example #5 – Download files and directories using the get command; 6.


I would suggest you to create a file with pattern of the files you want downloaded and then you can get files downloaded in one single line:

sftp_connection_string <<< $"ls -lrt"|grep -v '^sftp'|grep -f pattern_file|awk '{print $9}'|sed -e 's/^/get -P /g'|sftp_connection_string

if there are multiple definite folders to be looked into, then:

**Script version**
for fldr in folder1 folder2 folder3;do
    sftp_connection_string <<< $"ls -lrt ${fldr}/"|grep -v '^sftp'|grep -f pattern_file|awk '{print $9}'|sed -e "s/^/get -P ${fldr}/g"|sftp_connection_string
done

One-liner

for fldr in folder1 folder2 folder3;do sftp_connection_string <<< $"ls -lrt ${fldr}/"|grep -v '^sftp'|grep -f pattern_file|awk '{print $9}'|sed -e "s/^/get -P ${fldr}\//g"|sftp_connection_string;done

let me know if it works.

SFTP commands and options, Let's access a Storage Group Note: This list of commands and options are specific to the OpenSSH SFTP client and can vary -b <batch file>, Include this command and set the “<batch file>” variable to the command file to use in batch mode -1 - List the output in a single column; -a - Include files that start with a dot (“. EOF) is evaluate as input to the sftp session. Prompts will be displayed, and user input will be requested BEFORE any output (ls in this case) will be available from sftp. Consider using lftp, which has more flexible construct. In particular, it will let you use variables, create command dynamically, etc.


How To Assign Output of a Linux Command to a Variable, To store the output of a command in a variable, you can use the shell of files in the current working directory in a variable called FILES and� `pwd` command shows the path of the current working directory. The following script stores the output of `pwd` command into the variable, $current_dir and the value of this variable is printed by using `echo` command. $ current_dir = `pwd` $ echo "The current directory is : $current_dir"


Bash Assign Output of Shell Command To Variable, Let us see some common examples for Linux, macOS, *BSD and Unix-like systems running bash. BASH command output to the variable. To store� I'm making an sftp-connection to a remote server. I want the result of an ls-command in a local file and the result of ls on another folder in another local file. Because everything has to go as fast a possible I wan't to do everyting in one connection. The command I use is : psftp -v -batch -b (4 Replies)


How to redirect the output of a FTP recursive listing to a local file with , as far as i know once you are in the interactive ftp shell the output can't be captured. but you can either pipe commands to the ftp command or specify a file with� sftp gl.umbc.edu (The CSIT1 computer now requests username and password information.) cd project/data (On the remote machine, move to the project/data subdirectory.) ls (Get a list of the files in the current remote directory.) get oldstuff.txt (Copy the file oldstuff.txt from the remote directory.) get folder/olderstuff.txt (Also copy the file olderstuff.txt, which is in a subdirectory on the