parse and rearrange hostnames

  • Last Update :
  • Techknowledgy :

infile

web3.maxi.com
web4.maxi.com
web5.maxi.com
mail1.mexi.com
web6.maxi.com
web9.maxi.com
web9.maxi.com

web11.maxi.com
mail3.mexi.com
web7.maxi.com
mail4.mexi.com
mail25.mexi.com
mail26.mexi.com
mail27.mexi.com
mail28.mexi.com
web8.maxi.com
mail29.mexi.com
mail110.mexi.com
web1.maxi.com

parse.pl

#!/usr/bin/perl -l

use Set::IntSpan;
use File::Slurp qw / slurp / ;

$str = slurp(\ * STDIN);

# Remove redundant whitespace
chop $str;
$str = ~s / ^ [\t] + | [\t] + $ //gm;
$str = ~s / \R + /\n/g;

# Copy $str so we can match numbers in it without disturbing the loop
$nums = $str;

# Parse lines in $str in sequence
while ($str = ~/^(.*)$/gm) {
   $line = $1;

   # Extract bits before and after number
      ($pre, $post) = $line = ~/([^\d]+)\d+(.*)$/m;

   # Check
   if its been printed already
   next
   if $seen {
      $pre.$post
   };

   # If not, extract numbers
   @numbers = $nums = ~/$pre(\d+)$post/g;

   print $pre.
   "["
   .Set::IntSpan - > new(@numbers) - > run_list()
      .
   "]".$post;

   $seen {
      $pre.$post
   } = 1;
}

Run it like this:

perl parse.pl < infile
1._
with open("data.txt") as f:
   sites = [x.strip() for x in f]
ranges = []
for x in sites:
   x = x.split(".")
num = int(x[0][x[0].index("web") + 3: ])
if ranges:
   if num - ranges[-1][-1] == 1:
   ranges[-1].append(num)
else:
   ranges.append([num])
else:
   ranges.append([num])
print ranges
print "web[" + ",".join(str(x[0]) if len(x) == 1
   else str(x[0]) + "-" + str(x[-1]) for x in ranges) + "].maxi.com"

output:

[
   [1],
   [3, 4, 5, 6, 7, 8, 9],
   [11]
]
web[1, 3 - 9, 11].maxi.com

My take on it:

#print hosts
lines = open("log.txt").readlines()
numbers = [int(line.split(".")[0][3: ]) for line in lines]
out = [
   []
]
index = 0
for i in xrange(len(numbers) - 1):
   out[index].append(numbers[i])
if (numbers[i + 1] - numbers[i] != 1):
   out.append([])
index += 1
out[-1].append(numbers[-1])
strings = [str(number[0]) if len(number) == 1
   else str(number[0]) + "-" + str(number[-1]) for number in out
]
print ",".join(strings)

Suggestion : 2

Since domain name registrars organize their namespaces in different ways, it's not straight-forward to split a hostname into subdomains, the domain and top-level domains. In order to do that parse-domain uses a large list of known top-level domains from publicsuffix.org:, Splits a hostname into subdomains, domain and (effective) top-level domains. ,Splits a hostname into subdomains, domain and (effective) top-level domains., Splits a hostname into subdomains, domain and (effective) top-level domains.

import {
   parseDomain,
   ParseResultType
} from "parse-domain";

const parseResult = parseDomain(
   // This should be a string with basic latin letters only.
   // More information below.
   "www.some.example.co.uk"
);

// Check if the domain is listed in the public suffix list
if (parseResult.type === ParseResultType.Listed) {
   const {
      subDomains,
      domain,
      topLevelDomains
   } = parseResult;

   console.log(subDomains); // ["www", "some"]
   console.log(domain); // "example"
   console.log(topLevelDomains); // ["co", "uk"]
} else {
   // Read more about other parseResult types below...
}
npm install parse - domain
import {
   parseDomain,
   fromUrl
} from "parse-domain";

const {
   subDomains,
   domain,
   topLevelDomains
} = parseDomain(
   fromUrl("https://www.münchen.de?query")
);

console.log(subDomains); // ["www"]
console.log(domain); // "xn--mnchen-3ya"
console.log(topLevelDomains); // ["de"]

// You can use the 'punycode' NPM package to decode the domain again
import {
   toUnicode
} from "punycode";

console.log(toUnicode(domain)); // "münchen"
import {
   parseDomain,
   ParseResultType
} from "parse-domain";

const parseResult = parseDomain("münchen.de");

console.log(parseResult.type === ParseResultType.Invalid); // true
import {
   parseDomain,
   ParseResultType,
   Validation
} from "parse-domain";

const parseResult = parseDomain("_jabber._tcp.gmail.com", {
   validation: Validation.Lax,
});

console.log(parseResult.type === ParseResultType.Listed); // true
import {
   parseDomain,
   ParseResultType
} from "parse-domain";

const parseResult = parseDomain("192.168.2.1");

console.log(parseResult.type === ParseResultType.Ip); // true
console.log(parseResult.ipVersion); // 4

Suggestion : 3

To change the system hostname without a public DNS name,For Amazon Linux 2: Use the hostnamectl command to set your hostname to reflect the fully qualified domain name (such as webserver.mydomain.com).,For Amazon Linux 2: Use the hostnamectl command to set your hostname to reflect the desired system hostname (such as webserver).,If you have a public DNS name registered for the IP address of your instance (such as webserver.mydomain.com), you can set the system hostname so your instance identifies itself as a part of that domain. This also changes the shell prompt so that it displays the first portion of this name instead of the hostname supplied by AWS (for example, ip-12-34-56-78). If you do not have a public DNS name registered, you can still change the hostname, but the process is a little different.

In order for your hostname update to persist, you must verify that the preserve_hostname cloud-init setting is set to true. You can run the following command to edit or add this setting:

sudo vi / etc / cloud / cloud.cfg

If the preserve_hostname setting is not listed, add the following line of text to the end of the file:

preserve_hostname: true

For Amazon Linux 2: Use the hostnamectl command to set your hostname to reflect the fully qualified domain name (such as webserver.mydomain.com).

[ec2 - user~] $ sudo hostnamectl set - hostname webserver.mydomain.com

Reboot the instance to pick up the new hostname.

[ec2 - user~] $ sudo reboot

Log into your instance and verify that the hostname has been updated. Your prompt should show the new hostname (up to the first ".") and the hostname command should show the fully-qualified domain name.

[ec2 - user @webserver~] $ hostname
webserver.mydomain.com

For Amazon Linux 2: Use the hostnamectl command to set your hostname to reflect the fully qualified domain name (such as webserver.mydomain.com).

[ec2 - user~] $ sudo hostnamectl set - hostname webserver.mydomain.com

For Amazon Linux AMI: On your instance, open the /etc/sysconfig/network configuration file in your favorite text editor and change the HOSTNAME entry to reflect the fully qualified domain name (such as webserver.mydomain.com).

HOSTNAME = webserver.mydomain.com

Reboot the instance to pick up the new hostname.

[ec2 - user~] $ sudo reboot

Log into your instance and verify that the hostname has been updated. Your prompt should show the new hostname (up to the first ".") and the hostname command should show the fully-qualified domain name.

[ec2 - user @webserver~] $ hostname
webserver.mydomain.com

For Amazon Linux 2: Use the hostnamectl command to set your hostname to reflect the desired system hostname (such as webserver).

[ec2 - user~] $ sudo hostnamectl set - hostname webserver.localdomain