How to Pipe The Output of an Ls on Remote Server to The Local Filesystem via Sftp

How do I pipe the output of an LS on remote server to the local filesystem via SFTP?

If you're willing to wait the 20 minutes for the data to scroll across your screen you can capture all the output using "script".

Call 'script' before you start your ssh or sftp session and it will capture all terminal output to your local disk. Type 'exit' to finish the capture.

NAME
script -- make typescript of terminal session

SYNOPSIS
script [-akq] [-t time] [file [command ...]]

DESCRIPTION
The script utility makes a typescript of everything printed on your ter-
minal. It is useful for students who need a hardcopy record of an inter-
active session as proof of an assignment, as the typescript file can be
printed out later with lpr(1).

If the argument file is given, script saves all dialogue in file. If no
file name is given, the typescript is saved in the file typescript.

If the argument command is given, script will run the specified command
with an optional argument vector instead of an interactive shell.

The following options are available:

-a Append the output to file or typescript, retaining the prior con-
tents.

-k Log keys sent to program as well as output.

-q Run in quiet mode, omit the start and stop status messages.

-t time
Specify time interval between flushing script output file. A
value of 0 causes script to flush for every character I/O event.
The default interval is 30 seconds.

The script ends when the forked shell (or command) exits (a control-D to
exit the Bourne shell (sh(1)), and exit, logout or control-D (if
ignoreeof is not set) for the C-shell, csh(1)).

Certain interactive commands, such as vi(1), create garbage in the type-
script file. The script utility works best with commands that do not
manipulate the screen. The results are meant to emulate a hardcopy ter-
minal, not an addressable one.

ENVIRONMENT
The following environment variable is utilized by script:

SHELL If the variable SHELL exists, the shell forked by script will be
that shell. If SHELL is not set, the Bourne shell is assumed.
(Most shells set this variable automatically).

SEE ALSO
csh(1) (for the history mechanism).

HISTORY
The script command appeared in 3.0BSD.

BUGS
The script utility places everything in the log file, including linefeeds
and backspaces. This is not what the naive user expects.

It is not possible to specify a command without also naming the script
file because of argument parsing compatibility issues.

When running in -k mode, echo cancelling is far from ideal. The slave
terminal mode is checked for ECHO mode to check when to avoid manual echo
logging. This does not work when in a raw mode where the program being
run is doing manual echo.

Save the result of ls command in the remote SFTP server on local machine

Here's how I would do it, with a more "traditional" approach that involves opening a file for writing the desired output (I like @kastelian 's log_file idea, though):

#!/usr/bin/expect

spawn sftp myuser@myftp.mydomain.com
expect "password:"
send "mypassword\n";
expect "sftp>"

set file [open /tmp/ls-output w] ;# open for writing and set file identifier

expect ".*" ;# match anything in buffer so as to clear it

send "ls\r"

expect {
"sftp>" {
puts $file $expect_out(buffer) ;# save to file buffer contents since last match (clear) until this match (sftp> prompt)
}
timeout { ;# somewhat elegant way to die if something goes wrong
puts $file "Error: expect block timed out"
}
}

close $file

interact

The resulting file will hold the same two extra lines as in the log_file proposed solution: the ls command at the top, and the sftp> prompt at the bottom, but you should be able to deal with those as you like.

I have tested this and it works.

Let me know if it helped!

Access local files via a web server on another comupter

Your idea of using ssh2_sftp would work (or any type of copy command, for that matter - even just scp), to copy the files from the remote recordings server to the lamp server. Just copy the files to a web-accessible directory on the LAMP server, then your web pages can link to these files via their respective URL's. The whole thing (including the copy) can be done on the fly in PHP.

File returns NULL Value while using sftp_open() for copying file from local to remote using SFTP libssh in C++

You were using sftp_open(); only for creating the file by using access_type O_CREAT,you need O_WRONLY writing some data into that file.

 fr = fopen("C:/Users/Sami/Desktop/we/s.txt", "r");
fseek(fr, 0, SEEK_END);
lSize = ftell(fr);
rewind(fr);
char * buffer = (char*)malloc(sizeof(char)*lSize);
result = fread(buffer, 1, lSize, fr);
int access_type = O_WRONLY | O_CREAT;
file = sftp_open(sftp, "/home/serversj/Desktop/sami/s.txt", access_type,S_IRWXU);
nwritten = sftp_write(file, buffer, result);

I hope this will work..

Can we move a file from an FTP server to a local directory using apache camel endpoints?

No, &move option is intended for moving file in scope of remote filesystem.

Use this instead:

from("sftp://user@ftp.com/outgoingFolder?password=*****&throwExceptionOnConnectFailed=true&maximumReconnectAttempts=0&stepwise=false&fastExistsCheck=true&disconnect=true&consumer.delay=300000&passiveMode=true")
.to("file:C:\\folder");

SFTP: return number of files in remote directory?

echo ls -l | sftp server | grep -v '^sftp' | wc -l

If you want to count the files in a directory the directory path should be put after the ls -l command like

echo ls -l /my/directory/ | sftp server | grep -v '^sftp' | wc -l

How do I list one filename per output line in Linux?

Use the -1 option (note this is a "one" digit, not a lowercase letter "L"), like this:

ls -1a

First, though, make sure your ls supports -1. GNU coreutils (installed on standard Linux systems) and Solaris do; but if in doubt, use man ls or ls --help or check the documentation. E.g.:

$ man ls
...
-1 list one file per line. Avoid '\n' with -q or -b

Working on PHP projects on a remote dev server via sFTP

May not be a good idea:

Warnings:

1) Disconnect:
What if you are coding and your connection gets lost, you may get a corrupted file or loose some work. Disconnects occur much more often that power loss in your home/office, and you can safeguard by using a small UPC - that will give a minute to save your work.

2) SCM:
Use git, mercurial, svn or what have you, to speed up deployment. Increases ability to share code, backup and roll backs.

3) Auto completion will not work very well over network connection, because ( at least in NetBeans) it scans your project to figure out what you want to auto-complete. It takes a few seconds even on a local machine.

Solution:
If after all of the above you still want to do it, you can trick your editor by mounting remote storage as a local drive. You didn't specify your OS but on Mac and Linux - you can easily do it - take a look at Fuse. http://fuse.sourceforge.net/

Khmm apperatnly there was an attempt to port Fuse to Windows:
http://fuse4win.4host.ru/

Hope that helps

Update

There are also a few commercial products - one was recommended by macworld I think ( they are both for Mac and Windows)

http://en.wikipedia.org/wiki/WebDrive

http://en.wikipedia.org/wiki/ExpanDrive



Related Topics



Leave a reply



Submit