Scripting an HTTP header request with netcat

后端 未结 5 1666
渐次进展
渐次进展 2020-12-08 11:04

I\'m trying to play around with netcat to learn more about how HTTP works. I\'d like to script some of it in bash or Perl, but I\'ve hit upon a stumbling block early on in m

相关标签:
5条回答
  • 2020-12-08 11:33

    This line will also work as equivalent:

    echo -e "HEAD / HTTP/1.1\nHost: 10.1.1.2\nConnection: close\n\n\n\n" | netcat 10.1.1.2 80
    
    0 讨论(0)
  • 2020-12-08 11:40

    Another way is to use what is called the 'heredoc' convention.

    $ nc -n -i 1 10.1.1.2 80 <<EOF
    > HEAD / HTTP/1.0
    >
    > EOF
    
    0 讨论(0)
  • 2020-12-08 11:56

    You need two lots of "\r\n", and also to tell netcat to wait for a response. printf "HEAD / HTTP/1.0\r\n\r\n" |nc -n -i 1 10.1.1.2 80 or similar should work.

    0 讨论(0)
  • 2020-12-08 11:58

    Another way to get nc to wait for the response is to add a sleep to the input. e.g.

    (printf 'GET / HTTP/1.0\r\n\r\n'; sleep 1) | nc HOST 80
    
    0 讨论(0)
  • 2020-12-08 11:59

    You can use below netcat command to make your instance webserver:

    MYIP=$(ifconfig eth0|grep 'inet addr'|awk -F: '{print $2}'| awk '{print $1}')
    while true; do echo -e "HTTP/1.0 200 OK\r\n\r\nWelcome to $MYIP" | sudo nc -l -p 80 ; done&
    
    0 讨论(0)
提交回复
热议问题