PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Thursday, September 15, 2022

[FIXED] How to cut specifics parts of a line with Python?

 September 15, 2022     debian, fail2ban, printing, python-3.x     No comments   

Issue

I'm trying to make a script that display recent Ban, Found or Unban infos that are in fail2ban logs (/var/log/fail2ban), but I want to hide this selection for all lines :

,325 fail2ban.actions [424]: NOTICE [sshd]

In order to have this :

2022-07-02 18:15:17 Unban 192.168.200.20

And not this :

2022-07-02 18:15:17,325 fail2ban.actions [424]: NOTICE [sshd] Unban 192.168.200.20

Here is my code :

with open('/var/log/fail2ban.log', 'r') as f:
     for line in f.readlines():
             if 'Ban' in line:
                   print(line)

             if 'Unban' in line:
                    print(line)

             if 'Found' in line:
                    print(line)

Solution

You can use replace function .

with open('/var/log/fail2ban.log', 'r') as f:
    for line in f.readlines():
            line = line.replace('that string you dont want', '')
            if 'Ban' in line:
                  print(line)
            if 'Unban' in line:
                   print(line)
            if 'Found' in line:
                    print(line)


Answered By - Tonmoy Ahmed Dhroubo
Answer Checked By - Candace Johnson (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing