Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

JavaScript

Babel is required to process package imports. If you need a different preprocessor remove all packages first.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

HTML

              
                <div ng-app="vlsm" class="container-fluid">
	<div ng-controller="MainController" class="row">
		<div class="col-12 oculto-impresion">
			<div class="form-group">
				<label for="hosts">Escribe los hosts separados con una coma, no importa el orden</label>
				<textarea ng-change="quitarTabla()" ng-model="hosts" name="" id="hosts" cols="30" rows="1" class="form-control"></textarea></div>
			<div class="form-group">
				<label for="ip">Escribe la ip:</label>
				<input ng-change="quitarTabla()" ng-model="ip" type="text" class="form-control">
			</div>
			<div class="form-group">
				<button ng-click="calcular(hosts, ip)" class="btn btn-primary">Calcular</button>
			</div>
		</div>
		<div class="col-12 visible-impresion">
			<h1>VLSM</h1>
			<p>
				Para los hosts: <strong>{{hosts}}</strong><br>
				Usando la ip: <strong>{{ip}}</strong>
			</p>
		</div>
		<div class="col-12 oculto-impresion"  ng-show="ips">
			<button ng-click="imprimir()" class="btn btn-success">Imprimir o guardar como PDF</button><br><br>		
		</div>
		<div class="col-12" ng-show="ips">
			<table class="table table-bordered table-striped table-sm">
			  <thead>
				<tr>
				  <th>Pos.</th>
				  <th>IP</th>
				  <th>Rango</th>
				  <th>Broadcast</th>
				  <th>/Máscara</th>
				  <th>Máscara de subred</th>
				</tr>
			  </thead>
			  <tbody>
				<tr ng-repeat="ip in ips track by $index">
				  <td>{{getPosition($index)}} ({{$index + 1}})</td>
				  <td>{{ip.ip}}</td>
				  <td>{{ip.rango}}</td>
				  <td>{{ip.broadcast}}</td>
				  <td>{{ip.mascaraDiagonal}}</td>
				  <td>{{ip.mascara}}</td>
				</tr>
			  </tbody>
			</table>
		</div>
	</div>
</div>
              
            
!

CSS

              
                @media print{
	.oculto-impresion, .oculto-impresion *{
		display: none !important;
	}
	.visible-impresion{}
}
@media screen{
	.visible-impresion{
		display: none !important;
	}
}

              
            
!

JS

              
                angular
.module("vlsm", [])
.constant("POSICIONES", "primera segunda tercera cuarta quinta sexta séptima octava novena décima decimoprimera decimosegunda decimotercera decimocuarta decimoquinta".split(" "))
.constant("REGEX_IP", /^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/)
.constant("CLASE_C", {
	maximoNumeroHosts: 254,
	potenciaMinima: 2,
	potenciaMaxima: 8,
	bitsMascara: 24,
	bitsPorOcteto: 8,
	hostsPorSubred: 126
})
.controller("MainController", ["$scope","CLASE_C","REGEX_IP", "POSICIONES", ($scope, CLASE_C, REGEX_IP, POSICIONES)=>{
	$scope.imprimir = ()=>{
		window.print();
	};
	$scope.getPosition = index => POSICIONES[index];
	$scope.quitarTabla = () => {
		$scope.ips = null;
	};
	$scope.ip = "192.168.1.0";
	$scope.hosts = "50,8,21,25,5,18,4,10,5,9,5";
	$scope.obtenerPotenciaSuficiente = numeroHosts => {
		if(numeroHosts > CLASE_C.maximoNumeroHosts) throw Error("¡Más hosts de los permitidos!");
		for(let x = CLASE_C.potenciaMinima; x <= CLASE_C.potenciaMaxima; x++)
			if(Math.pow(2, x) - 2 >= numeroHosts) return x;
	};
	
	$scope.parsearIp = ip => ip.split(".");
	
	$scope.obtenerNumeroDeHostQueDeberiaUsar = (numeroHosts, potencia) => Math.pow(2, potencia);
	$scope.calcularMascara = bitsPrestados =>{
		let posiciones = [128,64,32,16,8,4,2,1], mascara = 0;
		for(let x = 0; x < bitsPrestados; x++) mascara += posiciones[x];
		return mascara;
	};
	$scope.calcular = (hosts, ip) =>{
		if(hosts && ip){
			if(!REGEX_IP.test(ip)){
				alert("Parece que no pusiste una dirección ipv4 válida");
				return;
			}
			let arregloHosts = hosts.trim().split(","), octetos = $scope.parsearIp(ip);
			if(arregloHosts.length > 0){
				//Los convertimos a enteros para después ordenarlos
				arregloHosts = arregloHosts.map(host => parseInt(host));
				
				//Comprobar si todos los hosts pueden ocupar una subred
				arregloHosts.forEach(host => {
					if(host > CLASE_C.hostsPorSubred){
						alert(`¡No puedes tener una subred con más de ${CLASE_C.hostsPorSubred} hosts!`);
						$scope.quitarTabla();
						return;
					}
				});
				
				//Comprobar si la suma no es mayor a la cantidad máxima
				let suma = arregloHosts.reduce((a, b) => a + b, 0);
				if(suma >= CLASE_C.maximoNumeroHosts + 1){
					alert(`El número total de hosts (${suma}) supera al límite de hosts para la subred (${CLASE_C.maximoNumeroHosts + 1})`);
					$scope.quitarTabla();
					return;
				}
				
				//Los ordenamos de mayor a menor
				arregloHosts = arregloHosts.sort((a,b) => b - a);
				let contador = 0, ips = [];
				arregloHosts.forEach(host => {
					if(contador >= CLASE_C.maximoNumeroHosts) {
						alert(`¡No puedes tener más de ${CLASE_C.maximoNumeroHosts} hosts en la red!`);
						$scope.quitarTabla();
						return;
					}
					let potencia = $scope.obtenerPotenciaSuficiente(host),
						numeroHosts = $scope.obtenerNumeroDeHostQueDeberiaUsar(host, potencia),
						primerosTresOctetos = `${octetos[0]}.${octetos[1]}.${octetos[2]}`,
						bitsTomados = CLASE_C.bitsPorOcteto - potencia;
					ips.push({
						ip: `${primerosTresOctetos}.${contador}`,
						rango: `${contador + 1} - ${contador + numeroHosts - 2}`,
						broadcast: `${primerosTresOctetos}.${contador + numeroHosts - 1}`,
						mascaraDiagonal: `/${CLASE_C.bitsMascara + bitsTomados}`,
						mascara: `255.255.255.${$scope.calcularMascara(bitsTomados)}`
					});
					contador += numeroHosts;
				});
				$scope.ips = ips;
			}
		}
	};
}]);
              
            
!
999px

Console