d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Piping In Unix
12Next
Add Reply New Topic New Poll
Member
Posts: 23,862
Joined: Aug 16 2006
Gold: 20.00
Jan 29 2013 07:39pm
so my professor didn't give me a reall suffice answer to this


but the way I understand piping is you take the result of the left argument and use it as a parameter for the right argument

so for example if you do

Code
ls -l | grep q0
# if you must know, we ls -l inside the class directory to find our quiz (labeled q0)


the output from the ls would be used as the location parameter for grep to search in correct?

so basically my question is:

does the output from the left argument fill in whatever parameter is first left blank on the right argument? Or is it set to do certain things with certain commands?

This post was edited by Eep on Jan 29 2013 07:40pm
Member
Posts: 13,425
Joined: Sep 29 2007
Gold: 0.00
Warn: 20%
Jan 29 2013 07:44pm
pretty much but thats not how grep works

grep takes 2 parameters ones a piece of data and the other is a search term and it will output all data that matches that term
Member
Posts: 23,862
Joined: Aug 16 2006
Gold: 20.00
Jan 29 2013 07:47pm
Quote (AbDuCt @ Jan 29 2013 08:44pm)
pretty much but thats not how grep works

grep takes 2 parameters ones a piece of data and the other is a search term and it will output all data that matches that term


what is the piece of data exactly? the search term you are referring to is like a string right?
Member
Posts: 4,605
Joined: Sep 15 2011
Gold: 9,464.00
Jan 29 2013 07:50pm
Member
Posts: 13,425
Joined: Sep 29 2007
Gold: 0.00
Warn: 20%
Jan 29 2013 07:50pm
Quote (Eep @ Jan 29 2013 09:47pm)
what is the piece of data exactly? the search term you are referring to is like a string right?


basically the pipe operator | takes the std output from one application and feeds it into another. in the case of grep its command line parameters are

grep [~OPTIONS] pattern file

if there is no file stated it checks stdin for data and since you used the | operator to take the stdout of ls and pipes it to the stdin of grep.

Member
Posts: 23,862
Joined: Aug 16 2006
Gold: 20.00
Jan 29 2013 07:54pm
Quote (AbDuCt @ Jan 29 2013 08:50pm)
basically the pipe operator | takes the std output from one application and feeds it into another. in the case of grep its command line parameters are

grep [~OPTIONS] pattern file

if there is no file stated it checks stdin for data and since you used the | operator to take the stdout of ls and pipes it to the stdin of grep.


but you couldn't, say, pipe a pattern for grep to search for?
Member
Posts: 13,425
Joined: Sep 29 2007
Gold: 0.00
Warn: 20%
Jan 29 2013 08:00pm
Quote (Eep @ Jan 29 2013 09:54pm)
but you couldn't, say, pipe a pattern for grep to search for?


no. go read the man page for grep `man grep` it clearly says that if a file is not specified after the pattern to search for it trys reading from stdin.

something tells me you dont know about the standard inputs and outputs and their file discriptors.

0 stdin
1 stdout
2 stderr

any program that launches (including bash and everything else) always open 3 file discriptors at run time without you knowing. they are 0, 1, 2. these are kind of "links" that if you write any data to them they will appear to the screen or otherwise.

for example create a simple

Code
int main()
{
  char str[10] = "testing";

  fputs(str, 1);

return 0;
}


will output "testing" to your screen.

although in C and C++ there are defined macros for these file descriptors so if you see any weird things like that which is non stnadard (people use cerr, cout, cin) they will most likely sue the defines STDOUT, STDERR, and STDIN.

edit:: overall | takes stdout (1) and feeds it to stdin (0) of another running application.


This post was edited by AbDuCt on Jan 29 2013 08:07pm
Member
Posts: 23,862
Joined: Aug 16 2006
Gold: 20.00
Jan 29 2013 08:04pm
Quote (AbDuCt @ Jan 29 2013 09:00pm)
no. go read the man page for grep `man grep` it clearly says that if a file is not specified after the pattern to search for it trys reading from stdin.

something tells me you dont know about the standard inputs and outputs and their file discriptors.

0 stdin
1 stdout
2 stderr

any program that launches (including bash and everything else) always open 3 file discriptors at run time without you knowing. they are 0, 1, 2. these are kind of "links" that if you write any data to them they will appear to the screen or otherwise.

for example create a simple

Code
int main()
{
  char str[10] = "testing";

  fputs(str, 1);

return 0;
}


will output "testing" to your screen.

although in C and C++ there are defined macros for these file descriptors so if you see any weird things like that which is non stnadard (people use cerr, cout, cin) they will most likely sue the defines STDOUT, STDERR, and STDIN.

edit:: overall | takes stdout (1) and feeds it to stdin (0) of another running application.



ahh. I didn't know that about grep. I did know about the stdin stdout stderr, in light context.
Member
Posts: 13,425
Joined: Sep 29 2007
Gold: 0.00
Warn: 20%
Jan 29 2013 08:10pm
Quote (Eep @ Jan 29 2013 10:04pm)
ahh. I didn't know that about grep. I did know about the stdin stdout stderr, in light context.


yea its interesting

fgets or rather any file command also works on this basis. fgets(str, 10, 0); would get 10 characters from stdin.

on a side note fopen() opens a link to a file and gives it a file discriptor number. creating sockets works on this basis as well. and it is actually possible to read all the open file discriptors in an application and hijack a active socket of an application and send data through it from outside the program. i helped write a PoC shellcode exploit that allows an attacker to hijack a socket the exploit came in on and do a reverse connection back to the attacker for command line access to evade firewalls (because the socket is already open with the attacker) and ids systems. this is a new technique that helps in tighter security systems where you cannot simple start a new process or create a new outgoing socket because of firewall rules. although its not very viable to use in the wild simply because the sheer size of the bytecode. it was like 79 bytes alone iirc

This post was edited by AbDuCt on Jan 29 2013 08:11pm
Member
Posts: 23,862
Joined: Aug 16 2006
Gold: 20.00
Jan 29 2013 08:14pm
Quote (AbDuCt @ Jan 29 2013 09:10pm)
yea its interesting

fgets or rather any file command also works on this basis. fgets(str, 10, 0); would get 10 characters from stdin.

on a side note fopen() opens a link to a file and gives it a file discriptor number. creating sockets works on this basis as well. and it is actually possible to read all the open file discriptors in an application and hijack a active socket of an application and send data through it from outside the program. i helped write a PoC shellcode exploit that allows an attacker to hijack a socket the exploit came in on and do a reverse connection back to the attacker for command line access to evade firewalls (because the socket is already open with the attacker) and ids systems. this is a new technique that helps in tighter security systems where you cannot simple start a new process or create a new outgoing socket because of firewall rules.


wow..

I just read up a bit about sockets the other day (not for class, for fun).....I also read a little win32 tut on how to create a simple BLOCKING socket? in c++.

I am pretty interested in network comm. and how that all works.

The code here http://www.win32developer.com/tutorial/winsock/winsock_tutorial_1.shtm

seems relatively light, but I feel there is a lot more to it probably?

Also curious as to how sockets work when you modularize programs....what file do you put that sort of stuff in lol? Or would it belong to a class? Beats me ><

Go Back To Programming & Development Topic List
12Next
Add Reply New Topic New Poll