Compare commits
4 Commits
1484b004be
...
bc8cb4a237
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
bc8cb4a237 | ||
|
|
ae90a9f4c4 | ||
|
|
7e661ca2de | ||
|
|
9f22ed4304 |
@@ -20,6 +20,7 @@ prompt. Here is a non-exhaustive list of what we have:
|
||||
- A bar style prompt with hour, execution time and exit code of the last
|
||||
command;
|
||||
- clean: erase after confirmation any backup file, possibly recursively;
|
||||
- dwl: a curl/wget/fetch download wrapper;
|
||||
- pkgs: search for the given pattern in the installed packages name;
|
||||
- expandlist: usefull in scripts, it expand any expression using wildcards into
|
||||
the corresponding list of file and directories;
|
||||
@@ -31,6 +32,7 @@ the corresponding list of file and directories;
|
||||
- ku: kill all the processes owned by the given user name or ID;
|
||||
- mcd: create a directory and immediately move into it;
|
||||
- meteo: display weather forecast information;
|
||||
- myextip: get informations about your public IP;
|
||||
- ppg: look for the given patern in the running processes;
|
||||
- rain: console screensaver with rain effect;
|
||||
- rmhost: remove the given host (name or IP) to the list of SSH known host;
|
||||
@@ -51,8 +53,9 @@ directory only if needed;
|
||||
|
||||
## 3. Configuration
|
||||
Some functions might have configurable default behaviour. You can create a
|
||||
.profile.conf file to configure those default behaviour. You should have a look
|
||||
at the doc/.profile.conf.example to see the list of available options.
|
||||
profile.conf file to configure those default behaviour. You should have a look
|
||||
at the doc/profile.conf.example to see the list of available options. The
|
||||
configuration file is located in the same directory as profile.sh file.
|
||||
|
||||
## 4. Contact and more information
|
||||
### 4.1. New users
|
||||
|
||||
@@ -16,8 +16,8 @@ TERM=xterm-256color
|
||||
DEFAULT_CITY="Toulouse"
|
||||
|
||||
[general]
|
||||
# General section allow to set any variable that can be used by the profile or its functions. It is
|
||||
# also a good place to set freely global variables for personal use.
|
||||
# General section allow to set any variable that can be used by the profile or its functions.
|
||||
# It is also a good place to set freely global variables for personal use.
|
||||
# Set some compiling values
|
||||
CFLAGS="-O2 -pipe -march=native"
|
||||
CXXFLAGS="$CFLAGS"
|
||||
|
||||
@@ -104,7 +104,7 @@ meteo()
|
||||
|
||||
for city in "${cities[@]}"; do
|
||||
encoded=$(urlencode "$city")
|
||||
curl -s "https://wttr.in/$encoded" || \
|
||||
dwl "https://wttr.in/$encoded" || \
|
||||
disp E "Failed fetching datas for $city."
|
||||
done
|
||||
}
|
||||
|
||||
141
profile.d/net.sh
141
profile.d/net.sh
@@ -34,6 +34,54 @@
|
||||
# * OF SUCH DAMAGE.
|
||||
# ------------------------------------------------------------------------------
|
||||
|
||||
# ------------------------------------------------------------------------------
|
||||
# Download a resource using curl, wget, or fetch.
|
||||
# Usage: dwl <url> [output_file]
|
||||
dwl()
|
||||
{
|
||||
case "$1" in
|
||||
--help|-h)
|
||||
echo "Usage: dwl <url> [output_file]"
|
||||
echo "Downloads a resource using curl, wget, or fetch."
|
||||
echo ""
|
||||
echo "Arguments:"
|
||||
echo " url The full URL to download (http/https/ftp)."
|
||||
echo " output_file (Optional) Path to save the file. If omitted, prints to stdout."
|
||||
return 0
|
||||
;;
|
||||
"")
|
||||
echo "Error: URL argument is missing." >&2
|
||||
echo "Try 'get_resource --help' for usage." >&2
|
||||
return 1
|
||||
;;
|
||||
esac
|
||||
|
||||
case "$1" in
|
||||
http://*|https://*|ftp://*) ;;
|
||||
*)
|
||||
echo "Error: '$1' does not look like a valid URL. Must start with http://, https://, or ftp://" >&2
|
||||
return 1
|
||||
;;
|
||||
esac
|
||||
|
||||
local url="$1"
|
||||
local output="$2"
|
||||
|
||||
if command -v curl >/dev/null 2>&1; then
|
||||
[ -z "$output" ] && curl -sL "$url" || curl -sL -o "$output" "$url"
|
||||
elif command -v wget >/dev/null 2>&1; then
|
||||
[ -z "$output" ] && wget -qO- "$url" || wget -q -O "$output" "$url"
|
||||
elif command -v fetch >/dev/null 2>&1; then
|
||||
[ -z "$output" ] && fetch -o - "$url" || fetch -o "$output" "$url"
|
||||
else
|
||||
echo "Error: No download utility (curl, wget, or fetch) found." >&2
|
||||
return 1
|
||||
fi
|
||||
}
|
||||
export -f dwl
|
||||
# ------------------------------------------------------------------------------
|
||||
|
||||
|
||||
# ------------------------------------------------------------------------------
|
||||
# Determine if parameter is a valid IPv4 address
|
||||
# Usage: isipv4 <ip_address>
|
||||
@@ -43,23 +91,32 @@ isipv4()
|
||||
local ip=$1
|
||||
[[ -z $ip ]] && return 1
|
||||
|
||||
# Start with a regex format test
|
||||
if [[ $ip =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]]; then
|
||||
# Start with a regex format test (four octets)
|
||||
if [[ $ip =~ ^([0-9]{1,3}\.){3}[0-9]{1,3}$ ]]; then
|
||||
local old_ifs=$IFS
|
||||
IFS="."
|
||||
ip=($ip)
|
||||
IFS='.'
|
||||
read -r -a ip_arr <<< "$ip"
|
||||
IFS=$old_ifs
|
||||
if [[ ${ip[0]} -le 255 && ${ip[1]} -le 255 &&
|
||||
${ip[2]} -le 255 && ${ip[3]} -le 255 ]]; then
|
||||
if [[ -t 1 ]]; then
|
||||
disp "The given IPv4 is valid."
|
||||
|
||||
# Ensure each octet is between 0 and 255
|
||||
local oct
|
||||
for oct in "${ip_arr[@]}"; do
|
||||
# Reject leading plus/minus or empty entries
|
||||
if [[ -z $oct || $oct =~ [^0-9] ]]; then
|
||||
[[ -t 1 ]] && disp "The given parameter is NOT a valid IPv4."
|
||||
return 1
|
||||
fi
|
||||
if (( oct > 255 )); then
|
||||
[[ -t 1 ]] && disp "The given parameter is NOT a valid IPv4."
|
||||
return 1
|
||||
fi
|
||||
done
|
||||
|
||||
[[ -t 1 ]] && disp "The given IPv4 is valid."
|
||||
return 0
|
||||
fi
|
||||
fi
|
||||
if [[ -t 1 ]]; then
|
||||
disp "The given parameter is NOT a valid IPv4."
|
||||
fi
|
||||
|
||||
[[ -t 1 ]] && disp "The given parameter is NOT a valid IPv4."
|
||||
return 1
|
||||
}
|
||||
export -f isipv4
|
||||
@@ -67,7 +124,7 @@ export -f isipv4
|
||||
|
||||
|
||||
# ------------------------------------------------------------------------------
|
||||
# Determine if parameter is a valid IPv4 address
|
||||
# Determine if parameter is a valid IPv6 address
|
||||
# Usage: isipv6 <ip_address>
|
||||
isipv6()
|
||||
{
|
||||
@@ -98,7 +155,7 @@ urlencode() {
|
||||
for (( i = 0; i < length; i++ )); do
|
||||
local c="${str:i:1}"
|
||||
case "$c" in
|
||||
[a-zA-Z0-9.~_-]) printf "$c" ;;
|
||||
[a-zA-Z0-9.~_-]) printf '%s' "$c" ;;
|
||||
' ') printf '+' ;;
|
||||
*) printf '%%%02X' "'$c" #| cut -d' ' -f2 ;;
|
||||
esac
|
||||
@@ -113,12 +170,16 @@ export -f urlencode
|
||||
# Usage: myextip [-i|--ip] [-s|--isp] [-l|--loc] [-c|--coord]
|
||||
# If no option is provided, all information will be displayed.
|
||||
# Options:
|
||||
# -h, --help Display help screen
|
||||
# -i, --ip Display only the external IP address
|
||||
# -s, --isp Display only the ISP name
|
||||
# -l, --loc Display only the location (city, region, country)
|
||||
# -c, --coord Display only the coordinates (latitude, longitude)
|
||||
# -a, --as Display only the Autonomous System (AS) information
|
||||
# -R, --raw Display raw JSON response
|
||||
myextip() {
|
||||
local show_ip=false show_isp=false show_loc=false show_coord=false
|
||||
local show_ip=false show_isp=false show_loc=false
|
||||
local show_coord=false show_as=false show_raw=false
|
||||
local all=true
|
||||
|
||||
# Parse arguments
|
||||
@@ -144,15 +205,21 @@ myextip() {
|
||||
show_as=true
|
||||
all=false
|
||||
;;
|
||||
-R|--raw)
|
||||
all=false
|
||||
show_raw=true
|
||||
;;
|
||||
-h|--help)
|
||||
printf "Usage: myextip [-i|--ip] [-s|--isp] [-l|--loc] [-c|--coord] [-a|--as]\n"
|
||||
printf "Fetch and display external IP information.\n\n"
|
||||
printf "Usage: myextip [-i|--ip] [-s|--isp] [-l|--loc] [-c|--coord] [-a|--as] [-R|--raw]\n\n"
|
||||
printf "Options:\n"
|
||||
printf " -i, --ip Display only the external IP address\n"
|
||||
printf " -s, --isp Display only the ISP name\n"
|
||||
printf " -l, --loc Display only the location (city, region, country)\n"
|
||||
printf " -c, --coord Display only the coordinates (latitude, longitude)\n"
|
||||
printf " -a, --as Display only the Autonomous System (AS) information\n"
|
||||
printf "\t-h, --help\tDisplay this help screen\n"
|
||||
printf "\t-i, --ip\tDisplay only the external IP address\n"
|
||||
printf "\t-s, --isp\tDisplay only the ISP name\n"
|
||||
printf "\t-l, --loc\tDisplay only the location (city, region, country)\n"
|
||||
printf "\t-c, --coord\tDisplay only the coordinates (latitude, longitude)\n"
|
||||
printf "\t-a, --as\tDisplay only the Autonomous System (AS) information\n"
|
||||
printf "\t-R, --raw\tDisplay raw JSON response\n"
|
||||
return 0
|
||||
;;
|
||||
--)
|
||||
@@ -160,30 +227,40 @@ myextip() {
|
||||
break
|
||||
;;
|
||||
*)
|
||||
disp E "Unknown option: $1"
|
||||
disp E "Unknown option: $1, use \"myextip --help\" to display usage."
|
||||
return 1
|
||||
;;
|
||||
esac
|
||||
shift
|
||||
done
|
||||
|
||||
# Fetch data
|
||||
local response
|
||||
response=$(curl -s http://ip-api.com/json/)
|
||||
# Fetch data. Allow overriding endpoint via env var MYEXTIP_URL
|
||||
local MYEXTIP_URL
|
||||
MYEXTIP_URL=${MYEXTIP_URL:-http://ip-api.com/json/}
|
||||
|
||||
# Parse with jq
|
||||
if command -v jq >/dev/null 2>&1; then
|
||||
local response
|
||||
if ! response=$(dwl "$MYEXTIP_URL"); then
|
||||
disp E "Failed to fetch external IP information from $MYEXTIP_URL"
|
||||
return 2
|
||||
fi
|
||||
|
||||
# Parse with jq when available and when raw wasn't requested. The jq filter
|
||||
# is tolerant to field-name differences between providers (ip-api / ipinfo).
|
||||
if command -v jq >/dev/null 2>&1 && [[ "$show_raw" != true ]]; then
|
||||
echo "$response" | jq -r --argjson all "$all" --argjson ip "$show_ip" \
|
||||
--argjson isp "$show_isp" --argjson loc "$show_loc" \
|
||||
--argjson coord "$show_coord" --argjson as "$show_as" '
|
||||
[
|
||||
(if $all or $ip then "IP Address : \(.query)" else empty end),
|
||||
(if $all or $isp then "ISP : \(.isp)" else empty end),
|
||||
(if $all or $loc then "Location : \(.city), \(.regionName), \(.country)" else empty end),
|
||||
(if $all or $coord then "Coordinates: \(.lat), \(.lon)" else empty end),
|
||||
(if $all or $as then "AS : \(.as)" else empty end)
|
||||
(if $all or $ip then "IP Address : \(.query // .ip)" else empty end),
|
||||
(if $all or $isp then "ISP : \(.isp // .org)" else empty end),
|
||||
(if $all or $loc then
|
||||
("Location : " + ((.city // "") + (if .city then ", " else "" end) + (if .regionName then .regionName else .region end) + (if .country then ", " + .country else "" end)))
|
||||
else empty end),
|
||||
(if $all or $coord then (if (.lat and .lon) then "Coordinates: \(.lat), \(.lon)" elif .loc then "Coordinates: \(.loc)" else empty end) else empty end),
|
||||
(if $all or $as then "AS : \(.as // .org)" else empty end)
|
||||
] | .[]'
|
||||
else
|
||||
[[ "$show_raw" != true ]] && disp W "jq is not installed, displaying raw JSON response."
|
||||
echo "$response"
|
||||
fi
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user