Saturday, 9 February 2019

Checking Firewall Status and Stopping Firewall in centos



[root@DSCenWeb01 ~]# sudo firewall-cmd --state
running
[root@DSCenWeb01 ~]# sudo systemctl stop firewalld

Friday, 8 February 2019

Celery Beat To Schedule Multiple Tasks




CELERY_BROKER_URL = 'redis://localhost:6379'
CELERY_RESULT_BACKEND = 'redis://localhost:6379'
CELERY_ACCEPT_CONTENT = ['application/json']
CELERY_RESULT_SERIALIZER = 'json'
CELERY_TASK_SERIALIZER = 'json'
CELERY_TIMEZONE = 'Asia/Kolkata'
CELERY_BEAT_SCHEDULE = {
    'NetworkBackup': {
      'task': 'NetworkAutomation.views.bkpSchedule',
      #'schedule': timedelta(seconds=3600),
      'schedule': crontab(minute=0, hour="*/6"),
    },
    'copyBkpFromAnsibletoJenkins': {
      'task': 'NetworkAutomation.views.copyBkpFromAnsibletoJenkins',
      #'schedule': crontab(minute="*/3"),
      'schedule': crontab(minute=0, hour="*/1"),
    },
    'copyBkpFromJenkinstoDjango': {
      'task': 'NetworkAutomation.views.copyBkpFromJenkinstoDjango',
      #'schedule': crontab(minute="*/2"),
      'schedule': crontab(minute=0, hour="*/1"),
    },

}

Thursday, 7 February 2019

SCP to Copy all files inside a directory from remote linux host using sshpass



 "sshpass -p '<password>' scp -r root@<remoteIP>:<sourcepath> <destinationpath>"

Django Celery task to copy file from remote host using SCP and SSHPASS



@task()
def copyBkpFromJenkinstoDjango():
       ip = ''
       port = ""
       username = ""
       password = ""
       cmd = "sshpass -p '<password>' scp -r root@<remoteIP>:/root/networkBackups /root/Django"
       #print (cmd)
       ssh=paramiko.SSHClient()
       ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
       ssh.connect(ip,port,username,password)
       stdin,stdout,stderr=ssh.exec_command(cmd)
       time.sleep(50)
       outlines=stdout.readlines()
       resp =''.join(outlines)
       #print(resp)
       return resp

Wednesday, 6 February 2019

Moving all files from one directory to another using Python


import shutil
import os
source = '/path/to/source_folder'
dest1 = '/path/to/dest_folder'

files = os.listdir(source)

for f in files:
        shutil.move(source+f, dest1)

Tuesday, 5 February 2019

Python script to trigger ansible playbook via SSH using paramiko SSH Client



       cmd = "ansible-playbook " +  <playbookname> + " -vvvv"
       print (cmd)
       ssh=paramiko.SSHClient()
       ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
       ssh.connect(ip,port,username,password)
       stdin,stdout,stderr=ssh.exec_command(cmd)
       time.sleep(50)
       outlines=stdout.readlines()
       resp =''.join(outlines)
       print(resp)
       return resp

Python Script to create File and append 10 dummy lines on every function call

def createFolder():
   try:
        if not os.path.exists(directory):
            os.makedirs(directory)
            return 'FolderCreated'
            return 'FolderNotCreated'

     f = open("/home/raghu/Desktop/raghu1234.txt","a+")

     for i in range(10):
        f.write("This is the line %d\r\n" % (i+1))

     f.close()

# FunctionCall
createFolder()

Git

1 git add ↳ It lets you add changes from the working directory into the staging area 2 git commit ↳ It lets you save a snapshot of currently...