Modified TOC, Added Bash Cheatsheet

This commit is contained in:
Jun Wei Woon 2020-07-06 17:53:39 +08:00
parent 804fee007f
commit 31c55096be
6 changed files with 215 additions and 31 deletions

View File

@ -9,7 +9,7 @@ hideToc: false
enableToc: true enableToc: true
enableTocContent: true enableTocContent: true
tocPosition: inner tocPosition: inner
tocLevels: ["h2", "h3", "h4"] tocLevels: ["h1", "h2", "h3"]
tags: tags:
- -
series: series:

View File

@ -33,9 +33,9 @@ googleAnalytics = ""
lineNumbersInTable = true lineNumbersInTable = true
noClasses = false noClasses = false
[markup.tableOfContents] [markup.tableOfContents]
endLevel = 4 endLevel = 3
ordered = false ordered = false
startLevel = 2 startLevel = 1
[outputs] [outputs]
home = ["HTML", "RSS", "SearchIndex"] home = ["HTML", "RSS", "SearchIndex"]

View File

@ -0,0 +1,187 @@
---
author: "Devoalda"
authorEmoji: 🐺
title: "Bash Cheatsheet"
date: 2020-07-06T16:47:03+08:00
description: Simple Bash Cheatsheet
draft: false
hideToc: false
enableToc: true
enableTocContent: true
tocPosition: inner
tocLevels: ["h1", "h2", "h3"]
tags:
- bash
- linux
- script
- cheetsheet
series:
- Bash
categories:
- bash
- script
image:
---
# Introduction
This is a simple bash script cheatsheet.
# Variables
## Define variables
```bash
abc=123
hello=321
qwerty=asdf
```
### Assign output of command to variable
``` bash
music=$(mpc)
```
### Argument
```bash
word=$1
```
### Argument and default value
``` bash
words=${1-hello} # If variable not set or null, use default.
FOO=${VARIABLE:=default} # If variable not set or null, set it to default.
```
{{< alert theme="info" dir="ltr" >}}
Ensure no spaces between variable name and value
{{< /alert >}}
## View variable content
```bash
$ echo $abc
# outputs: 123
$ echo "$(echo "upg")"
# outputs: upg
$ echo '$(echo "upg")'
# outputs: $(echo "upg")
```
# Loops
## For loop
``` bash
for i in {1..5}
do
echo "Hello $i"
done
```
### {START..END..INCREMENT}
``` bash
for i in {0..10..2}
do
echo "Hello $i"
done
```
### Files
``` bash
for file in $HOME
do
echo $file
done
```
## While loop
``` bash
while [ condition ]
do
echo "hello"
done
```
### Infinite while loop
``` bash
while true
do
echo "hello"
done
```
{{< alert theme="info" dir="ltr" >}}
Hit CTRL-C to stop the loop
{{< /alert >}}
### Read File
``` bash
#!/usr/bin/env bash
FILE=${1-hello.txt}
while read line
do
# use $line variable to process line
echo $line
done < "$FILE"
```
# If Else Loop
``` bash
if [ condition ]; then
echo 1
elif [ condition2 ]; then
echo 2
else
echo 3
fi
```
# Conditions
| Operator | Description |
|----------|----------------------|
| -eq | Equal |
| -lt | Less than |
| -ge | Greater than |
| == | 2 Strings equal |
| != | 2 Strings not equal |
| ! | Statement is false |
| -d | Directory is present |
| -e | File is present |
## Condition Usage Examples
``` bash
if [[ "$1" == "bash" && "$2" = "shell" ]]; then
echo "$1 $2"
elif [ "$1" == "bash" ]; then
echo "$1"
elif [ -e "$HOME/hello.txt" ]
echo "File Present"
elif [ ! -d "$HOME/Documents" ]
echo "Directory NOT Present"
else
echo Condition
fi
```
# Case
``` bash
word=$1
case "$word" in
hello)
echo hello
;;
bye)
echo bye
;;
*)
echo Universal
;;
esac
```
# Functions
``` bash
function fun(){
echo fun
}
```
{{< expand "Credits/References" >}}
[stackoverflow1](https://stackoverflow.com/questions/6697753/difference-between-single-and-double-quotes-in-bash)
[stackoverflow2](https://stackoverflow.com/questions/2013547/assigning-default-values-to-shell-variables-with-a-single-command-in-bash)
{{< /expand >}}

View File

@ -9,7 +9,7 @@ hideToc: false
enableToc: true enableToc: true
enableTocContent: true enableTocContent: true
tocPosition: inner tocPosition: inner
tocLevels: ["h2", "h3", "h4"] tocLevels: ["h1", "h2", "h3"]
tags: tags:
- python - python
- automation - automation
@ -22,25 +22,25 @@ categories:
image: images/postImages/Broom.png image: images/postImages/Broom.png
--- ---
## Introduction # Introduction
This is a script to automate cleanups in any directory. It will move files into folders according to the file extension. This is a script to automate cleanups in any directory. It will move files into folders according to the file extension.
### Why? ## Why?
A clean home/project directory allows us to be able to work productively, without spending much time searching for the particular document/file we need. A clean home/project directory allows us to be able to work productively, without spending much time searching for the particular document/file we need.
This is a simple script to help you clean your directory in seconds This is a simple script to help you clean your directory in seconds
### Caution ## Caution
{{< alert theme="danger" dir="ltr" >}} {{< alert theme="danger" dir="ltr" >}}
__DO NOT__ run the script on any directory containing system/important files. __DO NOT__ run the script on any directory containing system/important files.
{{< /alert >}} {{< /alert >}}
## Dependencies # Dependencies
* [Python3.8](https://www.python.org/) * [Python3.8](https://www.python.org/)
## Code ## Code
### Imports ## Imports
This is a python script, we will be using modules [os](https://docs.python.org/3/library/shutil.html) and [shutil](https://docs.python.org/3/library/shutil.html). First of all, create a new script called script.py, and import the modules required for this This is a python script, we will be using modules [os](https://docs.python.org/3/library/shutil.html) and [shutil](https://docs.python.org/3/library/shutil.html). First of all, create a new script called script.py, and import the modules required for this
```python ```python
#!/usr/bin/env python #!/usr/bin/env python
@ -56,7 +56,7 @@ Without the shebang, we will need to use the full command
```bash ```bash
python3 script.py python3 script.py
``` ```
### Defining Main ## Defining Main
This step is __optional__, although it will make the code look cleaner This step is __optional__, although it will make the code look cleaner
```python ```python
def main(): def main():
@ -68,10 +68,10 @@ if __name__ == "__main__":
This is how I usually start my code, you will be able to run your code now with the command This is how I usually start my code, you will be able to run your code now with the command
```bash ```bash
./script.py ./script.py
# Output: Hello,World! Output: Hello,World!
``` ```
### Defining a dictionary ## Defining a dictionary
A dictionary has a key, value pair, similar to a JSON file structure. In this script, we will create a dictionary of File type(Key) and File Extensions (Value). A dictionary has a key, value pair, similar to a JSON file structure. In this script, we will create a dictionary of File type(Key) and File Extensions (Value).
Insert the following ___above___ the line ```def main():``` Insert the following ___above___ the line ```def main():```
@ -89,7 +89,7 @@ DIRECTORIES_DICTIONARY = {
``` ```
You are able to expand this dictionary by adding your own file types and file extensions in a similar structure. You are able to expand this dictionary by adding your own file types and file extensions in a similar structure.
### Creating folders based on directory key ## Creating folders based on directory key
In ```def main():```, add the following lines In ```def main():```, add the following lines
```python ```python
DIRECTORIES= [] DIRECTORIES= []
@ -103,7 +103,7 @@ for dir in DIRECTORIES_DICTIONARY:
_Note_ the indentation of the lines! _Note_ the indentation of the lines!
When you run the script, you will see new folders being created in your directory When you run the script, you will see new folders being created in your directory
### Moving file to correct directories ## Moving file to correct directories
Append the following lines to your main function Append the following lines to your main function
```python ```python
file_moved_counter = 0 file_moved_counter = 0
@ -137,20 +137,20 @@ These lines will move files to their folder based on their file extensions, and
In this script, I've used both modules os and shutil to move files to show the difference in syntax for each of the modules. In this script, I've used both modules os and shutil to move files to show the difference in syntax for each of the modules.
### Running the script ## Running the script
```bash ```bash
./script.py ./script.py
``` ```
To test the script, you should add/create files with any extensions in the same directory as your script. Upon running the script, you should see all your files moved and categorized according to their file extensions. To test the script, you should add/create files with any extensions in the same directory as your script. Upon running the script, you should see all your files moved and categorized according to their file extensions.
## Tips # Tips
* I usually create a project folder for any scripts that I create, and run the script in the folder. This prevents any unwanted modifications to any directory. * I usually create a project folder for any scripts that I create, and run the script in the folder. This prevents any unwanted modifications to any directory.
* Note that indentations in a python script is very __important__. * Note that indentations in a python script is very __important__.
* This script is useful in certain directories, like Documents or Downloads, as it categorizes those files accordingly and cleans the directory. * This script is useful in certain directories, like Documents or Downloads, as it categorizes those files accordingly and cleans the directory.
## Summary # Summary
Thats it! You have yourself a cleanup script that you can use to clean any directory you want! The full code is below, do take a look if you are facing any problems! Thats it! You have yourself a cleanup script that you can use to clean any directory you want! The full code is below, do take a look if you are facing any problems!
### Full Code ## Full Code
{{< expand "Python Code" >}} {{< expand "Python Code" >}}
``` python ``` python
#!/usr/bin/env python #!/usr/bin/env python
@ -212,7 +212,7 @@ if __name__ == "__main__":
``` ```
{{< /expand >}} {{< /expand >}}
## Credits # Credits
{{< expand "Credits" >}} {{< expand "Credits" >}}
<a target="_blank" href="https://icons8.com/icons/set/broom">Broom icon</a> icon by <a target="_blank" href="https://icons8.com">Icons8</a> <a target="_blank" href="https://icons8.com/icons/set/broom">Broom icon</a> icon by <a target="_blank" href="https://icons8.com">Icons8</a>

View File

@ -21,16 +21,14 @@ series:
- Linux Programs - Linux Programs
--- ---
# Fail2ban Setup guide # Introduction
## Introduction
Fail2ban scans log files (e.g. /var/log/apache/error_log) and bans IPs that show the malicious signs -- too many password failures, seeking for exploits, etc. Generally Fail2Ban is then used to update firewall rules to reject the IP addresses for a specified amount of time, although any arbitrary other action (e.g. sending an email) could also be configured. Out of the box Fail2Ban comes with filters for various services (apache, courier, ssh, etc). Fail2ban scans log files (e.g. /var/log/apache/error_log) and bans IPs that show the malicious signs -- too many password failures, seeking for exploits, etc. Generally Fail2Ban is then used to update firewall rules to reject the IP addresses for a specified amount of time, although any arbitrary other action (e.g. sending an email) could also be configured. Out of the box Fail2Ban comes with filters for various services (apache, courier, ssh, etc).
Fail2Ban is able to reduce the rate of incorrect authentications attempts however it cannot eliminate the risk that weak authentication presents. Configure services to use only two factor or public/private authentication mechanisms if you really want to protect services. Fail2Ban is able to reduce the rate of incorrect authentications attempts however it cannot eliminate the risk that weak authentication presents. Configure services to use only two factor or public/private authentication mechanisms if you really want to protect services.
I have Fail2Ban set up on my personal workstation to help protect my machine from bruteforce attacks. Fail2Ban is a free and open source software that helps in securing your Linux server against malicious logins. If you have set up an SSH server on your machine, you might find a huge number of IPs trying to login to your machine via SSH, hence Fail2Ban will be able to protect your system from unwanted malicious logins. I have Fail2Ban set up on my personal workstation to help protect my machine from bruteforce attacks. Fail2Ban is a free and open source software that helps in securing your Linux server against malicious logins. If you have set up an SSH server on your machine, you might find a huge number of IPs trying to login to your machine via SSH, hence Fail2Ban will be able to protect your system from unwanted malicious logins.
## Installation # Installation
Install fail2ban on machine Install fail2ban on machine
```bash ```bash
yay -S fail2ban yay -S fail2ban

View File

@ -9,7 +9,7 @@ hideToc: false
enableToc: true enableToc: true
enableTocContent: true enableTocContent: true
tocPosition: inner tocPosition: inner
tocLevels: ["h2", "h3", "h4"] tocLevels: ["h1", "h2", "h3"]
image: images/postImages/SSH.png image: images/postImages/SSH.png
tags: tags:
- linux - linux
@ -22,9 +22,8 @@ categories:
- linux - linux
- program - program
--- ---
# SSH Setup guide
## Introduction # Introduction
SSH is an important tool as a system administrator. It allows for remote connection to your machine through a client, and the ability to control your machine from wherever you are. SSH is an important tool as a system administrator. It allows for remote connection to your machine through a client, and the ability to control your machine from wherever you are.
However, malicious attackers are able to do a brute force attack on your machine via SSH. Therefore, this tutorial will not only setup your ssh server, it will strengthen the server to help against unwanted brute force attacks. However, malicious attackers are able to do a brute force attack on your machine via SSH. Therefore, this tutorial will not only setup your ssh server, it will strengthen the server to help against unwanted brute force attacks.
@ -33,19 +32,19 @@ This tutorial will setup SSH on your machine on port 2222, instead of the defaul
You will need 2 machines to use keypair authentication You will need 2 machines to use keypair authentication
## Installation # Installation
Install openssh and ufw with your package manager on both your local and remote machines Install openssh and ufw with your package manager on both your local and remote machines
```bash ```bash
yay -S openssh ufw yay -S openssh ufw
``` ```
## Start and enable ssh service # Start and enable ssh service
```bash ```bash
sudo systemctl start sshd sudo systemctl start sshd
sudo systemctl enable sshd sudo systemctl enable sshd
``` ```
## Key generation # Key generation
Use Command Use Command
```bash ```bash
ssh-keygen -t rsa ssh-keygen -t rsa
@ -56,7 +55,7 @@ Follow the prompt to generate a key, copy key to remote machines
**REST** of the configurations below are done in the ___remote___ machine **REST** of the configurations below are done in the ___remote___ machine
## SSH Configurations # SSH Configurations
On the **remote** machine, edit sshd_config file On the **remote** machine, edit sshd_config file
```bash ```bash
sudo vim /etc/ssh/sshd_config sudo vim /etc/ssh/sshd_config
@ -95,7 +94,7 @@ Allow ssh port on the firewall, I am using non-default ports for ssh
sudo ufw limit 2222/tcp comment 'SSH Port 2222 limit' sudo ufw limit 2222/tcp comment 'SSH Port 2222 limit'
``` ```
## Connection # Connection
On your machine, connect to the remote machine with: On your machine, connect to the remote machine with:
``` ```
ssh -p 2222 <username>@<Remote IP Address> ssh -p 2222 <username>@<Remote IP Address>