Create a Log File in PowerShell Script
Last Updated :
23 Jan, 2023
In this article, we will be discussing a shell script that can be used to report the cumulative connection time for month/year entries found in a system log file. This script can be useful for network administrators or system administrators who need to analyze log files and get an overview of how much time a particular user or IP address has spent connected to the system. To make this task easier, we can use a shell script to automate the process of extracting this information from a log file. The script provided in this article can be used to report the cumulative connection time for month/year entries found in a system log file. It uses a combination of Linux commands like grep, awk, cut, pastes, and bc to extract the information from the log file and calculate the cumulative connection time. The script can be easily modified to suit the specific requirements of a particular system or log format. Additionally, this script is a basic example and can be extended in many ways to include more advanced filtering or computation.
The Script:
Here is the full script that can be used to generate a report of the cumulative connection time for month/year entries found in a log file:
#!/bin/bash
# Define the log file to be analyzed
log_file="/path/to/logfile.log"
# Define the output file for the report
output_file="/path/to/report.txt"
# Initialize a variable to store the cumulative connection time
connection_time=0
# Get a list of unique months and years from the log file
months_years=$(grep -oE "[A-Za-z]{3} [0-9]{4}" "$log_file" | sort | uniq)
# Loop through each month and year
for month_year in $months_years; do
# Extract the number of minutes for the current month and year
minutes=$(grep "$month_year" "$log_file" | awk '{print $2}' | cut -d ':' -f 2 | paste -sd+ - | bc)
# Add the minutes to the cumulative connection time
connection_time=$((connection_time + minutes))
# Output the result to the report file
echo "$month_year: $minutes minutes" >> "$output_file"
done
# Output the final cumulative connection time to the report file
echo "Total connection time: $connection_time minutes" >> "$output_file"
Explanation:
- The first line of the script specifies that it should be executed using the bash shell.
- The log_file variable stores the path to the log file that will be analyzed. This path should be updated to the path of the log file on the system.
- The output_file variable stores the path to the file where the report will be saved. This path should be updated to a location on the system where the report can be saved.
- The connection_time variable is initialized to 0 and will be used to store the cumulative connection time.
- The script uses the grep command to extract all the unique month-year entries from the log file, these entries are stored in the months_years variable
- Next script iterate through each unique month-year entry in the months_years variable
- For each iteration, the script uses the grep command to extract all the entries from the log file that match the current month-year.
- Then the script uses the awk command to extract the connection time, which is the second column of the log file, from the matches and stores it in the minutes variable.
- Then it uses the cut command to remove the colon so that it is left with only the minutes and paste command to concatenate the minutes with a plus sign.
- Then it uses the bc command to calculate the sum of minutes.
- The script then adds the number of minutes for the current month and year to the cumulative connection time.
- It then outputs the result for the current month and year and finally, after iterating through all the months and years, the script outputs the final cumulative connection time to the report file, along with a message "Total connection time: X minutes".
Sample Inputs and Outputs:
Input: A system log file containing the following entries:
Jan 2020: Connection from 192.168.1.100 for 10 minutes
Feb 2020: Connection from 192.168.1.100 for 20 minutes
Mar 2020: Connection from 192.168.1.100 for 30 minutes
Apr 2020: Connection from 192.168.1.100 for 15 minutes
Output: A report file with the following content:
Jan 2020: 10 minutes
Feb 2020: 20 minutes
Mar 2020: 30 minutes
Apr 2020: 15 minutes
Total connection time: 75 minutes
Conclusion:
The script provided in this article can be useful for network administrators or system administrators to quickly and easily analyze a log file and get an overview of the cumulative connection time for each month and year. It can be easily modified to suit the specific requirements of a particular system or log format. Additionally, this script is a basic example and can be extended in many ways to include more advanced filtering or computation. For example, to filter only certain IP addresses, usernames or timestamps, etc.
Similar Reads
How to Create Virtual Network in Azure using PowerShell ? Microsoft Azure provides various resources and services to provide us with a scalable environment for development and testing processes. Azure provides a virtual machine service, which allows us to leverage the resources virtually. We may have more than one virtual machine as a resource, according t
9 min read
How to Run PowerShell Script From CMD Executing PowerShell scripts from the Command Prompt (CMD) is a common task for developers and system administrators, essential for automation and integration. In this blog post, weâll guide you through the steps to run a PowerShell script using CMD, covering key commands and their functions to help
4 min read
How to Create a Shell Script in linux Shell is an interface of the operating system. It accepts commands from users and interprets them to the operating system. If you want to run a bunch of commands together, you can do so by creating a shell script. Shell scripts are very useful if you need to do a task routinely, like taking a backup
7 min read
Microsoft Azure - Using PowerShell Core on Linux PowerShell Core on Linux is now integrated into Azure Cloud Shell. PowerShell used to be only available on Windows. Now, with PowerShell Core, it is cross-platform and runs on macOS and lots of Linux distributions like Ubuntu and Red Hat on ARM-Based platforms and even on Windows. This allows you to
3 min read
How to Create a Batch File in Windows? A batch file is a straightforward text document in any version of Windows with a set of instructions meant to be run by Windows' command-line interpreter (CMD) by maintaining all the functions. Batch files are a great tool for streamlining internally configured processes, automating tedious jobs, an
5 min read
How to Create a File in CMD Creating a file using Windows GUI is very easy, but did you know that you can also create files using Windows CMD? Now, this article will walk you through the steps to create a file using CMD, ensuring that you can efficiently manage your files directly from the command prompt.How to Create a File i
4 min read
How to Create a File in the Linux Using the Terminal? In this article, we will learn to create a file in the Linux/Unix system using the terminal. In the Linux/Unix system, there are the following ways available to creating files. Using the touch commandUsing the cat commandUsing redirection operatorUsing the echo commandUsing the heredocUsing the dd c
4 min read
How to find the file properties using powershell PowerShell is a modern command shell that includes the best features of different shells . Unlike the other shells which only accept and return the text but in PowerShell it is different it returns the object in the .NET objects. In this article we will find out certain commands to find the file pro
2 min read
How to Install PowerShell in Linux? PowerShell is well-known among Windows administrators. It is a.NET-based task-based command-line shell and scripting language. You can quickly automate tasks for operating system maintenance and much more with PowerShell. PowerShell was once only available for Microsoft Windows. This admin tool, on
2 min read
Microsoft Azure - Create Resource Group with Tags using PowerShell Pre-requisite: Microsoft Azure: Resource Tagging and Best Practices In this article, I am going to show you how to create resource groups with tags using PowerShell in Microsoft Azure from Azure Cloud Shell in simple easy steps. Let's get started. Prerequisite: Contributor Access on Subscription to
1 min read