Skip to content

Instantly share code, notes, and snippets.

@yymao
Last active May 16, 2019 13:17
Show Gist options
  • Save yymao/914660bd169ff54509ad2b1d297db75b to your computer and use it in GitHub Desktop.
Save yymao/914660bd169ff54509ad2b1d297db75b to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
import os
from shutil import copyfileobj
try:
from urllib.request import urlopen
except ImportError:
from urllib2 import urlopen
SLAC_KNOWN_HOSTS_URL = 'ftp://ftp.slac.stanford.edu/admin/known_hosts'
KNOWN_HOSTS_PATH = os.path.join(os.getenv('HOME'), '.ssh', 'known_hosts')
my_hosts = []
if os.path.isfile(KNOWN_HOSTS_PATH):
with open(KNOWN_HOSTS_PATH, 'r') as f:
for l in f:
l = l.strip()
if l and l[0] != '#' and 'slac.stanford.edu' not in l:
my_hosts.append(l)
os.rename(KNOWN_HOSTS_PATH, KNOWN_HOSTS_PATH + '.bak')
with open(KNOWN_HOSTS_PATH, 'wb') as f:
copyfileobj(urlopen(SLAC_KNOWN_HOSTS_URL), f)
if my_hosts:
with open(KNOWN_HOSTS_PATH, 'a') as f:
f.write('\n#below are non-slac hosts\n')
f.write('\n'.join(my_hosts))
f.write('\n')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment