Sftp using batchfile - storing result local


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Sftp using batchfile - storing result local
# 1  
Old 09-18-2013
Sftp using batchfile - storing result local

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 batchfile.ftp @ACCOUNT@remoteserver

The batchfile.ftp contains the following :

Code:
lcd *local directory*
cd folder1
ls *.txt filename1.lst
cd ../folder2
ls *.txt filename2.lst
bye

running the command doesn't give any errors. The results are displayed in the terminalwindow. But the local files aren't build. I want to use these commands in a script and need the 2 files. Can anybody help? How can I create these files using 1 connection. (redirecting the output of the psft-command isn't a solution because I need 2 seperate files.)

Last edited by radoulov; 09-18-2013 at 09:06 AM..
# 2  
Old 09-18-2013
sftp does not work that way, it is not a shell and doesn't have shell features like file redirection.

Why not split the file afterward?
# 3  
Old 09-19-2013
I'm trying to solve my problem this way now but I'm stuck on the following :
How can I split a file in different files using variable linenumbers?
I have a variable "begin" that contains the linenumber where the split should start and a variable "end" that contains a linenumber where the split should end.
My code :
Code:
sed -n $begin,$start 'p' > file1.txt

doesn't work. I tried different variations of this with " ' / ....
Can you help me with this command? On the internet there are a lot of solutions to this problem but so far nothing works.

---------- Post updated at 11:18 ---------- Previous update was at 11:02 ----------

who searches, finds ... Smilie
Code:
sed -n "$begin,$end"p inputfile > outputfile

# 4  
Old 09-21-2013
Look at lftp as an alternative to sftp. It is scriptable, can be used for various protocols including ftp, sftp and http, and ls can be redirected! So:
Code:
sftp stfp://joe@remote.com
cd dir1
ls > file1.txt
cd ../dir2
rels > file2.txt

should do what you want. Note the use of rels as ls is cached (although it may not matter if you change directory - best to experiment).

lftp may not be there by default, but it will be in the repository if you are using a Linux distro.

Andrew
# 5  
Old 09-22-2013
Depending on your versions of sftp, you may need to adapt this:
Code:
psftp -v -batch -b batchfile.ftp @ACCOUNT@remoteserver | 
awk     '/^sftp> cd/    {fn=$NF; next}
         /^sftp>/       {P=0}
         P              {print > fn}
         /^sftp> ls/    {P=1}
        '

In any case, you need to split the cd ../folder2 into cd..; cd folder2 to make above work.

Last edited by RudiC; 09-22-2013 at 03:19 PM.. Reason: making script a bit mor readable
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Help on commands in sftp using BatchFile

Hi, The script didn't continue as "20130109" folder is already created on the destination server. Please help. Entry in script: cat > $filebatch << __EOF__ mkdir $current_date mkdir $current_date/$fpdomain cd $current_date/$fpdomain ls -l __EOF__ Error: sftp -b... (12 Replies)
Discussion started by: chococrunch6
12 Replies

2. Shell Programming and Scripting

Pattern matching & storing result in variable

Hi! i'm trying to parse textfiles against a pattern and storing the result in a variable. The strings i want to get are embraced by and can occur several times in one line, so e.g. some text anything else endwhat i have so far: #!/bin/bash for f in $* do exec 3<&0 exec 0<$f ... (2 Replies)
Discussion started by: thoni
2 Replies

3. Shell Programming and Scripting

sftp can't fine batchfile "No such file or directory"

Hi, I've got a C program that is using execlp to run a non-interactive sftp (using a batchfile) session to send some files to another system. Just before doing that, I create the batchfile called sftp_batch on the fly: V8_26_1:sun-->cat /workspace/sftp_batch cd /tmp/newsftp put test.file... (2 Replies)
Discussion started by: Fiaran
2 Replies

4. Shell Programming and Scripting

Passing variables: sftp using -b batchfile

Hi All, I have created a script for an sftp transfer that works without a date variable being passed, I want it to work with a date variable being passed. So, my initial script, mainsftp.sh, looks like this: ----------------------------------------------------------------------- #... (1 Reply)
Discussion started by: j_miller
1 Replies

5. Shell Programming and Scripting

sftp -b doesn't read the batchfile

Several of our end-users need to send a file to our insurance carrier using ssh and sftp. We've put together a Windows VBS script that opens the ssh tunnel and calls sftp with the -b option pointing to a batch script in the same directory, however sftp doesn't seems to be reading from the... (1 Reply)
Discussion started by: kmw
1 Replies

6. Shell Programming and Scripting

Issue while storing grep result

Hi All, Command: grep -i -n "rule" *.err *.log | grep -v "SP_RULE" The above command when run gives me two -three lines of output. Now I am storing the result of the grep command ie, ruleerrors=`grep -i -n "rule" *.err *.log | grep -v "SP_RULE"` Now when I echo the value of ruleerrors, I... (6 Replies)
Discussion started by: Sreejith_VK
6 Replies

7. Shell Programming and Scripting

storing result of a command in variable

For whatever reason I cant seem to fix my syntax to do the following. I want to run a grep and count how many instances come up and store that number in a variable but I keep erroring out. Here's my code in bash: number=grep blah file.txt | wc -l (1 Reply)
Discussion started by: eltinator
1 Replies

8. Shell Programming and Scripting

Problem storing SSH result in a variable

i have this SSH command which runs perfectly on command prompt in sunOS ssh -o Port=${portno} ${uname}@${server} find ${dir_path} -name '***' output : /usr/local/home/*** My problem is when i run same command in my script #!/usr/bin/ksh res=`ssh -o Port=${portno} ${uname}@${server}... (1 Reply)
Discussion started by: prash184u
1 Replies

9. Shell Programming and Scripting

sFTP result

Hi all, batchfile ====== lcd /home/data/ cd /tmp/source/ get "*.temp.ack" quit Result from sftp ============ 1111.temp.ack| 0B | 0B/s | TOC: 00:00:01 | 100%^M ^M2222.temp.ack | 0B | 0B/s | TOC: 00:00:01 | 100%^M ^M3333.temp.ack | 0B | 0B/s | TOC: 00:00:01 | 100%^M ^M As I don't know... (4 Replies)
Discussion started by: mpang_
4 Replies

10. Shell Programming and Scripting

Automate batchfile generation for sFTP

Hey all, I am writing a script for sFTP using batchfile option (-B). My script is supposed to monitor a certain directory for several files and when they are found I will send them to a testing server. So using a static batchfile wouldn't be feasible, I am just wondering if anyone can show... (0 Replies)
Discussion started by: mpang_
0 Replies
Login or Register to Ask a Question