Unix/Linux Go Back    


Emergency UNIX and Linux Support 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

Emergency UNIX and Linux Support


Reply    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 12-14-2016
chakrapani's Unix or Linux Image
chakrapani chakrapani is offline
Registered User
 
Join Date: Sep 2009
Last Activity: 10 January 2017, 8:11 AM EST
Location: America
Posts: 207
Thanks: 0
Thanked 8 Times in 7 Posts
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 Linux

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

Last edited by rbatte1; 12-15-2016 at 06:09 AM.. Reason: Changed ICODE tags to CODE tags
Sponsored Links
    #2  
Old Unix and Linux 12-14-2016
Corona688 Corona688 is offline Forum Staff  
Mead Rotor
 
Join Date: Aug 2005
Last Activity: 28 April 2017, 12:18 PM EDT
Location: Saskatchewan
Posts: 22,099
Thanks: 1,075
Thanked 4,159 Times in 3,849 Posts
Sorry, that - and the entire rest of the common gateway interface - is something a web server does for you.
Sponsored Links
    #3  
Old Unix and Linux 12-15-2016
chakrapani's Unix or Linux Image
chakrapani chakrapani is offline
Registered User
 
Join Date: Sep 2009
Last Activity: 10 January 2017, 8:11 AM EST
Location: America
Posts: 207
Thanks: 0
Thanked 8 Times in 7 Posts
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 Unix and Linux 12-15-2016
Corona688 Corona688 is offline Forum Staff  
Mead Rotor
 
Join Date: Aug 2005
Last Activity: 28 April 2017, 12:18 PM EDT
Location: Saskatchewan
Posts: 22,099
Thanks: 1,075
Thanked 4,159 Times in 3,849 Posts
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.
Sponsored Links
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux 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
Post using nc(netcat) ajaypadvi Shell Programming and Scripting 3 07-27-2011 08:04 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



All times are GMT -4. The time now is 05:44 PM.