JavaScript preprocessors can help make authoring JavaScript easier and more convenient. For instance, CoffeeScript can help prevent easy-to-make mistakes and offer a cleaner syntax and Babel can bring ECMAScript 6 features to browsers that only support ECMAScript 5.
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.
You can apply a script from anywhere on the web to your Pen. Just put a URL to it here and we'll add it, in the order you have them, before the JavaScript in the Pen itself.
If the script you link to has the file extension of a preprocessor, we'll attempt to process it before applying.
You can also link to another Pen here, and we'll pull the JavaScript from that Pen and include it. If it's using a matching preprocessor, we'll combine the code before preprocessing, so you can use the linked Pen as a true dependency.
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.
<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>
@media print{
.oculto-impresion, .oculto-impresion *{
display: none !important;
}
.visible-impresion{}
}
@media screen{
.visible-impresion{
display: none !important;
}
}
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;
}
}
};
}]);
Also see: Tab Triggers