Home Man
Search
Today's Posts
Register

Please post your urgent questions here for highest visibility. Posting a new thread to this forum requires Bits. We monitor this forum to help people with emergencies, but we do not guarantee response time or answers. This forum is "best effort" only. Members who reply to posts here receive a bonus of 1000 Bits per reply.

Netcat ( nc -l ) as webserver

Login 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

Code:
$ 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

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.


Versions:

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

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

redhat
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 .

Last edited by rbatte1; 12-15-2016 at 06:09 AM.. Reason: Changed ICODE tags to CODE tags
# 2  
Old 12-14-2016
Sorry, that - and the entire rest of the common gateway interface - is something a web server does for you.
# 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.

Code:
$ mkfifo  webout

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

Code:
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:

Code:
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 to Reply

« Previous Thread | Next Thread »
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Telnet vs netcat behavior ckmehta UNIX for Advanced & Expert Users 4 07-25-2014 01:52 PM
Help with Netcat MrCrumbs IP Networking 0 09-12-2012 09:30 AM
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
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
netcat like file transfer linuxdba Programming 2 04-28-2005 06:10 AM


All times are GMT -4. The time now is 12:21 AM.

Unix & Linux Forums Content Copyrightę1993-2018. All Rights Reserved.
UNIX.COM Login
Username:
Password:  
Show Password