Skip to content

Instantly share code, notes, and snippets.

@wilpig
Created October 29, 2021 20:57
Show Gist options
  • Save wilpig/2e98743b9b6b047615cde7692a7776cb to your computer and use it in GitHub Desktop.
Save wilpig/2e98743b9b6b047615cde7692a7776cb to your computer and use it in GitHub Desktop.
#!/usr/local/bin/python3
import socket
import subprocess
remote_names=['home.themillikens.com','plex.iyeman.net']
debug=False
def update(hostname):
try:
addr = socket.gethostbyname(hostname)
except:
print ('error resolving '+hostname)
return
grep=subprocess.run("grep "+hostname+" wilpig.com.conf -B1",shell=True,capture_output=True)
if debug:
print (grep)
if grep.returncode == 0:
if debug:
print(grep.stdout.decode())
if addr not in grep.stdout.decode():
if debug:
print ('update address')
replace_cmd='sed -i "" "s/'+grep.stdout.decode().split("\n")[0]+'/ Require ip '+addr+'/" wilpig.com.conf'
sed=subprocess.run(replace_cmd,shell=True,capture_output=True)
if sed.returncode == 0:
print (hostname+' updated to '+addr)
else:
print ('sed failed '+str(sed.returncode)+' '+sed.stdout.decode())
else:
print (hostname+' not found in wilpig.com.conf')
for x in remote_names:
update(x)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment