Netcat ( nc -l ) as webserver

Login or Register to Reply

Thread Tools Search this Thread
# 1  
Old 12-14-2016
Lightbulb Netcat ( nc -l ) as webserver

Dear Linux guru's

I am trying to create a webserver using nc (netcat only) on RHEL 7.2 running on bash shell.

now the easy thing is to get nc listing to a port and respond back

$ while true; do { echo -e 'HTTP/1.0 200 OK\r\n'; set; } | nc -l 7877; done

This when called from a browser: http://My-server:7877 I get my env variables. Works perfect !! Love it Smilie

Now when I try http://My-server:7877?asd=1&something=2&other=hello

I was hopping to get QUERY_STRING env variable with all the values sent via browser GET method ! . However the env variable is not set. There is not variable set with these values.


GNU bash, version 4.2.46(1)-release (x86_64-redhat-linux-gnu)

Ncat: Version 6.40 ( Ncat - Netcat for the 21st Century )

Red Hat Enterprise Linux Server release 7.2

Dont want to use anything other than nc. Cannot install any tool or webserver or binaries. Please dont suggest that

And thanks all for reading my question Smilie.

Last edited by rbatte1; 12-15-2016 at 06:09 AM.. Reason: Changed ICODE tags to CODE tags
# 3  
Old 12-15-2016
I made a named pipe and seems very close to getting my solution of garbing HTTP headers, Also noticed my browser does not Always send all the header each time.
(Chrome Version 54.0.2840.99 even in incognito )

My workaround (not solution) have a named pipe to communicated with nc.

$ mkfifo  webout

and then have the while loop to feed netcat(nc)

while true; do { echo -e 'HTTP/1.0 200 OK\r\n'; cat webout;  } | nc -vl 8080 >webout; done

I am just playing around with named pipe, looking to find something in the lines of:

nc -l PORT <webin > 2&1>webout

and then loop out with tail kind of thing on webin and webout. Any experts on FIFO's please suggest.

Last edited by chakrapani; 12-15-2016 at 10:50 AM.. Reason: typo
# 4  
Old 12-15-2016
This is going to be a royal pain to do in the shell. You're going to get stuck waiting on input from the web service while the web browser is stuck waiting on input for you. Named pipes are prone to this, even used one-way -- using a pair means almost guaranteed deadlock.

If you have Python or Perl, I'd look into writing it in that language instead. You would use a socket directly, avoiding the need for these deadlocks.

Another alternative, which I know you didn't want, would be the expect language. It can work pretty much the way you were expecting - run a program, check if it's got input, check if it's got output, decide what do do with it, move on.
Login or Register to Reply

Thread Tools Search this Thread
Search this Thread:
Advanced Search

More UNIX and Linux Forum Topics You Might Find Helpful
Telnet vs netcat behavior ckmehta UNIX for Advanced & Expert Users 4 07-25-2014 01:52 PM
Connect to Windows from Linux using NetCat Jotne Shell Programming and Scripting 1 11-18-2012 12:46 PM
Help with Netcat MrCrumbs IP Networking 0 09-12-2012 09:30 AM
nc (netcat): Connection refused problem dinga IP Networking 2 03-14-2012 10:37 PM
Help with sed to add delimiter to send HEX with netcat patx Shell Programming and Scripting 3 12-30-2011 10:16 AM
Post using nc(netcat) ajaypadvi Shell Programming and Scripting 3 07-27-2011 08:04 AM
Thank you radoulov for your help on netcat command (nc -lp) vectrum Linux 0 07-02-2011 07:00 AM
Please help me to install netcat on solaris praset Solaris 2 01-08-2011 03:05 PM
netcat prints blank pages dunpealslyr UNIX and Linux Applications 3 06-10-2010 11:17 AM
How does netcat manage the data it transfers ? qiulang UNIX for Advanced & Expert Users 2 08-31-2009 10:25 PM
problem feeding netcat a list of hosts seanovision Shell Programming and Scripting 3 08-25-2008 07:47 PM
netcat MartyIX Shell Programming and Scripting 6 08-22-2008 07:29 AM
Netcat with Authentication? pileofrogs UNIX for Advanced & Expert Users 1 12-04-2006 09:48 AM
Cannot connect to webserver negixx SuSE 5 07-13-2005 12:59 PM
netcat like file transfer linuxdba Programming 2 04-28-2005 06:10 AM