Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
368 changes: 367 additions & 1 deletion .bashrc
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ alias freshclam='sudo freshclam'
alias vi='nvim'
alias svi='sudo vi'
alias vis='nvim "+set si"'

alias fullupdate="sudo apt update && sudo apt upgrade"

# Change directory aliases
alias home='cd ~'
Expand Down Expand Up @@ -260,6 +260,359 @@ alias docker-clean=' \
#######################################################
# SPECIAL FUNCTIONS
#######################################################


#sytemd, Sysvinit, Openrc or runit identifier
system_init(){
if command -v systemctl &>/dev/null; then
INIT_SYSTEM="systemd"
INIT_SYSTEM_RESTART="sudo systemctl restart"
INIT_SYSTEM_STOP="sudo systemctl stop"
INIT_SYSTEM_START="sudo systemctl start"
INIT_SYSTEM_STATUS="sudo systemctl status"
elif [ -d /etc/init.d/ ] && [ "$(ls -A /etc/init.d/)" ]; then
INIT_SYSTEM="SysVinit"
INIT_SYSTEM_DEFAULT="sudo service"
elif command -v rc-service &>/dev/null; then
INIT_SYSTEM="OpenRC"
INIT_SYSTEM_DEFAULT="sudo rc-service"
elif command -v sv &>/dev/null; then
INIT_SYSTEM="runit"
INIT_SYSTEM_RESTART="sudo sv restart"
INIT_SYSTEM_START="sudo sv start"
INIT_SYSTEM_STOP="sudo sv stop"
INIT_SYSTEM_STATUS="sudo sv status"
else
INIT_SYSTEM="Unknown"
echo -e "[error] system init not found"
fi
}

if [ -z "$INIT_SYSTEM" ] || [ INIT_SYSTEM == "Unknown" ]; then
system_init
fi

export INIT_SYSTEM
export INIT_SYSTEM_START
export INIT_SYSTEM_STOP
export INIT_SYSTEM_STATUS
export INIT_SYSTEM_RESTART

export INIT_SYSTEM_DEFAULT

# simple manager to start, stop, restart and see the status of aplications
system(){
echo -e 'APPS MANAGER\n'
echo -e '(1) start\n(2) stop\n(3) restart\n(4) status'
read option
case $option in
1)
echo -e 'name of the app (ex: docker, ssh, Mysql, MongoDb...):\n'
read app
if [ "$INIT_SYSTEM" = "SysVinit" ] || [ "$INIT_SYSTEM" = "OpenRC" ]; then
$INIT_SYSTEM_DEFAULT $FIREWALL start
else
$INIT_SYSTEM_START $app
fi
;;

2)
echo -e 'name of the app (ex: docker, ssh, Mysql, MongoDb...):\n'
read app
if [ "$INIT_SYSTEM" = "SysVinit" ] || [ "$INIT_SYSTEM" = "OpenRC" ]; then
$INIT_SYSTEM_DEFAULT $FIREWALL stop
else
$INIT_SYSTEM_STOP $app
fi
;;

3)
echo -e 'name of the app (ex: docker, ssh, Mysql, MongoDb..):\n'
read app
if [ "$INIT_SYSTEM" = "SysVinit" ] || [ "$INIT_SYSTEM" = "OpenRC" ]; then
$INIT_SYSTEM_DEFAULT $FIREWALL restart
else
$INIT_SYSTEM_RESTART $app
fi
;;

4)
echo -e 'name of the app (ex: docker, ssh, Mysql, MongoDb..):\n'
read app
if [ "$INIT_SYSTEM" = "SysVinit" ] || [ "$INIT_SYSTEM" = "OpenRC" ]; then
$INIT_SYSTEM_DEFAULT $FIREWALL restart
else
$INIT_SYSTEM_STATUS $app
fi

;;

esac
}


#firewall simple configuration
firewall() {
echo -e '(1) firewall status\n(2) reset firewall\n(3) reload firewall\n(4) list apps\n(5) allow (PORT)\n(6) deny (PORT)'
read option

if command -v ufw &>/dev/null; then
FIREWALL="ufw"
elif command -v firewall-cmd &>/dev/null; then
FIREWALL="firewalld"
elif command -v iptables &>/dev/null; then
FIREWALL="iptables"
else
echo "Fail, firewall not found."
return 1
fi

case $option in
1)
echo -e "Firewall status:\n"
if [ "$FIREWALL" = "ufw" ]; then
sudo ufw status
elif [ "$FIREWALL" = "firewalld" ]; then
sudo firewall-cmd --state
else
sudo iptables -L
fi
;;
2)
echo -e "Resetting firewall:\n"
if [ "$FIREWALL" = "ufw" ]; then
sudo ufw reset
elif [ "$FIREWALL" = "firewalld" ]; then
sudo firewall-cmd --complete-reload
else
sudo iptables -F
fi
;;
3)
echo -e "Reloading firewall:\n"
if [ "$FIREWALL" = "ufw" ]; then
sudo ufw reload
elif [ "$FIREWALL" = "firewalld" ]; then
sudo firewall-cmd --reload
else
if [ "$INIT_SYSTEM" = "SysVinit" ] || [ "$INIT_SYSTEM" = "OpenRC" ]; then
$INIT_SYSTEM_DEFAULT $FIREWALL restart
else
$INIT_SYSTEM_RESTART $FIREWALL
fi
fi
;;
4)
echo -e "App list:\n"
if [ "$FIREWALL" = "ufw" ]; then
sudo ufw app list
elif [ "$FIREWALL" = "firewalld" ]; then
sudo firewall-cmd --get-services
else
sudo iptables --list
fi
;;
5)
read -p "Digit a port to allow: " allow_port
if [ "$FIREWALL" = "ufw" ]; then
sudo ufw allow "$allow_port"
elif [ "$FIREWALL" = "firewalld" ]; then
sudo firewall-cmd --permanent --add-port="$allow_port/tcp"
sudo firewall-cmd --reload
else
sudo iptables -A INPUT -p tcp --dport "$allow_port" -j ACCEPT
fi
;;
6)
read -p "Digit the port to deny:\n" deny_port
if [ "$FIREWALL" = "ufw" ]; then
sudo ufw deny "$deny_port"
elif [ "$FIREWALL" = "firewalld" ]; then
sudo firewall-cmd --permanent --remove-port="$deny_port/tcp"
sudo firewall-cmd --reload
else
sudo iptables -A INPUT -p tcp --dport "$deny_port" -j DROP
fi
;;
*)
echo "Invalid option"
;;
esac
}

# SSH simple manager
configssh() {
echo -e 'SSH MANAGER:\n(1) start\n(2) stop\n(3) restart\n(4) status\n(5) config SSH\n(6) connect to a SSH\n'
read option

echo "System init: $INIT_SYSTEM\n"

case $option in
1)
case $INIT_SYSTEM in
systemd)
echo -e "Starting SSH with systemd."
sudo systemctl start ssh
;;
SysVinit)
echo -e "Starting SSH with SysVinit."
sudo service sshd start
sudo service ssh start
;;
OpenRC)
echo -e "Starting SSH with OpenRC."
sudo rc-service sshd start
;;
runit)
echo -e "Starting SSH with runit."
sudo sv start sshd
;;
*)
echo "Unknown init system, unable to start SSH."
;;
esac
;;
2)
case $INIT_SYSTEM in
systemd)
echo -e "Stopping SSH with systemd."
sudo systemctl stop ssh
;;
SysVinit)
echo -e "Stopping SSH with SysVinit."
sudo service ssh stop
sudo service sshd stop
;;
OpenRC)
echo -e "Stopping SSH with OpenRC."
sudo rc-service sshd stop
;;
runit)
echo -e "Stopping SSH with runit."
sudo sv stop sshd
;;
*)
echo "Fail, unknown init system, unable to stop SSH."
;;
esac
;;
3)
case $INIT_SYSTEM in
systemd)
echo -e "Restarting SSH with systemd."
sudo systemctl restart ssh
;;
SysVinit)
echo -e "Restarting SSH with SysVinit."
sudo service ssh restart
;;
OpenRC)
echo -e "Restarting SSH with OpenRC."
sudo rc-service sshd restart
;;
runit)
echo -e "Restarting SSH with runit."
sudo sv restart sshd
;;
*)
echo "Unknown init system, unable to restart SSH."
;;
esac
;;
4)
case $INIT_SYSTEM in
systemd)
echo -e "Checking SSH status with systemd."
sudo systemctl status ssh
;;
SysVinit)
echo -e "Checking SSH status with SysVinit."
sudo service ssh status
;;
OpenRC)
echo -e "Checking SSH status with OpenRC."
sudo rc-service sshd status
;;
runit)
echo -e "Checking SSH status with runit."
sudo sv status sshd
;;
*)
echo "Unknown init system, unable to check SSH status."
;;
esac
;;
5)
if [ -f /etc/ssh/sshd_config ]; then
echo -e "Opening sshd_config in vi."
sudo vi /etc/ssh/sshd_config
else
echo "Failed to find the sshd_config file."
fi
;;
6)
echo -e "Enter username:"
read user
echo -e "Enter IP address:"
read ip
echo -e "Enter port:"
read port
sudo ssh $user@$ip -p $port
;;
*)
echo "Invalid option"
;;
esac
}

# show listening ports
listenports(){
echo -e '(1) list all listening ::ports \n(2) show a listening ::port \n(3) kill a process'
read option
case $option in
1)
if command -v lsof &> /dev/null; then
lsof -i
elif command -v netstat &> /dev/null; then
netstat -tuln
elif command -v ss &> /dev/null; then
ss -tuln
else
echo "Error: No suitable command found to list listening ports."
fi
;;

2)
echo -e 'digit the port'
read port
if command -v lsof &> /dev/null; then
lsof -i :$port
elif command -v netstat &> /dev/null; then
netstat -tuln | grep ":$port"
elif command -v ss &> /dev/null; then
ss -tuln | grep ":$port"
else
echo "Error: No suitable command found to show the port."
fi
;;

3)
echo -e 'digit the pid'
read pid
if kill -0 $pid &> /dev/null; then
kill -9 $pid
echo "Process $pid killed."
else
echo "Error: Process $pid not found."
fi
;;

*)
echo "Invalid option."
;;
esac
}

# Extracts any archive(s) (if unp isn't installed)
extract() {
for archive in "$@"; do
Expand Down Expand Up @@ -631,10 +984,23 @@ function hb {
fi
}


#######################################################
# Set the ultimate amazing command prompt
#######################################################


alias bashconfig="code ~/.bashrc"
alias bashrestart="source ~/.bashrc"
alias fullupdate="sudo apt update && sudo apt upgrade"

mycommands(){
echo -e '1- firewall\n2- bashconfig\n3- bashrestart\n4- fullupdate\n5- configssh\n6- system\n7- configssh\n8- listenports \n9- morecommands(to see more)'
}
morecommands(){
echo -e '9- distribution\n10- ver\n11- apachelog\n12- apacheconfig\n13- phpconfig\n14- mysqlconfig\n15- whatismyip\n16- trim\n17- ftext\n18- extract\n19- cpg\n20- mvg\n21- mkdirg\n22- up\n23- pwdtail'
}

alias hug="hugo server -F --bind=10.0.0.97 --baseURL=http://10.0.0.97"

# Check if the shell is interactive
Expand Down