This commit is contained in:
Candifloss 2024-10-26 16:42:22 +05:30
parent cb5f9a1bb6
commit 99b6680a7f
10 changed files with 142 additions and 2 deletions

View File

@ -1,3 +1,3 @@
# misc_scripts
# misc-scripts
Some scripts that I use/used for various tasks
Misc scripts I use/used for various tasks

6
dhcp-log/README.md Normal file
View File

@ -0,0 +1,6 @@
# Extract details from DHCP logs
This script extracts the ip address, mac address, and host names from the DHCP server logs on a linux system.
Since this was not tested on multiple systems, results can not be guaranteed with your log files, unless they are the the format as the `example.log` in this directory.
The regex may need adjustments.
Tested on: `Ubuntu 20.04.4`, `isc-dhcpd-4.4.1`

View File

@ -0,0 +1,38 @@
import re
# This script extracts ip, mac, and hostname from a dhcp server's log file to a csv file.
# This works only if the log files match the format in the example log file in this directory
def extract_info_from_logs(log_file_list, output_file_path):
# Regular expression to match IP address, MAC address, and hostname
regex = re.compile(
r'\b(?:DHCPACK|DHCPREQUEST|DHCPOFFER)\b.*?on\s+([\d.]+)\s+to\s+([\da-f:]+)(?:\s+\((.*?)\))?'
)
# A list of (ip, mac, host) data extracted from matched lines
extracted_data = []
for logfile in log_file_list:
with open(logfile, 'r') as logfile:
for line in logfile:
match = regex.search(line)
if match:
ip = match.group(1)
mac = match.group(2)
host = match.group(3) if match.group(3) else "Unknown"
data = ip + "," + mac + "," + host # Customizable as per requirements
if data not in extracted_data: # Ensure uniqueness
# Add other conditions here as per requirements
extracted_data.append(data) # Add extratcted data to the list
# Write the extracted data to the output file
with open(output_file_path, 'w') as output_file:
output_file.write("IP,MAC,HOST\n") # Header row
output_file.write('\n'.join(extracted_data)) # Data, as row+'\n'
print(f"Extraction complete. Data written to {output_file_path}.")
logfilelist = ['/var/log/syslog', '/var/log/syslog.1', '/var/log/dhcp.log'] # Specify the list of /path/log_file
outputfile = 'output.csv' # Specify the output file /path/name.csv
extract_info_from_logs(logfilelist, outputfile)

43
ldap_basic/README.md Normal file
View File

@ -0,0 +1,43 @@
# Basic LDAP operations
These scripts act as shortcuts for frequently used `openldap` commands or operations.
## Example commands
It is necessary to understand these basic commands
- `ldapsearch`: Search for entries in the directory
- `ldapadd`: Add entries to the directory
- `ldapmodify`: Modify entries in the directory
- `ldapvi`: A program to edit enties using your text-editor
## Common options
You will have to modify these fields in the scripts:
- `-H`: Host ip-address or url: `ldap://192.12.34.123/`, `ldaps://ldap.example.com/`
- `-D`: Bind DN: `cn=ldapadmin,dc=example,dc=com`
- `-w`: Bind password: `-wS3cretP4$$w0rd` or `-w S3cretP4$$w0rd`
- `-b`: Search base: `dc=example,dc=com`
- `-f`: File: The `.ldif` file with the `ldif` data to add or modify an entry
Usage examples:
```bash
ldapsearch -D cn=ldapadmin,dc=example,dc=com -w Y0ur4dm!nPwd -H ldap://ldap.example.com/ -b dc=example,dc=com uid=tomsawyer
```
```bash
ldapadd -D cn=ldapadmin,dc=example,dc=com -w Y0ur4dm!nPwd -H ldap://192.12.34.123/ -f testuser.ldif
```
## Script usage
[ldapsearch.sh](ldapsearch.sh): Search entry by any attribute
```bash
bash ldapsearch.sh uid=tomsawyer
```
[ldapadd.sh](ldapadd.sh): Add entries from ldif file
```bash
bash ldapadd.sh filename.ldif
```
[ldapmodify.sh](ldapmodify.sh): Modify entries with info from ldif file
```bash
bash ldapmodify.sh filename.ldif
```

1
ldap_basic/ldapadd.sh Normal file
View File

@ -0,0 +1 @@
ldapadd -D cn=ldapadmin,dc=example,dc=com -w Y0ur4dm!nPwd -H ldap://192.12.34.123/ -f $1

1
ldap_basic/ldapmodify.sh Normal file
View File

@ -0,0 +1 @@
ldapmodify -D cn=ldapadmin,dc=example,dc=com -w Y0ur4dm!nPwd -H ldap://192.12.34.123/ -f $1

1
ldap_basic/ldapsearch.sh Normal file
View File

@ -0,0 +1 @@
ldapsearch -D cn=ldapadmin,dc=example,dc=com -w Y0ur4dm!nPwd -b dc=example,dc=com $*

View File

@ -0,0 +1,3 @@
# Randomize IP and mac
Replace the ip and mac addresses in your text files with random-generated ones. Useful for sharing example log files or asking question on forums.

View File

@ -0,0 +1,23 @@
import re
import random
def generate_random_ip():
return f"{random.randint(0, 255)}.{random.randint(0, 255)}.{random.randint(0, 255)}.{random.randint(0, 255)}"
def replace_ips_in_file(file_path):
with open(file_path, 'r') as file:
content = file.read()
# Regular expression to match IP addresses
ip_pattern = r'\b(?:\d{1,3}\.){3}\d{1,3}\b'
# Replace IPs with random IPs
modified_content = re.sub(ip_pattern, lambda _: generate_random_ip(), content)
with open(file_path, 'w') as file:
file.write(modified_content)
if __name__ == "__main__":
file_path = 'dhcpcopy.log' # Change to your file path
replace_ips_in_file(file_path)
print("IP addresses replaced successfully.")

View File

@ -0,0 +1,24 @@
import re
import random
def generate_random_mac():
# Generate a random MAC address
return ':'.join(f"{random.randint(0, 255):02x}" for _ in range(6))
def replace_macs_in_file(file_path):
with open(file_path, 'r') as file:
content = file.read()
# Regular expression to match MAC addresses
mac_pattern = r'\b(?:[0-9a-fA-F]{2}:){5}[0-9a-fA-F]{2}\b'
# Replace MACs with random MACs
modified_content = re.sub(mac_pattern, lambda _: generate_random_mac(), content)
with open(file_path, 'w') as file:
file.write(modified_content)
if __name__ == "__main__":
file_path = 'dhcpcopy.log' # Change to your file path
replace_macs_in_file(file_path)
print("MAC addresses replaced successfully.")