SED to add a suffix


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting SED to add a suffix
# 1  
Old 01-28-2010
SED to add a suffix

Hi all,

Im trying to make a proper hosts.allow with the lists of sshbl.org to block the ssh brute force attackers.

The list is a text file with an IP on every line.
What I've gotten up sofar is to prefix "sshd : " on every line, but I need a " : deny" suffix behind every line as well.
How can I do this? I've made the part that is unknown to me underlined.
Code sofar:
Code:
#!/usr/local/bin/bash
rm /tmp/base.txt
fetch -o /tmp/base.txt http://www.sshbl.org/lists/base.txt
rm /tmp/base.tmp
cat /tmp/base.txt | grep '[0-9]*[0-9]*[0-9][.][0-9]*[0-9]*[0-9][.][0-9]*[0-9]*[0-9]' | sed 's/^/sshd : /' > /tmp/base.tmp
cat /tmp/base.tmp | grep '[0-9]*[0-9]*[0-9][.][0-9]*[0-9]*[0-9][.][0-9]*[0-9]*[0-9]' | sed 's/???/ : deny /' > /tmp/base.tmp2
rm /etc/hosts.allow
cp /tmp/base.tmp2 /etc/hosts.allow
cat /root/SSHBL/hosts.allow >> /etc/hosts.allow

Thx for the help.
# 2  
Old 01-28-2010
Using the first few lines as example:
Code:
$ cat base.txt
# sshbl.org
# Thu Jan 28 11:45:02 2010 CET
#
# source ip
61.155.177.2
217.120.162.182
60.191.5.181
88.77.54.74
210.21.225.204
174.34.172.133
109.123.74.86
$ awk '!/^#/{print "sshd : "$0" : deny"}' base.txt
sshd : 61.155.177.2 : deny
sshd : 217.120.162.182 : deny
sshd : 60.191.5.181 : deny
sshd : 88.77.54.74 : deny
sshd : 210.21.225.204 : deny
sshd : 174.34.172.133 : deny
sshd : 109.123.74.86 : deny

That ok?
# 3  
Old 01-28-2010
Exactemundo!
awk it is, thx alot (:

---------- Post updated at 12:10 PM ---------- Previous update was at 12:06 PM ----------

In case anyone cares, I added a different output, since the above command did the output to stdout instead of a file.
Here is the entire script:
Code:
#!/usr/local/bin/bash
rm /tmp/base.txt
fetch -o /tmp/base.txt http://www.sshbl.org/lists/base.txt
rm /tmp/base.tmp
awk '!/^#/{print "sshd : "$0" : deny"}' /tmp/base.txt > /tmp/base.tmp
rm /etc/hosts.allow
cp /tmp/base.tmp /etc/hosts.allow
cat /root/SSHBL/hosts.allow >> /etc/hosts.allow

# 4  
Old 01-28-2010
However, you can get quite a bit of better security by blocking these IPs with a firewall (pf or iptables) and/or moving the SSH port away from the default 22.
# 5  
Old 01-28-2010
Hey Pludi,

Yeah I know, I always ran ssh @ 22222, but from some locations the tcp 22222 outbound was blocked, while 22 wasn't.
Im still working to migrate from the IPFW to PF as firewall, since PF can use these kind of files as input for firewall rules.
I also run sshguard-ipfw which puts an attacker in the IPFW firewall when guessing usernames.
So this hosts.allow is just an extra Smilie
Thx for the advice/thinking with me though!
# 6  
Old 01-29-2010
I know 2 utilities against brute-force attacks (and aother attacks)
1. denyhosts : acts on /etc/hosts.deny.
2. fail2ban : acts on iptables.
Search a bit around about those 2 and you'll find which you prefer.
# 7  
Old 01-29-2010
Hi Frans,

I'm using sshguard already, quite simulair to fail2ban, but it can be used for more services then only sshd.
Thx for your advice though!

---------- Post updated at 10:17 AM ---------- Previous update was at 10:17 AM ----------

btw Mod, the thread can be locked, I got my answer.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Prefix/Suffix on same file

Hi, I want to add prefix and suffix on line# 205 using SED or AWK and want to change on the same file without creating new file. This command will be used in the bash script Am using Bash shell Regards Nayaj (3 Replies)
Discussion started by: Nayaj
3 Replies

2. Shell Programming and Scripting

Suffix formatting with awk

i would like to format the 9 character with suffix as "0". i tried below it doesn't work. >a=12345 > echo $a | awk '{printf "%-09s\n",$1}' >12345 required output is 123450000 can you guys help me out ? (7 Replies)
Discussion started by: expert
7 Replies

3. Shell Programming and Scripting

Rsync script to rewrite suffix - BASH, awk, sed, perl?

trying to write up a script to put the suffix back. heres what I have but can't get it to do anything :( would like it to be name.date.suffix rsync -zrlpoDtub --suffix=".`date +%Y%m%d%k%M%S`.~" --bwlimit=1024 /mymounts/test1/ /mymounts/test2/ while IFS=. read -r -u 9 -d '' name... (1 Reply)
Discussion started by: jmituzas
1 Replies

4. Shell Programming and Scripting

Column content match and add suffix

My input chr3 galGal3_xenoRefFlat CDS 4178235 4178264 0.000000 + 0 gene_id "T6J4.19; T6J4_19"; transcript_id "T6J4.19; T6J4_19"; chr3 galGal3_xenoRefFlat exon 4178235 4178264 0.000000 + . gene_id "T6J4.19; T6J4_19"; transcript_id "T6J4.19;... (2 Replies)
Discussion started by: jacobs.smith
2 Replies

5. Shell Programming and Scripting

Arguments and suffix name

While calling shell script i need to use prefix . Any idea? Ex: myscript.sh -parameter1 "AA;BB" -parameter2 "DD;E" (5 Replies)
Discussion started by: mnjx
5 Replies

6. Shell Programming and Scripting

suffix a sequence in awk

hi I have a string pattern like ... ... 000446448742 00432265 040520100408 21974435 DEWSWATER GARRIER AAG IK4000 N 017500180000000000000000077000000000100 000446448742 00580937 040520100408 32083576 PEWSWATER BARRIER DAG GK4000 ... (6 Replies)
Discussion started by: zainravi
6 Replies

7. Shell Programming and Scripting

Removing domain suffix with SED

Hi Experts, I have a syslog file from 1000's of different hosts which I want to adjust by removing the domain suffix from the hosts. My previous attempts haven't managed to match all the different lenghts of the subdomains which are being logged. Could somebody suggest which sed syntax... (6 Replies)
Discussion started by: krypton
6 Replies

8. Shell Programming and Scripting

how to find files not suffix with .c

if I want to search those files which were suffix with .c, I can use find ./ -name *.c but how to find out those files which were not suffix with .c ?? Thanks a lot! (2 Replies)
Discussion started by: cqlouis
2 Replies

9. Shell Programming and Scripting

csplit suffix-format: how to?

I am using GNU csplit to extract chapters from a big file into smaller files. I want to use the -b option -b, --suffix-format=FORMAT use sprintf FORMAT instead of %d but I have failed so far. 1) All the generated files need to have a suffix .txt at the end 2) They have to look like... (1 Reply)
Discussion started by: MarioColuzzi
1 Replies

10. Shell Programming and Scripting

prefix suffix to each argument

Hi, I have a variable, which contains comma separated values. Something like. StringA="abc,def,ghi,jkl" I want to apply prefix and suffix to each value in the string without using any loops. Say if Prefix is Pre_ and Suffix is _Suf then I need to get ... (1 Reply)
Discussion started by: tostay2003
1 Replies
Login or Register to Ask a Question