Created
December 2, 2017 12:48
-
-
Save coman3/310fa534b8924d7ba4b0e4c8ba598454 to your computer and use it in GitHub Desktop.
Update Azure DNS recordset with servers current WAN IP.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using CommandLine; | |
using CommandLine.Text; | |
using Microsoft.Azure.Management.Dns.Fluent; | |
using Microsoft.Azure.Management.Dns.Fluent.Models; | |
using Microsoft.Azure.Management.Fluent; | |
using Microsoft.Azure.Management.ResourceManager.Fluent; | |
using Newtonsoft.Json; | |
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Net.Http; | |
using System.Text; | |
using System.Threading.Tasks; | |
namespace Azure.DynDNS | |
{ | |
class Program | |
{ | |
public static IAzure Azure { get; set; } | |
public static DnsManagementClient DnsManagementClient { get; set; } | |
public static Options Options { get; set; } = new Options(); | |
static void Main(string[] args) | |
{ | |
if (!CommandLine.Parser.Default.ParseArguments(args, Options)) | |
{ | |
Console.WriteLine("Error parsing arguments!"); | |
Console.WriteLine("ERROR: FAILED PARSE ARGS"); | |
#if DEBUG | |
Console.ReadKey(); | |
#endif | |
return; | |
} | |
try | |
{ | |
var creds = SdkContext.AzureCredentialsFactory.FromServicePrincipal(Options.ClientId, Options.ClientSecret, Options.TenantId, AzureEnvironment.AzureGlobalCloud); | |
Azure = Microsoft.Azure.Management.Fluent.Azure | |
.Configure() | |
.Authenticate(creds) | |
.WithDefaultSubscription(); | |
DnsManagementClient = new DnsManagementClient(creds); | |
var subscription = Azure.GetCurrentSubscription(); | |
DnsManagementClient.SubscriptionId = subscription.SubscriptionId; | |
} | |
catch (Exception ex) | |
{ | |
Console.Error.WriteLine(ex); | |
Console.WriteLine("ERROR: FAILED LOGIN"); | |
return; | |
} | |
Task.Run(() => PerformActions()).Wait(); | |
#if DEBUG | |
Console.ReadKey(); | |
#endif | |
} | |
static async void PerformActions() | |
{ | |
string ip = Options.Ip; | |
if (ip == "FETCH") | |
{ | |
try | |
{ | |
using (var httpClient = new HttpClient()) | |
{ | |
var response = await httpClient.GetStringAsync("http://ip-api.com/json"); | |
var responseObj = JsonConvert.DeserializeObject<dynamic>(response); | |
ip = responseObj.query.ToString(); | |
Console.WriteLine($"IP FETCHED: {ip}"); | |
} | |
} | |
catch (Exception) | |
{ | |
Console.WriteLine("ERROR: FAILED FETCH IP"); | |
return; | |
} | |
} | |
var para = new RecordSetInner(); | |
para.Name = Options.Suffix; | |
para.TTL = Options.Ttl; //30 minutes | |
switch (Options.Type) | |
{ | |
case RecordType.A: | |
para.ARecords = new List<ARecord> { | |
new ARecord(ip) | |
}; | |
break; | |
case RecordType.AAAA: | |
case RecordType.CNAME: | |
case RecordType.MX: | |
case RecordType.NS: | |
case RecordType.PTR: | |
case RecordType.SOA: | |
case RecordType.SRV: | |
case RecordType.TXT: | |
default: | |
Console.WriteLine($"Unsuported RecordType {Options.Type}."); | |
Console.WriteLine("ERROR: UNSUPPORTED TYPE"); | |
return; | |
} | |
try | |
{ | |
var data = await DnsManagementClient.RecordSets.CreateOrUpdateAsync(Options.ResourceGroup, Options.Zone, Options.Suffix, Options.Type, para); | |
Console.WriteLine($"SUCCESS: UPDATED {Options.Type} {Options.Suffix} (TTL: {Options.Ttl}) = {ip}"); | |
} | |
catch (Exception) | |
{ | |
Console.WriteLine("ERROR: FAILED UPDATE"); | |
return; | |
} | |
} | |
} | |
class Options | |
{ | |
//Authentication | |
[Option('c', "clientid", Required = true, HelpText = "ClientID (AppId)")] | |
public string ClientId { get; set; } | |
[Option('s', "clientsecret", Required = true, HelpText = "Client Secret (Password)")] | |
public string ClientSecret { get; set; } | |
[Option('i', "tenantid", Required = true, HelpText = "Tenant Id (Active Directiory Id)")] | |
public string TenantId { get; set; } | |
//Zone Updates | |
[Option('z', "zone", Required = true, HelpText = "DNSZone name")] | |
public string Zone { get; set; } | |
[Option('r', "resource", Required = true, HelpText = "ResourceGroup of DNSZone")] | |
public string ResourceGroup { get; set; } | |
[Option('n', "suffix", Required = false, DefaultValue = "@", HelpText = "DNS Recordset suffix")] | |
public string Suffix { get; set; } | |
[Option('t', "type", Required = false, DefaultValue = Microsoft.Azure.Management.Dns.Fluent.Models.RecordType.A, HelpText = "DNS Recordset type")] | |
public Microsoft.Azure.Management.Dns.Fluent.Models.RecordType Type { get; set; } | |
[Option('e', "ttl", Required = false, DefaultValue = 60 * 60, HelpText = "DNS TTL value")] | |
public int Ttl { get; set; } | |
[Option('p', "ip", Required = false, DefaultValue = "FETCH", HelpText = "DNS Recordset IP")] | |
public string Ip { get; set; } | |
[HelpOption] | |
public string GetUsage() | |
{ | |
return HelpText.AutoBuild(this, (HelpText current) => HelpText.DefaultParsingErrorsHandler(this, current)); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment