Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- # The Comprehensive Educational Guide and Research Project in Bash Scripting
- # By Michael Errington
- # Function to check internet connectivity
- check_internet() {
- ping -c 1 google.com &> /dev/null
- if [ $? -eq 0 ]; then
- echo "Internet connection is active."
- else
- echo "No internet connection."
- fi
- }
- # Function to display basic system information
- system_info() {
- echo "System Information:"
- uname -a
- echo "Available Disk Space:"
- df -h
- }
- # Function to create a new directory
- create_directory() {
- local directory_name="$1"
- # Create a new directory
- mkdir "$directory_name"
- echo "Directory '$directory_name' created."
- }
- # Function to search for a file
- search_file() {
- local file_name="$1"
- # Search for the file in the home directory
- find ~/ -name "$file_name"
- echo "File search complete."
- }
- # Function to connect to a website using curl
- connect_to_website() {
- local website_url="$1"
- local curl_output
- # Use curl to connect to the website and save the content to a variable
- curl_output=$(curl -s "$website_url")
- # Check if curl was successful (HTTP status code 200)
- if [ $? -eq 0 ]; then
- echo "Curl connection successful."
- return 0 # Success
- else
- echo "Curl connection failed."
- return 1 # Failure
- fi
- }
- # Function to download a resource using wget
- download_resource() {
- local resource_url="$1"
- # Use wget to download the resource to a specified directory
- wget -P ./downloads/ "$resource_url"
- # Check if wget was successful
- if [ $? -eq 0 ]; then
- echo "Wget download successful."
- return 0 # Success
- else
- echo "Wget download failed."
- return 1 # Failure
- fi
- }
- # Function to parse HTML content and extract specific information
- parse_html() {
- local html_content="$1"
- # Use grep or other tools to extract relevant information from HTML
- # Example: Extracting all links from HTML
- links=$(echo "$html_content" | grep -o '<a [^>]\+>' | grep -o 'href="[^\"]\+"' | sed 's/href="//;s/"//')
- echo "Extracted links: $links"
- }
- # Function to clean up temporary files
- cleanup() {
- local temp_file="$1"
- # Remove the temporary file
- rm "$temp_file"
- }
- # Function to interact with a REST API using cURL
- rest_api_interaction() {
- local api_url="$1"
- # Use cURL to make a GET request to the API
- api_response=$(curl -s "$api_url")
- echo "API Response:"
- echo "$api_response"
- }
- # Function to list contents of a directory
- list_directory() {
- local directory="$1"
- # List contents of the specified directory
- ls -l "$directory"
- echo "Directory listing complete."
- }
- # Function to create and edit a text file
- create_and_edit_file() {
- local file_name="$1"
- # Create a new text file and open it in a text editor
- touch "$file_name" && nano "$file_name"
- echo "Text file '$file_name' created and edited."
- }
- # Function to count the number of lines in a file
- count_lines() {
- local file_name="$1"
- # Use wc to count the number of lines in the file
- lines=$(wc -l < "$file_name")
- echo "Number of lines in '$file_name': $lines"
- }
- # Function to rename a file
- rename_file() {
- local old_name="$1"
- local new_name="$2"
- # Rename the file
- mv "$old_name" "$new_name"
- echo "File '$old_name' renamed to '$new_name'"
- }
- # Function to create a compressed archive of files
- create_archive() {
- local archive_name="$1"
- local files_to_archive="$2"
- # Create a compressed archive of specified files
- tar -czf "$archive_name" $files_to_archive
- echo "Archive '$archive_name' created."
- }
- # Function to display the current date and time
- display_date_time() {
- date
- }
- # Function to self-destruct the script
- self_destruct() {
- echo "Initiating self-destruct sequence..."
- rm -- "$0"
- echo "Self-destruct complete. Script terminated."
- exit 0
- }
- # Beginner Functions:
- # Function to check available disk space
- check_disk_space() {
- echo "Available Disk Space:"
- df -h
- echo "Disk space check complete."
- }
- # Function to list all installed packages
- list_installed_packages() {
- echo "Installed Packages:"
- dpkg --list
- echo "Package listing complete."
- }
- # Function to display running processes
- list_processes() {
- echo "Running Processes:"
- ps aux
- echo "Process listing complete."
- }
- # Function to create a backup of a file
- create_backup() {
- local file_name="$1"
- local backup_name="$file_name.backup"
- # Copy the file to create a backup
- cp "$file_name" "$backup_name"
- echo "Backup created: $backup_name"
- }
- # Function to display system uptime
- display_uptime() {
- echo "System Uptime:"
- uptime
- echo "Uptime information complete."
- }
- # Function to list available network interfaces
- list_network_interfaces() {
- echo "Available Network Interfaces:"
- ifconfig -a
- echo "Network interfaces listing complete."
- }
- # Function to display file permissions
- display_file_permissions() {
- local file_name="$1"
- # Display permissions of the specified file
- ls -l "$file_name" | awk '{print $1}'
- echo "File permissions displayed."
- }
- # Function to extract a tar archive
- extract_archive() {
- local archive_name="$1"
- # Extract contents of the specified archive
- tar -xzf "$archive_name" -C ./extracted_contents
- echo "Archive '$archive_name' extracted."
- }
- # Function to check system memory usage
- check_memory_usage() {
- echo "System Memory Usage:"
- free -h
- echo "Memory usage check complete."
- }
- # Advanced Functions:
- # Function to analyze network connections
- analyze_network_connections() {
- echo "Network Connections Analysis:"
- netstat -tunap
- echo "Network connections analysis complete."
- }
- # Function to monitor system resource usage continuously
- monitor_resources_continuous() {
- echo "Monitoring system resource usage continuously. Press Ctrl+C to stop."
- top
- }
- # Function to find and kill a specific process
- kill_process() {
- local process_name="$1"
- # Find the process ID (PID) using grep and awk
- local process_pid=$(ps aux | grep "$process_name" | awk '{print $2}')
- # Kill the process
- kill -9 "$process_pid"
- echo "Process '$process_name' (PID: $process_pid) killed."
- }
- # Function to analyze system logs
- analyze_system_logs() {
- echo "System Logs Analysis:"
- tail /var/log/syslog
- echo "System logs analysis complete."
- }
- # Function to create a symbolic link
- create_symbolic_link() {
- local target_file="$1"
- local link_name="$2"
- # Create a symbolic link
- ln -s "$target_file" "$link_name"
- echo "Symbolic link created: $link_name -> $target_file"
- }
- # Research Project: Advanced Bash Scripting for Cybersecurity
- echo "Welcome to the research project section, where we explore advanced Bash scripting in the realm of cybersecurity. I'm Michael Errington, and together, we'll delve into powerful scripting techniques."
- # Function to analyze system users
- analyze_system_users() {
- echo "System Users Analysis:"
- cut -d: -f1 /etc/passwd
- echo "System users analysis complete."
- }
- # Function to display firewall rules
- display_firewall_rules() {
- echo "Firewall Rules:"
- iptables -L
- echo "Firewall rules display complete."
- }
- # Function to check open ports
- check_open_ports() {
- echo "Open Ports Check:"
- ss -tunl
- echo "Open ports check complete."
- }
- # Function to perform a security audit
- security_audit() {
- echo "Security Audit:"
- Lynis
- echo "Security audit complete."
- }
- # Function to scan for open vulnerabilities
- scan_for_vulnerabilities() {
- echo "Vulnerability Scanning:"
- OpenVAS
- echo "Vulnerability scanning complete."
- }
- # Function to encrypt a file
- encrypt_file() {
- local file_name="$1"
- # Use GPG to encrypt the file
- gpg -c "$file_name"
- echo "File '$file_name' encrypted."
- }
- # Function to decrypt a file
- decrypt_file() {
- local encrypted_file="$1"
- # Use GPG to decrypt the file
- gpg -d "$encrypted_file" > "${encrypted_file%.gpg}"
- echo "File '$encrypted_file' decrypted."
- }
- # Educational Additions:
- echo "Let's expand our knowledge with additional educational functions."
- # Function to display kernel information
- display_kernel_info() {
- echo "Kernel Information:"
- uname -r
- echo "Kernel information displayed."
- }
- # Function to analyze disk usage
- analyze_disk_usage() {
- echo "Disk Usage Analysis:"
- du -h --max-depth=1 /
- echo "Disk usage analysis complete."
- }
- # Function to display active SSH sessions
- display_active_ssh_sessions() {
- echo "Active SSH Sessions:"
- who
- echo "Active SSH sessions displayed."
- }
- # Function to display system temperature
- display_system_temperature() {
- echo "System Temperature:"
- sensors
- echo "System temperature displayed."
- }
- # Function to check for available software updates
- check_software_updates() {
- echo "Available Software Updates:"
- sudo apt list --upgradable
- echo "Software updates check complete."
- }
- # Beginner Functions (Additional):
- # Function to list USB devices
- list_usb_devices() {
- echo "List of USB Devices:"
- lsusb
- echo "USB device listing complete."
- }
- # Function to display system information using uname
- display_system_info_uname() {
- echo "System Information (uname):"
- uname -a
- echo "System information displayed using uname."
- }
- # Function to create a text file and add content
- create_and_add_content() {
- local file_name="$1"
- local content="$2"
- # Create a new text file and add content
- echo "$content" > "$file_name"
- echo "Text file '$file_name' created and content added."
- }
- # Function to display system information using hostnamectl
- display_system_info_hostnamectl() {
- echo "System Information (hostnamectl):"
- hostnamectl
- echo "System information displayed using hostnamectl."
- }
- # Function to display environment variables
- display_environment_variables() {
- echo "Environment Variables:"
- printenv
- echo "Environment variables displayed."
- }
- # Advanced Functions (Additional):
- # Function to check for rootkits
- check_for_rootkits() {
- echo "Checking for Rootkits:"
- rkhunter --check
- echo "Rootkit check complete."
- }
- # Function to perform a port scan
- perform_port_scan() {
- local target_ip="$1"
- echo "Performing Port Scan on $target_ip:"
- nmap "$target_ip"
- echo "Port scan complete."
- }
- # Function to analyze network traffic
- analyze_network_traffic() {
- echo "Network Traffic Analysis:"
- tcpdump -i any
- echo "Network traffic analysis complete. Press Ctrl+C to stop."
- }
- # Function to check for listening services
- check_listening_services() {
- echo "Checking for Listening Services:"
- netstat -tulpn
- echo "Listening services check complete."
- }
- # Function to backup and compress a directory
- backup_and_compress_directory() {
- local directory_path="$1"
- local backup_name="backup_$(date +'%Y%m%d').tar.gz"
- # Create a compressed archive of the specified directory
- tar -czf "$backup_name" "$directory_path"
- echo "Backup and compression of '$directory_path' complete. Archive: $backup_name"
- }
- # Concluding Thoughts:
- echo "Our journey concludes with a comprehensive understanding of Bash scripting, ranging from fundamental tasks to advanced cybersecurity applications. Remember, the power of scripting lies in automation, and as you continue your exploration, you'll discover endless possibilities."
- echo "Thank you for joining me on this educational and research-driven adventure. Keep scripting, keep exploring, and may your cybersecurity endeavors be both exciting and impactful."
- # Self-destruct the script
- self_destruct
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement