diff --git a/README.md b/README.md index 190b2bd..57ac71c 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,3 @@ -# misc_scripts +# misc-scripts -Some scripts that I use/used for various tasks \ No newline at end of file +Misc scripts I use/used for various tasks \ No newline at end of file diff --git a/dhcp-log/README.md b/dhcp-log/README.md new file mode 100644 index 0000000..863644f --- /dev/null +++ b/dhcp-log/README.md @@ -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` \ No newline at end of file diff --git a/dhcp-log/extract-details.py b/dhcp-log/extract-details.py new file mode 100644 index 0000000..5c934b1 --- /dev/null +++ b/dhcp-log/extract-details.py @@ -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) diff --git a/ldap_basic/README.md b/ldap_basic/README.md new file mode 100644 index 0000000..2293529 --- /dev/null +++ b/ldap_basic/README.md @@ -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 +``` \ No newline at end of file diff --git a/ldap_basic/ldapadd.sh b/ldap_basic/ldapadd.sh new file mode 100644 index 0000000..48cdd3c --- /dev/null +++ b/ldap_basic/ldapadd.sh @@ -0,0 +1 @@ +ldapadd -D cn=ldapadmin,dc=example,dc=com -w Y0ur4dm!nPwd -H ldap://192.12.34.123/ -f $1 \ No newline at end of file diff --git a/ldap_basic/ldapmodify.sh b/ldap_basic/ldapmodify.sh new file mode 100644 index 0000000..43a4e5d --- /dev/null +++ b/ldap_basic/ldapmodify.sh @@ -0,0 +1 @@ +ldapmodify -D cn=ldapadmin,dc=example,dc=com -w Y0ur4dm!nPwd -H ldap://192.12.34.123/ -f $1 \ No newline at end of file diff --git a/ldap_basic/ldapsearch.sh b/ldap_basic/ldapsearch.sh new file mode 100644 index 0000000..e23f901 --- /dev/null +++ b/ldap_basic/ldapsearch.sh @@ -0,0 +1 @@ +ldapsearch -D cn=ldapadmin,dc=example,dc=com -w Y0ur4dm!nPwd -b dc=example,dc=com $* \ No newline at end of file diff --git a/randomize-ip-mac/README.md b/randomize-ip-mac/README.md new file mode 100644 index 0000000..aa659ab --- /dev/null +++ b/randomize-ip-mac/README.md @@ -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. \ No newline at end of file diff --git a/randomize-ip-mac/randomizeIP.py b/randomize-ip-mac/randomizeIP.py new file mode 100644 index 0000000..a641a47 --- /dev/null +++ b/randomize-ip-mac/randomizeIP.py @@ -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.") diff --git a/randomize-ip-mac/randomizeMac.py b/randomize-ip-mac/randomizeMac.py new file mode 100644 index 0000000..5bdab29 --- /dev/null +++ b/randomize-ip-mac/randomizeMac.py @@ -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.")