Skip to content

Instantly share code, notes, and snippets.

@askmrsinh
Last active March 30, 2020 07:32
Show Gist options
  • Save askmrsinh/159ea66c7af178ef338cf32ccc716818 to your computer and use it in GitHub Desktop.
Save askmrsinh/159ea66c7af178ef338cf32ccc716818 to your computer and use it in GitHub Desktop.
Miscellaneous scripts for various use cases.
#!/bin/bash
wget "https://www.dropbox.com/s/iif8wm4q31vtoeb/evernote.png?dl=0" -O ~/.local/share/icons/evernote.png
cat > ~/.local/share/applications/evernote-weblink.desktop <<EOL
[Desktop Entry]
Version=1.0
Encoding=UTF-8
Name=Evernote
Keywords=note;evernote;to-do
Type=Application
Categories=Accessories;Office;
Terminal=false
Exec=google-chrome "https://www.evernote.com/Home.action"
Icon=evernote.png
EOL
#!/bin/bash
echo "Show terminal color support"
echo -e "\n\033[4;31mLight Colors\033[0m \t\t\033[1;4;31mDark Colors\033[0m"
echo -e "\e[0;30;47m Black \e[0m 0;30m \t\e[1;30;40m Dark Gray \e[0m 1;30m"
echo -e "\e[0;31;47m Red \e[0m 0;31m \t\e[1;31;40m Dark Red \e[0m 1;31m"
echo -e "\e[0;32;47m Green \e[0m 0;32m \t\e[1;32;40m Dark Green \e[0m 1;32m"
echo -e "\e[0;33;47m Brown \e[0m 0;33m \t\e[1;33;40m Yellow \e[0m 1;33m"
echo -e "\e[0;34;47m Blue \e[0m 0;34m \t\e[1;34;40m Dark Blue \e[0m 1;34m"
echo -e "\e[0;35;47m Magenta \e[0m 0;35m \t\e[1;35;40m DarkMagenta\e[0m 1;35m"
echo -e "\e[0;36;47m Cyan \e[0m 0;36m \t\e[1;36;40m Dark Cyan \e[0m 1;36m"
echo -e "\e[0;37;47m LightGray\e[0m 0;37m \t\e[1;37;40m White \e[0m 1;37m"
for i in {0..255}; do
printf "\x1b[38;5;${i}mcolour${i}\x1b[0m\n"
done
#!/bin/bash
sudo apt-get update
sudo apt-get install -y software-properties-common
sudo add-apt-repository universe
sudo add-apt-repository ppa:certbot/certbot
sudo apt-get update
sudo apt-get install -y python-certbot-apache
# https://certbot.eff.org/docs/using.html#dns-plugins
# dns-<plugin>: dns-cloudflare, dns-digitalocean, dns-google, dns-linode, etc.
sudo apt-get install python3-certbot-dns-*
# Test on Staging environment (ACME v2)
# https://letsencrypt.org/docs/staging-environment/
# authenticator plugin name
# one of dns-cloudflare, dns-digitalocean, dns-google, dns-linode, etc.
dnsPlugin=''
# comma-separated list of domains
# the first domain provided will be the subject CN of the certificate, and all
# domains will be Subject Alternative Names on the certificate.
domainNames=''
# Keep credentials INI file ready for your dns-<plugin>
# Apache
# https://certbot.eff.org/lets-encrypt/ubuntubionic-apache
sudo certbot --staging \
--server https://acme-v02.api.letsencrypt.org/directory \
-a $dnsPlugin \
-i apache \
-d $domainNames
# Nginx
# https://certbot.eff.org/lets-encrypt/ubuntubionic-nginx
sudo certbot --staging \
--server https://acme-v02.api.letsencrypt.org/directory \
-a $dnsPlugin \
-i nginx \
-d $domainNames
# See currently issued certificates
sudo certbot certificates
# Revoking Certificates:
# https://letsencrypt.org/docs/revoking/
#!/bin/bash
read -r -p "Give name for configuration file: " file_name
read -r -p "File/Folder pattern for logs: " log_file_pattern
sudo cat << EOT > "/etc/logrotate.d/$file_name"
$log_file_pattern {
weekly
missingok
rotate 7
compress
notifempty
copytruncate
create 600
}
EOT
#!/bin/python2
import sys
import requests
from bs4 import BeautifulSoup
for word in sys.argv[1:]:
url = 'https://www.google.co.in/search?q=define%20' + word + '#cns=1'
response = requests.get(url, headers={"user-agent":"Mozilla/5.0(Macintosh; Intel Mac OS X 10.12; rv:49.0) Gecko/20100101 Firefox/49.0"})
html = response.content
final_soup = BeautifulSoup(html,"html5lib")
everyThing = final_soup.select("div._Jig")
for line in everyThing:
print("-",line.text)
Windows Registry Editor Version 5.00
; created by Walter Glenn
; for How-To Geek
; article: https://www.howtogeek.com/225844/how-to-make-windows-photo-viewer-your-default-image-viewer-on-windows-10/
[HKEY_CLASSES_ROOT\Applications\photoviewer.dll]
[HKEY_CLASSES_ROOT\Applications\photoviewer.dll\shell]
[HKEY_CLASSES_ROOT\Applications\photoviewer.dll\shell\open]
"MuiVerb"="@photoviewer.dll,-3043"
[HKEY_CLASSES_ROOT\Applications\photoviewer.dll\shell\open\command]
@=hex(2):25,00,53,00,79,00,73,00,74,00,65,00,6d,00,52,00,6f,00,6f,00,74,00,25,\
00,5c,00,53,00,79,00,73,00,74,00,65,00,6d,00,33,00,32,00,5c,00,72,00,75,00,\
6e,00,64,00,6c,00,6c,00,33,00,32,00,2e,00,65,00,78,00,65,00,20,00,22,00,25,\
00,50,00,72,00,6f,00,67,00,72,00,61,00,6d,00,46,00,69,00,6c,00,65,00,73,00,\
25,00,5c,00,57,00,69,00,6e,00,64,00,6f,00,77,00,73,00,20,00,50,00,68,00,6f,\
00,74,00,6f,00,20,00,56,00,69,00,65,00,77,00,65,00,72,00,5c,00,50,00,68,00,\
6f,00,74,00,6f,00,56,00,69,00,65,00,77,00,65,00,72,00,2e,00,64,00,6c,00,6c,\
00,22,00,2c,00,20,00,49,00,6d,00,61,00,67,00,65,00,56,00,69,00,65,00,77,00,\
5f,00,46,00,75,00,6c,00,6c,00,73,00,63,00,72,00,65,00,65,00,6e,00,20,00,25,\
00,31,00,00,00
[HKEY_CLASSES_ROOT\Applications\photoviewer.dll\shell\open\DropTarget]
"Clsid"="{FFE2A43C-56B9-4bf5-9A79-CC6D4285608A}"
[HKEY_CLASSES_ROOT\Applications\photoviewer.dll\shell\print]
[HKEY_CLASSES_ROOT\Applications\photoviewer.dll\shell\print\command]
@=hex(2):25,00,53,00,79,00,73,00,74,00,65,00,6d,00,52,00,6f,00,6f,00,74,00,25,\
00,5c,00,53,00,79,00,73,00,74,00,65,00,6d,00,33,00,32,00,5c,00,72,00,75,00,\
6e,00,64,00,6c,00,6c,00,33,00,32,00,2e,00,65,00,78,00,65,00,20,00,22,00,25,\
00,50,00,72,00,6f,00,67,00,72,00,61,00,6d,00,46,00,69,00,6c,00,65,00,73,00,\
25,00,5c,00,57,00,69,00,6e,00,64,00,6f,00,77,00,73,00,20,00,50,00,68,00,6f,\
00,74,00,6f,00,20,00,56,00,69,00,65,00,77,00,65,00,72,00,5c,00,50,00,68,00,\
6f,00,74,00,6f,00,56,00,69,00,65,00,77,00,65,00,72,00,2e,00,64,00,6c,00,6c,\
00,22,00,2c,00,20,00,49,00,6d,00,61,00,67,00,65,00,56,00,69,00,65,00,77,00,\
5f,00,46,00,75,00,6c,00,6c,00,73,00,63,00,72,00,65,00,65,00,6e,00,20,00,25,\
00,31,00,00,00
[HKEY_CLASSES_ROOT\Applications\photoviewer.dll\shell\print\DropTarget]
"Clsid"="{60fd46de-f830-4894-a628-6fa81bc0190d}"
#!/bin/sh
case "$PATH" in
(*[!:]:) PATH="$PATH:" ;;
esac
set -f; IFS=:
for dir in $PATH; do
set +f
[ -z "$dir" ] && dir="."
for file in "$dir"/*; do
if [ -x "$file" ] && ! [ -d "$file" ]; then
printf '%s = %s\n' "${file##*/}" "$file"
fi
done
done
#!/bin/sh
if [ -z "$1" ]; then
echo
echo "usage: $0 <interface>"
echo " e.g. $0 eth0 eth1 wlan0 wlo1"
echo
exit
else
for i in "$@"; do
if [ ! -f "/sys/class/net/$i/statistics/rx_bytes" ]; then
echo -e "\e[31mFile /sys/class/net/$i/statistics/rx_bytes doesn't exists!\e[0m"
exit
fi
done
fi
rxtxThreshold=3
retries=5
interval=10
retriesCount=$retries
while true
do
rx_t1=0
rx_t2=0
tx_t1=0
tx_t2=0
rx_all_t1=0
rx_all_t2=0
tx_all_t1=0
tx_all_t2=0
echo
echo "#-------------------------------------------------------------------#"
echo "retriesCount: $retriesCount"
for i in "$@"; do
echo
echo "$i: "
rxTemp=$(cat "/sys/class/net/$i/statistics/rx_bytes")
txTemp=$(cat "/sys/class/net/$i/statistics/tx_bytes")
echo -e " rxTemp:\t$rxTemp \t txTemp:\t$txTemp"
rx_t1=$((rxTemp + rx_t1))
tx_t1=$((txTemp + tx_t1))
rx_all_t1=$((rx_all_t1 + rx_t1))
tx_all_t1=$((tx_all_t1 + tx_t1))
done
#echo -e "rx_t1:\t$rx_t1"
#echo -e "tx_t1:\t$tx_t1"
sleep 1
for i in "$@"; do
echo
echo "$i: "
rxTemp=$(cat "/sys/class/net/$i/statistics/rx_bytes")
txTemp=$(cat "/sys/class/net/$i/statistics/tx_bytes")
echo -e " rxTemp:\t$rxTemp \t txTemp:\t$txTemp"
rx_t2=$((rxTemp + rx_t2))
tx_t2=$((txTemp + tx_t2))
rx_all_t2=$((rx_all_t2 + rx_t2))
tx_all_t2=$((tx_all_t2 + tx_t2))
done
#echo -e "rx_t2:\t$rx_t2"
#echo -e "tx_t2:\t$tx_t2"
txBPS=$((tx_all_t2 - tx_all_t1))
rxBPS=$((rx_all_t2 - rx_all_t1))
txKBPS=$((txBPS / 1024))
rxKBPS=$((rxBPS / 1024))
echo ""
echo -e "tx: $txKBPS kb/s rx: $rxKBPS kb/s"
echo ""
rxtxTotal=$((txKBPS + rxKBPS))
echo
echo -e "\e[34mrxtxTotal: $rxtxTotal kb/s\e[0m"
if [ $rxtxTotal -lt $rxtxThreshold ] && [ $retriesCount -le 0 ]; then
echo "DO SOMETHING"
break
elif [ $rxtxTotal -lt $rxtxThreshold ]; then
retriesCount=$((retriesCount - 1))
sleep "$interval"
else
retriesCount=$retries
sleep "$interval"
fi
done
  1. remove an old SSH Known Host
    ssh-keygen -f "/home/$USER/.ssh/known_hosts" -R 192.168.0.1

    • makes a backup of ~/.ssh/known_hosts to ~/.ssh/known_hosts.old
    • removes the entry for 192.168.0.1
  2. SSL certificates that will work with Chrome

    openssl req \
      -newkey rsa:2048 \
      -x509 \
      -nodes \
      -keyout uhttpd.key \
      -new \
      -out uhttpd.crt \
      -subj '/O=Gargoyle Router Management Utility/CN=Gargoyle' \
      -reqexts SAN \
      -extensions SAN \
      -config <(cat /etc/ssl/openssl.cnf \
        <(printf '[SAN]\nsubjectAltName=DNS:gargoyle.lan,IP:192.168.1.1')) \
      -sha256 \
      -days 3650
    
  3. See X509 certificate in text openssl x509 -noout -text -in <filename>

  4. List imported RPM keys rpm -q gpg-pubkey --qf '%{NAME}-%{VERSION}-%{RELEASE}\t%{SUMMARY}\n'

  5. CSV to horizontally spaced list, sets of 10

    csv=""
    list=$(echo "$csv" | awk '{for (i=1;i<=NF;i++) if (!x[$i]++) printf("%s%s",$i,FS)}{printf("\n")}')
    echo "$list" | tr ' ' '\n' | awk '1;!(NR%10){print "";}'
    
  6. Graphics Driver in use lspci -nnk | grep -i vga -A3

portFree (){
for port in "$@"
do
echo "$HOSTNAME:$port"
lsof -i:$port
if [[ $? -eq 0 ]]; then
echo "Killing process on $HOSTNAME:$port"
kill $(lsof -t -i:$port)
else
echo "No action taken."
fi
done
}

Some Random Scripts

@echo off
::Remove Duplicate Drives From Navigation Pane of Windows Explorer
::Must be run as Administrator
REG QUERY HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\Desktop\NameSpace\DelegateFolders\{F5FB2C77-0E2F-4A16-A381-3E560C68BC83} /s
IF %ERRORLEVEL% EQU 0 (
REG COPY HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\Desktop\NameSpace\DelegateFolders\{F5FB2C77-0E2F-4A16-A381-3E560C68BC83} HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\Desktop\NameSpace\DelegateFolders\-{F5FB2C77-0E2F-4A16-A381-3E560C68BC83} /s
REG DELETE HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\Desktop\NameSpace\DelegateFolders\{F5FB2C77-0E2F-4A16-A381-3E560C68BC83} /f
)
REG QUERY HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Explorer\Desktop\NameSpace\DelegateFolders\{F5FB2C77-0E2F-4A16-A381-3E560C68BC83} /s
IF %ERRORLEVEL% EQU 0 (
REG COPY HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Explorer\Desktop\NameSpace\DelegateFolders\{F5FB2C77-0E2F-4A16-A381-3E560C68BC83} HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Explorer\Desktop\NameSpace\DelegateFolders\-{F5FB2C77-0E2F-4A16-A381-3E560C68BC83} /s
REG DELETE HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Explorer\Desktop\NameSpace\DelegateFolders\{F5FB2C77-0E2F-4A16-A381-3E560C68BC83} /f
)
#!/bin/bash
removeUnwantedShortcuts() {
rm -rf ~/.local/share/applications/java-*-jconsole.desktop &>/dev/null
rm -rf ~/.local/share/applications/java-*-policytool.desktop &>/dev/null
cp /usr/share/applications/java-*-jconsole.desktop ~/.local/share/applications/
cp /usr/share/applications/java-*-policytool.desktop ~/.local/share/applications/
echo "NoDisplay=true" >> ~/.local/share/applications/java-*-jconsole.desktop
echo "NoDisplay=true" >> ~/.local/share/applications/java-*-policytool.desktop
if [ -x "$(command -v vlc)" ]; then
rm -rf ~/.local/share/applications/org.gnome.Totem.desktop
cp /usr/share/applications/org.gnome.Totem.desktop ~/.local/share/applications/
echo "NoDisplay=true" >> ~/.local/share/applications/org.gnome.Totem.desktop
fi
if [ -x "$(command -v VirtualBox)" ]; then
rm -rf ~/.local/share/applications/org.gnome.Boxes.desktop
cp /usr/share/applications/org.gnome.Boxes.desktop ~/.local/share/applications/
echo "NoDisplay=true" >> ~/.local/share/applications/org.gnome.Boxes.desktop
fi
}
removeUnwantedShortcuts
#!/bin/bash
# https://wiki.archlinux.org/index.php/simple_stateful_firewall
sudo iptables -N TCP
sudo iptables -N UDP
sudo iptables -P FORWARD DROP
sudo iptables -P OUTPUT ACCEPT
sudo iptables -P INPUT DROP
sudo iptables -A INPUT -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT
sudo iptables -A INPUT -i lo -j ACCEPT
sudo iptables -A INPUT -m conntrack --ctstate INVALID -j DROP
sudo iptables -A INPUT -p icmp --icmp-type 8 -m conntrack --ctstate NEW -j ACCEPT
sudo iptables -A INPUT -p udp -m conntrack --ctstate NEW -j UDP
sudo iptables -A INPUT -p tcp --syn -m conntrack --ctstate NEW -j TCP
sudo iptables -A INPUT -p udp -j REJECT --reject-with icmp-port-unreachable
sudo iptables -A INPUT -p tcp -j REJECT --reject-with tcp-reset
sudo iptables -A INPUT -j REJECT --reject-with icmp-proto-unreachable
sudo iptables -t raw -I PREROUTING -m rpfilter --invert -j DROP
sudo iptables -I TCP -p tcp -m recent --update --rsource --seconds 60 --name TCP-PORTSCAN -j REJECT --reject-with tcp-reset
sudo iptables -D INPUT -p tcp -j REJECT --reject-with tcp-reset
sudo iptables -A INPUT -p tcp -m recent --set --rsource --name TCP-PORTSCAN -j REJECT --reject-with tcp-reset
sudo iptables -I UDP -p udp -m recent --update --rsource --seconds 60 --name UDP-PORTSCAN -j REJECT --reject-with icmp-port-unreachable
sudo iptables -D INPUT -p udp -j REJECT --reject-with icmp-port-unreachable
sudo iptables -A INPUT -p udp -m recent --set --rsource --name UDP-PORTSCAN -j REJECT --reject-with icmp-port-unreachable
sudo iptables -D INPUT -j REJECT --reject-with icmp-proto-unreachable
sudo iptables -A INPUT -j REJECT --reject-with icmp-proto-unreachable
#!/bin/bash
if pip3 show jupyterlab ; then
jupyter lab --ip=0.0.0.0 --port=8888 --no-browser
else
if pip3 show notebook; then
jupyter notebook --ip=0.0.0.0 --port=8888 --no-browser
else
echo "Jupyter lab/notebook can't be found"
fi
fi
#!/bin/bash
syncLibraries() {
remoteDirectory="ashesh@raspberrypi:/home/ashesh/Videos/"
targetDirectory="/run/media/ashesh/0A1E77D51E77B871/Videos"
notify-send 'syncLibraries' 'Start copying files from RaspberryPi to HDD.' --icon=dialog-information --category=transfer --urgency=normal
mkdir -p "/home/ashesh/.log"
date | tee -a "/home/ashesh/.log/syncLibraries.log"
echo >> "/home/ashesh/.log/syncLibraries.log"
rsync -avih --progress $remoteDirectory $targetDirectory 2>&1 | tee -a "/home/ashesh/.log/syncLibraries.log" || (notify-send 'syncLibraries' 'Failed copying files from RaspberryPi to HDD.' --icon=dialog-error --category=transfer --urgency=critical && return)
echo >> "/home/ashesh/.log/syncLibraries.log"
echo >> "/home/ashesh/.log/syncLibraries.log"
notify-send 'syncLibraries' 'End copying files from RaspberryPi to HDD.' --icon=dialog-information --category=transfer.complete --urgency=normal
}
syncLibraries
#!/bin/bash
MAX=65535
#cleanup
rm -rf ThereAreTooManyMethods.java
cat << 'EOT' >> ThereAreTooManyMethods.java
package com.ashessin.cs474.scratch;
public class ThereAreTooManyMethods {
private static int i = 0;
EOT
for i in $(eval echo "{1..$MAX}"); do
cat << EOT >> ThereAreTooManyMethods.java
public void Method$i() {
i++;
}
EOT
done
cat << 'EOT' >> ThereAreTooManyMethods.java
public static void main(String[] args) {
ThereAreTooManyMethods tooManyMethods= new ThereAreTooManyMethods();
EOT
for j in $(eval echo "{1..$MAX}"); do
cat << EOT >> ThereAreTooManyMethods.java
tooManyMethods.Method$j();
EOT
done
cat << 'EOT' >> ThereAreTooManyMethods.java
i++;
System.out.println(i);
}
}
EOT
javac ThereAreTooManyMethods.java
java ThereAreTooManyMethods.class
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment