Getting directory listing over http

2019-02-02 12:39发布

问题:

There is a directory that is being served over the net which I'm interested in monitoring. Its contents are various versions of software that I'm using and I'd like to write a script that I could run which checks what's there, and downloads anything that is newer that what I've already got.

Is there a way, say with wget or something, to get a a directory listing. I've tried using wget on the directory, which gives me html. To avoid having to parse the html document, is there a way of retrieving a simple listing like ls would give?

回答1:

I just figured out a way to do it:

$ wget --spider -r --no-parent http://some.served.dir.ca/

It's quite verbose, so you need to pipe through grep a couple of times depending on what you're after, but the information is all there. It looks like it prints to stderr, so append 2>&1 to let grep at it. I grepped for "\.tar\.gz" to find all of the tarballs the site had to offer.

Note that wget writes temporary files in the working directory, and doesn't clean up its temporary directories. If this is a problem, you can change to a temporary directory:

$ (cd /tmp && wget --spider -r --no-parent http://some.served.dir.ca/)


回答2:

The following is not recursive, but it worked for me:

$ curl -s https://www.kernel.org/pub/software/scm/git/

The output is HTML and is written to stdout. Unlike with wget, there is nothing written to disk.

-s (--silent) is relevant when piping the output, especially within a script that must not be noisy.

Whenever possible, remember not to use ftp or http instead of https.



回答3:

If it's being served by http then there's no way to get a simple directory listing. The listing you see when you browse there, which is the one wget is retrieving, is generated by the web server as an HTML page. All you can do is parse that page and extract the information.



回答4:

What you are asking for best served using FTP, not HTTP.

HTTP has no concept of directory listings, FTP does.

Most HTTP servers do not allow access to directory listings, and those that do are doing so as a feature of the server, not the HTTP protocol. For those HTTP servers, they are deciding to generate and send an HTML page for human consumption, not machine consumption. You have no control over that, and would have no choice but to parse the HTML.

FTP is designed for machine consumption, more so with the introduction of the MLST and MLSD commands that replace the ambiguous LIST command.



回答5:

AFAIK, there is no way to get a directory listing like that for security purposes. It is rather lucky that your target directory has the HTML listing because it does allow you to parse it and discover new downloads.