Python For System Admin Fast BootCamp. Modifying file read and write and using regular expression.

In this tutorial fast boot camp for python for system admin , we will look into how to open , edit file and use regular expression for quickly replace text . useful for system admin.

File Modes in Python

ModeDescription
‘r’This is the default mode. It Opens file for reading.
‘w’This Mode Opens file for writing.
If file does not exist, it creates a new file.
If file exists it truncates the file.
‘x’Creates a new file. If file already exists, the operation fails.
‘a’Open file in append mode.
If file does not exist, it creates a new file.
‘t’This is the default mode. It opens in text mode.
‘b’This opens in binary mode.
‘+’This will open a file for reading and writing (updating)

example of code how to open file in python.

  • File open to write :
# we are trying to write test.txt with some line .
file = open ("test.txt" , "w")
for i in range (10):
    file.write("Print this line number = %d n" % (i+1))
file.close()
  • File open to read
# Lets try to open file test.txt that we previously wrote
file = open("test.txt" , "r")
for i in file.readlines():
    print (i)
file.close()
 Print this line number = 1
 Print this line number = 2
 Print this line number = 3
 Print this line number = 4
 Print this line number = 5
 Print this line number = 6
 Print this line number = 7
 Print this line number = 8
 Print this line number = 9
 Print this line number = 10 

Python Regular Expression Tutorial

Please see this link for quick reference of regular expression. https://dodocrypto.com/index.php/2019/11/12/regular-expression-cheat-sheet-for-dummies/

List of Python re module .

The re module offers a set of functions that allows us to search a string for a match:

FunctionDescription
findallReturns a list containing all matches
searchReturns a Match object if there is a match anywhere in the string
splitReturns a list where the string has been split at each match
subReplaces one or many matches with a string

Let’s start with findall() . The findall() function returns a list containing all matches. Note : We will be using test.txt from example that we made earlier.

import re;
file = open ("test.txt" , "r" )
for i in file.readlines():
    result = re.findall (r"d+" , i)
    print (result)
file.close()
----------------------------------------------------------------
this will print out the number 1 to 10 and d+ mean match any digit or numeric at least one or more

The search() Function

The search() function searches the string for a match, and returns a Match object if there is a match.If there is more than one match, only the first occurrence of the match will be returned:

import re
file = open ("test.txt", "r")
for i in file.readlines():
    result = re.search (r"d+" , i)
    print (result.group())
file.close()
-----------------------------------
This will print 1 - 10

The split() Function

The split() function returns a list where the string has been split at each match:

import re
file = open ("test.txt" , "r")
for i in file.readlines():
    result = re.split (r"s", i)
    print (result)
file.close()
------------------- this will return
['Print', 'this', 'line', 'number', '=', '1', '', '']
 ['Print', 'this', 'line', 'number', '=', '2', '', '']
 ['Print', 'this', 'line', 'number', '=', '3', '', '']
 ['Print', 'this', 'line', 'number', '=', '4', '', '']
 ['Print', 'this', 'line', 'number', '=', '5', '', '']
 ['Print', 'this', 'line', 'number', '=', '6', '', '']
 ['Print', 'this', 'line', 'number', '=', '7', '', '']
 ['Print', 'this', 'line', 'number', '=', '8', '', '']
 ['Print', 'this', 'line', 'number', '=', '9', '', '']
 ['Print', 'this', 'line', 'number', '=', '10', '', '']

The sub() Function

The sub() function replaces the matches with the text of your choice:

import re
file = open ("test.txt" , "r")
result = file.read()
filew = open ("test.txt" , "w")
## We replace Print in test.txt to be New Print
result = re.sub(r"Print" , "New Print" , result)
## We replace number to blank
result = re.sub(r"d+" , "" , result)
## We replace number to Number
result = re.sub(r"number", "Number" , result )
filew.write(result)
filew.close()
file.close()
 ================
this will result in file text
New Print this line Number =
 New Print this line Number =
 New Print this line Number =
 New Print this line Number =
 New Print this line Number =
 New Print this line Number =
 New Print this line Number =
 New Print this line Number =
 New Print this line Number =
 New Print this line Number =

Remember to use readlines() in a big file instead of read()

Check out next tutorial at :

Mitre CVE database . Common Vulnerabilities and Exposures . Exploit Database.

MITRE

MITRE is a US government-sponsored organization that manages federally funded research and development centers (FFRDC). Its website emphasizes commercial publications and information related to their FFRDCs such as the National Cybersecurity program. It also maintains one of the biggest and widely referenced CVE databases currently available, searchable by the public.

https://cve.mitre.org/

Secure Coding Practice For C , C++ , Java , Perl , Android.

Welcome

This site supports the development of coding standards for commonly used programming languages such as C, C++, Java, and Perl, and the Android™ platform. These standards are developed through a broad-based community effort by members of the software development and software security communities.

For more information about this project and to see tips on how to contribute, please see the Development Guidelines.

Standards Development Area

The following development areas enable you to learn about and contribute to secure coding standards for commonly used programming languages C, C++, Java, and Perl. 

 SEI CERT C Coding Standard

SEI CERT C++ Coding Standard    

Android TM   Secure Coding Standard     

SEI CERT Oracle Coding Standard for Java

SEI CERT Perl Coding Standard

Setup Alibaba Cloud ECS Server Cloud Postfix Email Using Sendgrid

By default Alibaba Cloud Block Out going port 25 . We need to setup smtp relay for it. We choose https://sendgrid.com . Free account is available .

First register at https://sendgrid.com/ choose free plan or buy plan from them .

Next We setup postfix in our centos 7.

yum -y install postfix cyrus-sasl-plain mailx

Postfix will need to be restarted before the SASL framework will be detected.

systemctl restart postfix
systemctl enable postfix
Open the /etc/postfix/main.cf and add the following lines to the end of the file.
vi /etc/postfix/main.cf
add the configuration below
myhostname = hostname.example.com
relayhost = [smtp.sendgrid.net]:587
smtp_use_tls = yes
smtp_sasl_auth_enable = yes
smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd
smtp_tls_CAfile = /etc/ssl/certs/ca-bundle.crt
smtp_sasl_security_options = noanonymous
smtp_sasl_tls_security_options = noanonymous
Open /etc/postfix/sasl_passwd and add the configuration.
vi /etc/postfix/sasl_passwd
[smtp.sendgrid.net]:587 username:password
To get the username and password above : go to
https://app.sendgrid.com/guide/integrate/langs/smtp

A Postfix lookup table must now be generated from the sasl_passwd text file by running the following command.

postmap /etc/postfix/sasl_passwd

Access to the sasl_passwd files should be restricted.

chown root:postfix /etc/postfix/sasl_passwd
chmod 640 /etc/postfix/sasl_passwd
Lastly, reload the Postfix configuration.
systemctl restart postfix

Lastly test messages from mail to yourself and see /var/log/maillog

echo “This is a test.” | mail -s “test message” user@example.net

tail -f /var/log/maillog

Setup Centos 7 / RHEL 7 WordPress + FreeSSL

Since wordpress is not supporting php that come with centos 7 default package anymore , we use remi php repo , at current of this tutorial , stable php is version 7.3. Follow guide through of the following command . it just take than 5 minute before you have wordpress site running with Let’s encrypt free ssl .

# yum -y update

# hostnamectl set-hostname yourdomain.com

# systemctl restart systemd-hostnamed

# yum -y install http://rpms.remirepo.net/enterprise/remi-release-7.rpm

# yum install yum-utils

# yum-config-manager –disable remi-php54

# yum-config-manager –enable remi-php73

# yum install httpd mod_ssl mariadb mariadb-server php php-common php-mysql php-gd php-xml php-mbstring php-mcrypt

# systemctl enable mariadb

# systemctl enable httpd

# systemctl start httpd

# systemctl start mariadb

# mysql_secure_installation

# mysql -u root -p
Enter password:
## Create database ##
CREATE DATABASE wordpress;
## Creating new user ##
CREATE USER wordpress@localhost IDENTIFIED BY "change_this";
## Grant privileges to database ##
GRANT ALL ON wordpress.* TO wordpress@localhost;
## FLUSH privileges ##
FLUSH PRIVILEGES;
## Exit ##
exit

# cd /var/www/html

# yum -y install wget

# wget http://wordpress.org/latest.tar.gz

# tar -xvzf latest.tar.gz

# chown -R apache:apache /var/www/html/wordpress

# vi /etc/httpd/conf.d/yourdomain.com.conf

<VirtualHost *:80>
   ServerAdmin admin@yourdomain.com
   DocumentRoot /var/www/html/wordpress
   ServerName yourdomain.com
   ServerAlias www.yourdomain.com
   ErrorLog /var/log/httpd/yourdomain.com-error-log
   CustomLog /var/log/httpd/yourdomain.com-acces-log common
</VirtualHost>

# systemctl restart httpd

Now we are setting up Let’s Encrypt SSL Certificate to Secure Apache on RHEL/CentOS 7. Follow the walk through

# yum install epel-release

# cd /usr/local/

# git clone https://github.com/letsencrypt/letsencrypt

# cd /usr/local/letsencrypt/

Almost Finished Hang in there . now just run ./letsencrypt-auto and follow the installation. As easy as that and restart httpd.

# ./letsencrypt-auto

# systemctl restart httpd

Verify our brand new https by clicking link below
https://www.ssllabs.com/ssltest/analyze.html
Enjoy !!!
 

Computer Science BootCamp Course.

Welcome to Computer Science Program . This course is designed by harvard university and hopefully it will be useful for all of you the reader too. This Course is to prepare me or you as the reader to pick up any programming language such as ( c , c++ , c# , java , python , perl and many more )

Python 3 For System Admin

Learn to Code

If you’ve ever spent hours renaming files or updating hundreds of spreadsheet cells, you know how tedious tasks like these can be. But what if you could have your computer do them for you?

In Automate the Boring Stuff with Python, you’ll learn how to use Python to write programs that do in minutes what would take you hours to do by hand-no prior programming experience required. Once you’ve mastered the basics of programming, you’ll create Python programs that effortlessly perform useful and impressive feats of automation to:

  • Search for text in a file or across multiple files
  • Create, update, move, and rename files and folders
  • Search the Web and download online content
  • Update and format data in Excel spreadsheets of any size
  • Split, merge, watermark, and encrypt PDFs
  • Send reminder emails and text notifications
  • Fill out online forms

Step-by-step instructions walk you through each program, and practice projects at the end of each chapter challenge you to improve those programs and use your newfound skills to automate similar tasks.

Don’t spend your time doing work a well-trained monkey could do. Even if you’ve never written a line of code, you can make your computer do the grunt work. Learn how in Automate the Boring Stuff with Python.

Table of Content

Additional Content

About the Author

Al Sweigart is a software developer and teaches programming to kids and adults. He has written several books for beginners, including Scratch Programming PlaygroundCracking Codes with PythonInvent Your Own Computer Games with Python, and Making Games with Python & Pygame

Python Good Link

Documentation

  • Official Python 3 Documentation – “official”/technical explanation of what a particular function/operator does, examples of correct syntax, what the various libraries are, etc.

Textbooks/Tutorials

Debugging

  • Python Tutor – an excellent way to actually visualize how the interpreter actually reads and executes your code
  • DiffChecker – compares two sets of text and shows you which lines are different
  • Debugging in Python – steps you can take to try to debug your program

Software

Other Q&A

  • Stack Overflow – a large Q&A forum for programming concepts (not just Python). Try searching here before you post on the edX forum, and you may find that someone has already answered your question.

More practice problems

  • Python Challenge – a series of puzzles you can try to test your Python abilities
  • Project Euler – additional programming challenges you can try once your Python knowledge becomes stronger; problems are sorted by increasing difficulty
  • Coding Bat – problems you can solve within an online interpreter
  • Codewars – improve your skills by training on real code challenges

Different Between Package , Import and Interface in Java

package command in java tell compiler we going to save the source code in some directory .
Example :

package example;     /* This tell Java Compiler javac to save
                                         this file which is PackageImport.java
                                         in example directory
                                         */
public class PackageImport {
public static void main ( String[] argv) {
System.out.println (” example PackageImport is called “);
      }
}

To compile it run : javac -d directory PackageImport.java
example how to run compiled file is : java /destination/directory/PackageImport
Continue reading “Different Between Package , Import and Interface in Java”

Java Lesson Understanding Variable Scope

public class TestScope {
public static void main (String[] argv) {
               int x; // ———————-> X Exist until Closing bracket
               // Understanding Scope for x
              {
              char y;
               x = 15; // Notice we can access x here
               y = 65;
              System.out.println (“The Sum of x + y = “+ (x+y));
              } // —-> Y exist until curly or before bracket therefore it give error to print y
              // System.out.println(“The varable of y won’t exist any more in this scope” + y);
      } // ———————————> X exist before this curly bracket .
}
Explanation : variable scope is exist from first bracket { until the next matches closing bracket }
there are exception though example in for loop.

Continue reading “Java Lesson Understanding Variable Scope”