A few days ago I had to "sudo apt clean" my local cache to uninstall a bad package, but I needed to recover my cache, with the installed packages, since I work often in offline places, so that I can recover from a broken system without an active Internet connection.
So to be able to recover, or better, rebuild (or re-cache) your packages to your local storage (ie HDD or SSD) that usually is located at: /var/cache/apt/archives we have to do this:
1) get the installed package names:
dpkg -l | grep "^ii" | awk ' {print $2} ' > pkg_names.txt
2) get the installed package versions:
dpkg -l | grep "^ii" | awk ' {print $3} ' > pkg_version.txt
3) combine all package names and versions:
paste -d"=" pkg_names.txt pkg_version.txt > pkg_combine.txt
4) now download packages to cache:
cat pkg_combine.txt | xargs sudo apt-get -y install --reinstall --download-only
REF:
dpkg -l | grep "^ii"| awk ' {print $2} ' | xargs sudo apt-get -y install --reinstall --download-only
dpkg -s firefox | grep Version | awk ' {print $2} '
sudo apt-get install package=version
Todo:
Timeout each line to avoid long loops (when source is dead or pkg not available at repository)
Mostrando postagens com marcador bash. Mostrar todas as postagens
Mostrando postagens com marcador bash. Mostrar todas as postagens
sábado, 26 de outubro de 2019
quarta-feira, 16 de outubro de 2019
List total hits of iptable drops
I have been busy making and studying defense strategies to better implement and monitor my firewall system.
So after you have decided to insert DROP rules in your iptables, it is a good practice to check the statistics of how efficient the additional "load" has been to your overall performance, that is, is it effective or should you leave it for fail2ban to control the hits ?
I made a bash script to summaries the hits on iptables rules to avoid all the stdout that obfuscates the important information.
As usually sad, use at your own risk. Make a backup plan before proceeding.
#!/bin/bash
#
# check how many hits on iptables drop rule
# by braselectron.com OCT 16, 2019
#
# iptables formated output example needed:
# '0 0 DROP all -- any any 47.203.94.77 anywhere'
#
# so now clear and fix the spaces on the output
#
readarray iptls <<< "$(sudo iptables -vnL | grep DROP |\
sed 's/ / /g' | sed 's/ / /g' | sed 's/ / /g' |\
sed 's/^ //g')"
#
# debug point
# echo "iptls lenght is ${#iptls[@]}"
#
echo -e "Hits\tTarget Denied"
for element in "${iptls[@]}"
do
#
# debug point
# echo "> $element"
#
verify=( $(echo "$element" | cut -d " " -f 1) )
#
# debug point
# echo "verify = $verify"
#
if [ "$verify" -ne "0" ]; then
hits="$verify"
target=( $(echo "$element" | cut -d " " -f 8) )
echo -e "$hits\t$target"
fi
done
#
# check how many hits on iptables drop rule
# by braselectron.com OCT 16, 2019
#
# iptables formated output example needed:
# '0 0 DROP all -- any any 47.203.94.77 anywhere'
#
# so now clear and fix the spaces on the output
#
readarray iptls <<< "$(sudo iptables -vnL | grep DROP |\
sed 's/ / /g' | sed 's/ / /g' | sed 's/ / /g' |\
sed 's/^ //g')"
#
# debug point
# echo "iptls lenght is ${#iptls[@]}"
#
echo -e "Hits\tTarget Denied"
for element in "${iptls[@]}"
do
#
# debug point
# echo "> $element"
#
verify=( $(echo "$element" | cut -d " " -f 1) )
#
# debug point
# echo "verify = $verify"
#
if [ "$verify" -ne "0" ]; then
hits="$verify"
target=( $(echo "$element" | cut -d " " -f 8) )
echo -e "$hits\t$target"
fi
done
If all goes well you will get a output similar to this:
Hits Target Denied
1 198.108.66.0/23
4 92.118.161.0/24
3 92.118.160.0/24
1 74.82.47.0/24
1 185.173.35.0/24
1 71.6.128.0/17
2 122.228.0.0/16
4 95.154.101.209
1131 139.59.13.150
1 198.108.66.0/23
4 92.118.161.0/24
3 92.118.160.0/24
1 74.82.47.0/24
1 185.173.35.0/24
1 71.6.128.0/17
2 122.228.0.0/16
4 95.154.101.209
1131 139.59.13.150
But this is based on my active iptables rules.
Shields up captain!
Cheers!
quarta-feira, 25 de setembro de 2019
Check your servers are UP - Linux BASH
Recently I was trying to check if one of my servers had a glitch, so after some research and coding I came up with this solution for Linux (ie. in my case for Raspbian Jessie):
1) Get the following code and save it to your target server.
2) Choose another host on your network to ping that is up (for sure) and is trusted by you. For example: 192.168.0.1 (usually the router on your network - default factory value).
3) Now using "nohup" to setup your test on the target host, at the command terminal, do this:
4) Now you can logoff and from time to time check the nohup.out file to see if the ping failed.
5) When you are satisfied with your tests just kill the process on the target host, doing this:
5.1) first find your process with: ps -ef | grep test_alive
This will give you a output similar to this:
pi 1257 4688 1 17:59 pts/1 00:00:00 grep --color=auto test_alive
pi 22213 1 9 15:08 ? 00:16:24 /bin/bash test_alive.sh 192.168.0.1
5.2) get the process id and kill it with: kill 22213 # caution! use your id number
5.3) and remove the nohup.out file with: rm nohup.out
NOTE: if the nohup.out file is empty, means no erros, your server is working and connectivity is working. But if the target host is freezing or other problems that make the script stop, this may mislead your conclusion.
Cheers!
1) Get the following code and save it to your target server.
#!/bin/bash
#
# Test connectivity with ping
# filename: test_alive.sh
#
# braselectron.com - September 25, 2019
#
# get IP from command line argument
#
ip=$1
#
# check IP address format code
# Mitch Frazier - Linux Journal - June 26, 2008
#
valid_ip () {
local ip=$1
local stat=1
if [[ $ip =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]]; then
OIFS=$IFS
IFS='.'
ip=($ip)
IFS=$OIFS
[[ ${ip[0]} -le 255 && ${ip[1]} -le 255 && ${ip[2]} -le 255 && ${ip[3]} -le 255 ]]
stat=$?
fi
return $stat
}
#
# check syntax
#
if [ $# -eq 0 ]
then echo "syntax: test_live.sh <ip address>"
exit 1
fi
#
if ! valid_ip $ip
then echo "IP is invalid"
exit 1
fi
#
# ping but don't wait
# log if ping fails
#
while true
do ping -w 1 -c 1 $ip |\
grep received |\
cut -d" " -f 4 |\
if [ "$(cat -)" != "1" ]
then echo "Ping failed $(date)"
fi
done
#
# Test connectivity with ping
# filename: test_alive.sh
#
# braselectron.com - September 25, 2019
#
# get IP from command line argument
#
ip=$1
#
# check IP address format code
# Mitch Frazier - Linux Journal - June 26, 2008
#
valid_ip () {
local ip=$1
local stat=1
if [[ $ip =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]]; then
OIFS=$IFS
IFS='.'
ip=($ip)
IFS=$OIFS
[[ ${ip[0]} -le 255 && ${ip[1]} -le 255 && ${ip[2]} -le 255 && ${ip[3]} -le 255 ]]
stat=$?
fi
return $stat
}
#
# check syntax
#
if [ $# -eq 0 ]
then echo "syntax: test_live.sh <ip address>"
exit 1
fi
#
if ! valid_ip $ip
then echo "IP is invalid"
exit 1
fi
#
# ping but don't wait
# log if ping fails
#
while true
do ping -w 1 -c 1 $ip |\
grep received |\
cut -d" " -f 4 |\
if [ "$(cat -)" != "1" ]
then echo "Ping failed $(date)"
fi
done
2) Choose another host on your network to ping that is up (for sure) and is trusted by you. For example: 192.168.0.1 (usually the router on your network - default factory value).
3) Now using "nohup" to setup your test on the target host, at the command terminal, do this:
nohup /bin/bash test_alive.sh 192.168.0.1 &
# remember to rename the 192.168.0.1 with your host IP (step 2)
# remember to rename the 192.168.0.1 with your host IP (step 2)
4) Now you can logoff and from time to time check the nohup.out file to see if the ping failed.
5) When you are satisfied with your tests just kill the process on the target host, doing this:
5.1) first find your process with: ps -ef | grep test_alive
This will give you a output similar to this:
pi 1257 4688 1 17:59 pts/1 00:00:00 grep --color=auto test_alive
pi 22213 1 9 15:08 ? 00:16:24 /bin/bash test_alive.sh 192.168.0.1
5.2) get the process id and kill it with: kill 22213 # caution! use your id number
5.3) and remove the nohup.out file with: rm nohup.out
NOTE: if the nohup.out file is empty, means no erros, your server is working and connectivity is working. But if the target host is freezing or other problems that make the script stop, this may mislead your conclusion.
Cheers!
Assinar:
Postagens (Atom)