Visit Our UNIX and Linux User Community


Searching file for pattern, output to file (BASH)


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Searching file for pattern, output to file (BASH)
# 1  
Old 04-10-2012
Question Searching file for pattern, output to file (BASH)

Hello,
I'm trying to write a script in Bash to assist in pentesting. Essentially I'm looking to use a script to search for some terms in a log file and then send that key information into another file.

The log files consist of HTTP and SSL information that someone creates while browsing and logging into a website. But the login information gets buried underneath all of the HTTP information.

The output for the passwords vary such as this:
Code:
&pass=password123

Code:
&passwd=password123

However the username can be found easily by using a search for %40 because all usernames that are email's show up in the log like this:
Code:
testusername%40gmail.com

Is there anyway I can take the whole line where one of the above examples show up and print that into a file?

Thanks

EDIT: (This is what I have so far, I forgot to put it in with the original post)
Code:
read -p "What file are you searching? " fname
read -p "What are you looking for? " pattern
if [ -f "$fname" ]
then
    result=$(grep -i "$pattern" "$fname")
    echo "$result"
fi

# 2  
Old 04-10-2012
So what are you trying to extract from the logfile...the username and password information.
# 3  
Old 04-10-2012
Quote:
Originally Posted by shamrock
So what are you trying to extract from the logfile...the username and password information.
Exactly but I'm looking to take the whole line where the username and password is found in the log file and output it to another file. I want the whole line because it tells me what site the username and password correlates to.

I might also want the line above the line containing the usename and password as well.

Here's an example of a line in the log file that has the username and password:
Code:
.tries=1&.src=fpctx&.md5=&.hash=&.js=&.last=&promo=&.intl=us&.lang=en-US&.bypass=&.partner=&.u=ce680od7o8okp&.v=0&.challenge=3FqbyPI8_gfTw7vxeai_eaIuZlwU&.yplus=&.emailCode=&pkg=&stepid=&.ev=&hasMsgr=0&.chkP=Y&.done=http%3A%2F%2Fwww.yahoo.com%2F&.pd=fpctx_ver%3D0%26c%3D%26ivt%3D%26sg%3D&.ws=1&.cp=0&nr=0&pad=5&aad=5&login=testusername%40gmail.com&passwd=password123&.save=&passwd_raw=

The line above that included with the line containing the username and password looks like this:
Code:
2012-04-10 12:41:19,033 POST Data (login.yahoo.com):
.tries=1&.src=fpctx&.md5=&.hash=&.js=&.last=&promo=&.intl=us&.lang=en-US&.bypass=&.partner=&.u=ce680od7o8okp&.v=0&.challenge=3FqbyPI8_gfTw7vxeai_eaIuZlwU&.yplus=&.emailCode=&pkg=&stepid=&.ev=&hasMsgr=0&.chkP=Y&.done=http%3A%2F%2Fwww.yahoo.com%2F&.pd=fpctx_ver%3D0%26c%3D%26ivt%3D%26sg%3D&.ws=1&.cp=0&nr=0&pad=5&aad=5&login=testusername%40gmail.com&passwd=password123&.save=&passwd_raw=


Previous Thread | Next Thread
Test Your Knowledge in Computers #813
Difficulty: Medium
The CSS RGBA alpha parameter defines the opacity as a number between 1.0 (fully transparent) and 0.0 (fully opaque).
True or False?

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Searching for a pattern from filenames stored in a file

Hi, I have got some 10 filenames stored in a file or displayed in the console as a result of some query i made.. Now I need to open each of these files and search for a pattern in these 10 files.. Can someone help me with this? Thanks, Jean (9 Replies)
Discussion started by: jeanjkj
9 Replies

2. Shell Programming and Scripting

Remove the footer from a flat file by searching a pattern

Hi, I have more than 30,000 records in a flat file. I want to remove footer from the file by searching a string pattern for the footer. Example.... let the flat file test.dat contains below records. record1 record2 record3 .. .. .. record31000 Total records 31000 I want to remove the... (6 Replies)
Discussion started by: gani_85
6 Replies

3. Shell Programming and Scripting

Searching for a pattern in a file...

The problem is similar to my yesterday post I Would like an awk script that does the following: I have a file (f1) that contains 1 2 3 4 5and another one (f2) that contains 8|a 9|b 0|c 1|d 2|e 3|f 4|g 5|h 6|i 7|jAnd I'd like to get this: 1|d 2|e 3|f 4|g (5 Replies)
Discussion started by: tukuyomi
5 Replies

4. UNIX for Dummies Questions & Answers

Searching the date pattern in a file

Hi, I would like to search the pattern based on the date like "2010/08/15". I tried using / in the file giving /<<pattern>>. when i tried this it turns to /2010/+8, but not going to the pattern what ever i want. This is how the data in the file. INFO | jvm 1 | 2010/05/26 13:30:33... (5 Replies)
Discussion started by: venkatesht
5 Replies

5. Shell Programming and Scripting

Searching a pattern in a file.

Hi Guys, I am writing a shell script to extract only the error message from a log file. I am having difficulty in searching the highlighted text in the below code. <runtimeinfo datetime="Sun Apr 04 20:02:52 EDT 2008" docid="" source="RAWDATA" event="ERROR"... (2 Replies)
Discussion started by: prash1986
2 Replies

6. Shell Programming and Scripting

Searching a pattern in file and deleting th ewhole line containing the pattern

Hi All, Please can someone assist in the script I have made that searches a pattern in a file and delete the whole line containing the pattern. #!bin/sh # The pattern that user want to add to the files echo "Enter the pattern of the redirect" read value # check if the user has... (1 Reply)
Discussion started by: Shazin
1 Replies

7. Shell Programming and Scripting

Searching words in a file containing a pattern

Hi all, I would like to print words in a file seperated by whitespaces containing a specific pattern like "=" e.g. I have a file1 containing strings like %cat file1 The= some= in wish= born <eof> .I want to display only those words containing = i.e The= , some=,wish= ... (5 Replies)
Discussion started by: sree_123
5 Replies

8. UNIX for Advanced & Expert Users

searching one pattern in file and also counting

Hi, everybody this is my pattern in a file thomson, nicolas 5-3871 Wong, Fred 4-4123 Jones, Thomas 1-4122 Salazar, Richard 5-2522 first name and last names of people fallowed by Tele. while i am searching for pattern first name begin with 'S'... (8 Replies)
Discussion started by: ksr.test
8 Replies

9. Shell Programming and Scripting

searching pattern in another file and print

Suppose u have a file 12 22 73 another file L22D SSS S12J LLL H77K PPP J25O LOP I73S lOP K99O PLO so output shud like S12J LLL L22D SSS I73S lOP Thanks (2 Replies)
Discussion started by: cdfd123
2 Replies

10. UNIX for Advanced & Expert Users

searching for a pattern in a file and forwarding result to another

Hi, I am trying to search through a file for a particular word, then once found, I want the search to carry on for another 30 lines and append all this to a new file. For example, I search for the word "test" in hello.txt: <--some code--> <--some code--> <--some code--> test <--some more... (7 Replies)
Discussion started by: ocelot
7 Replies

Featured Tech Videos