How can I get the contents of a file using telnet?

I found this , at the end there is the task to download a picture using telnet, but I can't do that. I ask the help of more experienced and intelligent people.

  • the content you get, just get the http server headers, they must either be cut off or not sent to the output. The first can be done postprocessing, for example on perl, the second is done using expect for example. But why do you need such a perversion? - zb '
  • Yes, I'll do everything later, the main thing is to get it, but HOW ?! - andrei48
  • one
    somewhere in the middle of the picture. why do you need telnet to get it? echo -e "GET /morda-logo/i/ya-logo.png HTTP / 1.1 \ nHost: yandex.st \ n" | while read a; do echo $ a; sleep 1; done | telnet yandex.st 80 than Wget and curl did not suit you? - zb '
  • @eicto as I understand this bash script? Could not throw articles / books on it, only recently installed linux and have not moved in all things ... and transfer your comment to the answers! - andrei48
  • bash-scripting.ru/abs/chunks in the answers will not transfer. because it does not need to do telnet. - zb '

1 answer 1

to make an http request, it is usually enough to send a couple of headers like:

GET / HTTP/1.1 Host: ya.ru 

and two blank lines after them, only after that a small delay is necessary, sufficient to receive a response. for example, as follows (the command output is also shown):

 $ { echo -e 'GET / HTTP/1.1\nHost: ya.ru\n\n'; sleep 0.5; } | telnet ya.ru 80 Trying 93.158.134.3... Connected to ya.ru. Escape character is '^]'. HTTP/1.1 302 Found Server: nginx Date: Tue, 26 Apr 2016 16:01:28 GMT Content-Length: 0 Connection: keep-alive Cache-Control: no-cache,no-store,max-age=0,must-revalidate Location: https://ya.ru/ Expires: Tue, 26 Apr 2016 16:01:28 GMT Last-Modified: Tue, 26 Apr 2016 16:01:28 GMT P3P: policyref="/w3c/p3p.xml", CP="NON DSP ADM DEV PSD IVDo OUR IND STP PHY PRE NAV UNI" Set-Cookie: yandexuid=9932749951461686488; Expires=Fri, 24-Apr-2026 16:01:28 GMT; Domain=.ya.ru; Path=/ Connection closed by foreign host. 

there is just requested an index page (the address is indicated after GET - just / ) of the site http://ya.ru . in this case, we received a redirect answer to https://ya.ru .

and this is how you can get the picture at https://yastatic.net/morda-logo/i/apple-touch-icon/ru-76x76.png :

 $ { echo -e 'GET /morda-logo/i/apple-touch-icon/ru-76x76.png HTTP/1.1\nHost: yastatic.net\n\n'; sleep 0.5; } | telnet yastatic.net 80 Trying 178.154.131.215... Connected to yastatic.net. Escape character is '^]'. HTTP/1.1 200 OK Server: nginx/1.8.1 Date: Tue, 26 Apr 2016 16:05:06 GMT Content-Type: image/png Content-Length: 734 Connection: keep-alive Last-Modified: Mon, 25 Apr 2016 13:08:05 GMT ETag: "571e16b5-2de" Expires: Thu, 31 Dec 2037 23:55:55 GMT Cache-Control: max-age=315360000 Cache-Control: public Access-Control-Allow-Origin: * Timing-Allow-Origin: * Accept-Ranges: bytes здесь идут бинарные данные, которые я не копировал Connection closed by foreign host. 

This output can be redirected to a file:

 $ { echo -e 'GET /morda-logo/i/apple-touch-icon/ru-76x76.png HTTP/1.1\nHost: yastatic.net\n\n'; sleep 0.5; } | telnet yastatic.net 80 > some.file Connection closed by foreign host. 

however, in this case, the server will also get the server response - all these http-headers transmitted by the server.

you can remove them, for example, by “skipping” through the sed program, which deletes all lines, starting with the very first and up to the first empty (the list of headers ends with an empty line):

 $ { echo -e 'GET /morda-logo/i/apple-touch-icon/ru-76x76.png HTTP/1.1\nHost: yastatic.net\n\n'; sleep 0.5; } | telnet yastatic.net 80 | sed '1,/^$/d' > some.file Connection closed by foreign host.