LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 10-14-2015, 12:15 PM   #1
Ra'Jiska
Member
 
Registered: Apr 2013
Posts: 47

Rep: Reputation: Disabled
Post Remove End of string from pattern


Hello there,

I got an IP via TCPDUMP and I'd like to be able to to have the port of this IP removed.
Here is the format: 62.106.118.118.49954 ; how would I be able to remove the end '.49954' so I'd have: 62.106.118.118 ?

Thanks.

P.S: Don't worry about the IP, it's used against me to do DoS.

Last edited by Ra'Jiska; 10-14-2015 at 12:23 PM.
 
Old 10-14-2015, 12:31 PM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192
Please show us what you have done to try and solve this? Also you do not indicate if it is in a script, on the command line or any particular language. Help us to help you.
 
Old 10-14-2015, 12:41 PM   #3
Ra'Jiska
Member
 
Registered: Apr 2013
Posts: 47

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by grail View Post
Please show us what you have done to try and solve this? Also you do not indicate if it is in a script, on the command line or any particular language. Help us to help you.
Alrighty, yup', it's actually a script.
Someone is currently DoSing me and I'd like to have his ban automatic.

What I do is I send the output of tcpdump to a file, each line look like this:

19:20:45.620829 IP 62.106.118.118.49954 > xx.xx.xx.xx.xxxx: UDP, length 0

In this, I do only want to take '62.106.118.118' which is a variable, however, I did not manage to remove the extra '.49954'.
Here is what I've done so far to achieve this: echo "19:20:45.620829 IP 62.106.118.118.49954 > xx.xx.xx.xx.xxxx: UDP, length 0" | grep -o -P '(?<=IP ).*(?= >)' ; which outputs: 62.106.118.118.49954 .

Now, from this output, I'd like to get read of the four last characters (which might be variable).
Thank you very much for your help !
 
Old 10-14-2015, 01:38 PM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192
Ok, I am still a little lost by some of the terminology.

Is the string '19:20:45.620829 IP 62.106.118.118.49954 > xx.xx.xx.xx.xxxx: UDP, length 0' stored in a variable?
If yes, I would do the following:
Code:
str='19:20:45.620829 IP 62.106.118.118.49954 > xx.xx.xx.xx.xxxx: UDP, length 0'

ip=${str#*IP }
ip=${ip% *}
ip=${ip%.*}

echo "$ip"
If on the other hand it is output and we have to use grep:
Code:
echo '19:20:45.620829 IP 62.106.118.118.49954 > xx.xx.xx.xx.xxxx: UDP, length 0' | grep -oP '(?<=IP ).*(?=\.[0-9]* >)'
 
Old 10-14-2015, 01:44 PM   #5
Ra'Jiska
Member
 
Registered: Apr 2013
Posts: 47

Original Poster
Rep: Reputation: Disabled
Smile

Thank you very much, the second part helped and did the job.
Dunno exacly how it does it but will make sure to check some documentation when I'll have time to !

Thanks again.
 
Old 10-14-2015, 09:18 PM   #6
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,360

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
Given the result at post #3 I was thinking of piping through cut
Code:
echo '62.106.118.118.49954'|cut -d'.' -f1-4
62.106.118.118
 
Old 10-15-2015, 10:13 AM   #7
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192
Quote:
Dunno exacly how it does it but will make sure to check some documentation when I'll have time to !
Actually, I just added to your existing code Now the look behind needs to see a period followed by numbers and then your space and greater than sign
 
Old 10-16-2015, 06:29 AM   #8
Ra'Jiska
Member
 
Registered: Apr 2013
Posts: 47

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by chrism01 View Post
Given the result at post #3 I was thinking of piping through cut
Code:
echo '62.106.118.118.49954'|cut -d'.' -f1-4
62.106.118.118
Oh yea, this one is quite good too, thank you !

And yup', I saw Grail, I meant the end which I had trouble to understand (and is less intuitive than chrism01's one).
That said, thank you very much guys, you allowed me to make a script that prevents the bad boy from continuing to DOS my server !
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Replace a string with pattern within that string shreyas08 Linux - Newbie 4 03-14-2012 12:07 AM
[SOLVED] /bin/bash if statement pattern search, end of pattern special character? headhunter_unit23 Programming 3 04-29-2010 08:05 AM
how to match for multiple pattern at the end of given string Santoshkb Programming 2 06-23-2008 10:42 AM
remove the 'd' character from the end of string powah Programming 6 11-08-2007 07:00 AM
Rewrite rule with query string in the pattern string basahkuyup Linux - Newbie 2 10-17-2006 02:06 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 10:45 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration