Skip to content

Instantly share code, notes, and snippets.

@xinomilo
Forked from eighthave/find-https-debian-archives.py
Last active December 20, 2017 01:17
Show Gist options
  • Save xinomilo/e55185c04ae7e0e713c2 to your computer and use it in GitHub Desktop.
Save xinomilo/e55185c04ae7e0e713c2 to your computer and use it in GitHub Desktop.
Script to find official Debian mirrors that support HTTPS
#!/usr/bin/python
import urllib2
import re
import ssl
import sys
# # find generic mirrors
mirrors = urllib2.urlopen('https://www.debian.org/mirror/list')
https = []
for line in mirrors.readlines():
m = re.match('.*<td valign="top"><a rel="nofollow" href="http(.*)">.*', line)
if m:
url = 'https' + m.group(1)
print 'trying: ',
print url,
print '...',
sys.stdout.flush()
try:
response=urllib2.urlopen(url, timeout=1)
https.append(url)
print 'success!'
except urllib2.URLError as err:
print 'fail!'
except ssl.SSLError as err:
print 'bad SSL!'
except ssl.CertificateError as err:
print 'Certificate Error!'
# print 'HTTPS apt repos:'
#for url in https:
# print url
# # find security mirrors
mirrors = urllib2.urlopen('https://www.debian.org/mirror/list-full')
securitys = []
for line in mirrors.readlines():
m = re.match('.*</tt><br>Security updates over HTTP: <tt><a rel="nofollow" href="http(.*)">.*/debian-security/</a>.*', line)
if m:
url = 'https' + m.group(1)
print 'trying: ',
print url,
print '...',
sys.stdout.flush()
try:
response=urllib2.urlopen(url, timeout=1)
securitys.append(url)
print 'success!'
except urllib2.URLError as err:
print 'fail!'
except ssl.SSLError as err:
print 'bad SSL!'
except ssl.CertificateError as err:
print 'Certificate Error!'
# print 'HTTPS security repos:'
# for url in securitys:
# print url
# now find the backports mirrors
mirrors = urllib2.urlopen('http://backports-master.debian.org/Mirrors/')
backports = []
for line in mirrors.readlines():
#<td><a href="http://be.mirror.eurid.eu/debian-backports/">/debian-backports/</a>
m = re.match('.*<td><a href="http(.*)">.*/debian-backports/</a>.*', line)
if m:
url = 'https' + m.group(1)
print 'trying: ',
print url,
print '...',
sys.stdout.flush()
try:
response=urllib2.urlopen(url, timeout=1)
backports.append(url)
print 'success!'
except urllib2.URLError as err:
print 'fail!'
except ssl.SSLError as err:
print 'bad SSL!'
except ssl.CertificateError as err:
print 'Certificate Error!'
#print 'HTTPS backports repos:'
#for url in backports:
# print url
# now find the CD image mirrors
mirrors = urllib2.urlopen('https://www.debian.org/CD/http-ftp/')
cds = []
for line in mirrors.readlines():
# <a rel="nofollow" href="http://mirror.easyspeedy.com/debian-cd/">HTTP</a></li>
m = re.match('.*<a rel="nofollow" href="http(:.*)">HTTP</a></li>.*', line)
if m:
url = 'https' + m.group(1)
print 'trying: ',
print url,
print '...',
sys.stdout.flush()
try:
response=urllib2.urlopen(url, timeout=1)
cds.append(url)
print 'success!'
except urllib2.URLError as err:
print 'fail!'
except ssl.SSLError as err:
print 'bad SSL!'
except ssl.CertificateError as err:
print 'Certificate Error!'
print 'HTTPS CD image repos:'
for url in cds:
print url
# now write everything to a file
f = open('/tmp/https-debian-archives.txt', 'w')
f.write('HTTPS apt repos\n')
f.write('---------------\n')
for url in https:
f.write(url + '\n')
f.write('\n\nHTTPS security repos\n')
f.write('---------------\n')
for url in securitys:
f.write(url + '\n')
f.write('\n\nHTTPS backports repos\n')
f.write('--------------------\n')
for url in backports:
f.write(url + '\n')
f.write('\n\nHTTPS CD image repos\n')
f.write('--------------------\n')
for url in cds:
f.write(url + '\n')
f.close()
@xinomilo
Copy link
Author

xinomilo commented Dec 1, 2015

https mirrors output (1/12/2015) :

HTTPS apt repos

https://mirror.cse.unsw.edu.au/debian/
https://debian.ludost.net/debian/
https://debian.savoirfairelinux.net/debian/
https://mirror.cpsc.ucalgary.ca/mirror/debian.org/debian/
https://mirrors.tuna.tsinghua.edu.cn/debian/
https://mirror.one.com/debian/
https://mirror.ibcp.fr/pub/debian/
https://mirror.plusserver.com/debian/
https://ftp.fau.de/debian/
https://ftp-stud.hs-esslingen.de/debian/
https://packages.hs-regensburg.de/debian/
https://dennou-h.gfd-dennou.org/debian/
https://dennou-k.gfd-dennou.org/debian/
https://dennou-q.gfd-dennou.org/debian/
https://mirror.i3d.net/pub/debian/
https://ftp.rnl.tecnico.ulisboa.pt/pub/debian/
https://mirror.sinavps.ch/debian/
https://opensource.nchc.org.tw/debian/
https://mirror.applebred.net/debian/
https://mirror.kku.ac.th/debian/
https://mirrorservice.org/sites/ftp.debian.org/debian/
https://mirror.vorboss.net/debian/
https://the.earth.li/debian/
https://lug.mtu.edu/debian/
https://mirror.hmc.edu/debian/
https://mirrors.bloomu.edu/debian/
https://mirrors.kernel.org/debian/
https://mirrors.ocf.berkeley.edu/debian/
https://mirrors.xmission.com/debian/

HTTPS security repos

https://mirror.aarnet.edu.au/pub/debian-security/
https://mirror.cse.unsw.edu.au/debian-security/
https://mirror.cpsc.ucalgary.ca/mirror/debian.org/debian-security/

HTTPS backports repos

https://packages.hs-regensburg.de/debian-backports/
https://ftp-stud.hs-esslingen.de/debian-backports/
https://ftp.fau.de/debian-backports/
https://mirror.ibcp.fr/pub/debian-backports/
https://mirrorservice.org/sites/backports.debian.org/debian-backports/
https://mirror.i3d.net/pub/debian-backports/
https://ftp.rnl.tecnico.ulisboa.pt/pub/debian-backports/
https://mirror.applebred.net/debian-backports/
https://lug.mtu.edu/debian-backports/

HTTPS CD image repos

https://mirror.cse.unsw.edu.au/debian-cd/
https://debian.savoirfairelinux.net/pub/mirrors/debian-cd/
https://mirror.one.com/debian-cd/
https://ftp.fau.de/debian-cd/
https://ftp-stud.hs-esslingen.de/debian-cd/
https://ftp.rnl.tecnico.ulisboa.pt/pub/debian-cd/
https://mirrorservice.org/sites/cdimage.debian.org/debian-cd/
https://lug.mtu.edu/debian-cd/
https://mirrors.bloomu.edu/debian-cd/
https://mirrors.kernel.org/debian-cd/
https://mirrors.ocf.berkeley.edu/debian-cd/
https://mirrors.xmission.com/debian-cd/

@Lew-Rockwell-Fan
Copy link

Lew-Rockwell-Fan commented Dec 19, 2017

Thank you for posting this. How did you distinguish the "security repos" from the others? I don't see a distinguishing feature in the output lines.

My objective is a sources.list for Stretch that will get everything with https only.

For:
--those like me who are muddling through this & need a practical example of using this output (I sure could have)
AND
--anyone who can tell me if this looks right & whether apt-get update should have a number of output lines = the number of source lines in sources.list, which seems to me ought to be true, but isn't for me.

This SEEMS, kinda, sorta, I think, to be working for me, maybe:

. . . . . . . . . . . . . . . . . . .
deb https://deb.debian.org/debian stretch main
deb-src https://deb.debian.org/debian stretch main

deb https://deb.debian.org/debian stretch-updates main
deb-src https://deb.debian.org/debian stretch-updates main

deb https://mirror.aarnet.edu.au/pub/debian-security/ stretch/updates main
deb-src https://mirror.aarnet.edu.au/pub/debian-security/ stretch/updates main
. . . . . . . . . . . . . . . . . . .

But that's 6 lines, so I'd expect apt-get update to have 6 corresponding lines of output if it treats the deb and deb-src lines separately, or 3 if it should be 1 line/site. But I get 4, like this:

root@stretch:/home/j# apt-get update
Hit:1 https://mirror.aarnet.edu.au/pub/debian-security stretch/updates InRelease
Ign:2 https://cdn-aws.deb.debian.org/debian stretch InRelease
Hit:3 https://cdn-aws.deb.debian.org/debian stretch-updates InRelease
Hit:4 https://cdn-aws.deb.debian.org/debian stretch Release
Reading package lists... Done

The part following the space in each line is absolutely consistent, as is the number of lines, although the order of the lines isn't, and the numbers following the colon aren't. And of course "Hit" & "Ign" have varied a little.

So, am I doing it wrong or what?

For anybody trying this who doesn't care about the repos that DON'T work, but only wants to see the ones that do, I suggest invoking this like this:

whatever_you_name_the_script|grep -E "^trying:.{1,}success!$"|tee debian-secure-repos-xenomilo.txt

That works in bash at least, haven't tested in other shells, to give a cleaner output in the terminal (or console) and a totally clean output in the file. You'll still see 404s in the terminal. If you don't want to, you could tack on 2>/dev/null to the end I suppose.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment