-
Notifications
You must be signed in to change notification settings - Fork 16
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Printing the full output of apt-smart --list-mirrors to a file #16
Comments
You can use script --command 'apt-smart --list-mirrors' --return >mirrors.txt |
@Fleshgrinder , thanks a million! This works perfect. Now it's time to change how to prioritize servers giving more weight to speed than the default behavior. |
I'm sure this can be simplified, but I think it does what we are looking for.
#!/usr/bin/env bash
set -Eeuo pipefail
script --command 'apt-smart --list-mirrors' --return >&2
trap 'rm -f typescript' EXIT
declare -A urls=()
while read -r line; do
urls["${line%%:*}"]="${line##*: }"
done < <(grep --extended-regexp '^[0-9]+:' typescript)
mirrors=
while read -r line; do
line=${line,,}
if [[ $line =~ .*(unknown|days?|weeks?|months?|years?).* ]]; then
continue
fi
bandwidth=$(cut --delimiter='|' --fields=4 <<<"$line" | xargs)
case $bandwidth in
*kb/s) unit=k ;;
*mb/s) unit=M ;;
*gb/s) unit=G ;;
*) unit= ;;
esac
bandwidth=${bandwidth%% *}
bandwidth=${bandwidth%%.*}
bandwidth="$bandwidth$unit"
lag=$(cut --delimiter='|' --fields=3 <<<"$line" | xargs)
case $lag in
'up to date') lag=0 ;;
*hours*) lag=$((${lag%% *} * 60)) ;;
*) lag=${lag%% *} ;;
esac
url=$(cut --delimiter='|' --fields=2 <<<"$line" | xargs)
[[ $url != *...* ]] || url="${urls["$(cut --delimiter='|' --fields=1 <<<"$line" | xargs)"]}"
mirrors+="$bandwidth $lag $url"$'\n'
done < <(cut --delimiter='|' --fields=2,3,6,7 --only-delimited typescript | tail --lines=+4)
mirrors=${mirrors:0:-1}
sort --key=1,1hr --key=2,2n <<<"$mirrors" | cut --delimiter=' ' --fields=3 |
@Fleshgrinder , this looks like what I was looking for! |
Sure, no problem. You can remove the #!/usr/bin/env bash
set -Eeuo pipefail
script --command 'apt-smart --list-mirrors' --return >&2
trap 'rm -f typescript' EXIT
declare -A urls=()
while read -r line; do
urls["${line%%:*}"]="${line##*: }"
done < <(grep --extended-regexp '^[0-9]+:' typescript)
mirrors=
while read -r line; do
line=${line,,}
if [[ $line =~ .*(unknown|days?|weeks?|months?|years?).* ]]; then
continue
fi
lag=$(cut --delimiter='|' --fields=3 <<<"$line" | xargs)
case $lag in
'up to date') lag=0 ;;
*hours*) lag=$((${lag%% *} * 60)) ;;
*) lag=${lag%% *} ;;
esac
((lag < 600)) || continue
bandwidth=$(cut --delimiter='|' --fields=4 <<<"$line" | xargs)
case $bandwidth in
*kb/s) unit=k ;;
*mb/s) unit=M ;;
*gb/s) unit=G ;;
*) unit= ;;
esac
bandwidth=${bandwidth%% *}
bandwidth=${bandwidth%%.*}
bandwidth="$bandwidth$unit"
url=$(cut --delimiter='|' --fields=2 <<<"$line" | xargs)
[[ $url != *...* ]] || url="${urls["$(cut --delimiter='|' --fields=1 <<<"$line" | xargs)"]}"
mirrors+="$bandwidth $lag $url"$'\n'
done < <(cut --delimiter='|' --fields=2,3,6,7 --only-delimited typescript | tail --lines=+4)
mirrors=${mirrors:0:-1}
sort --key=1,1hr --key=2,2n <<<"$mirrors" | cut --delimiter=' ' --fields=3 |
Thanks a lot!
|
I'd like to automate the process of selecting the best mirror via a cron job script. Probably the biggest problem is that I can't print the output of
apt-smart --list-mirrors
to a file fully. For example, the commandapt-smart --list-mirrors > /tmp/mirrors.txt
contains only the names of mirror servers - without rank, last updated and bandwidth columns, which I also need so that I can better optimize the choice of the best server, as currentlyapt-smart --find-best-mirror
unfortunately chooses the most up-to-date server, although it may be far too slow compared with another which is several times faster while only a couple of hours behind its last update time.The text was updated successfully, but these errors were encountered: