给出1.1。%。%,其中%是通配符,我想遍历所有可能的IP地址。
到目前为止,我已经能够成功地用循环替换一个%,但是当我尝试替换2时,它只是用相同的数字替换了它。以下是我目前拥有的代码,对于如何放入第二个循环以获取第二个%的任何帮助,我们将不胜感激。
代码:
var wildCount = inputSt.match(/\%/g) //works out how many % are there
var newPlaceholder =''
for (var i = 0; i < wildCount.length; i++){
newPlaceHolder =inputSt.split("%").join(i)
for (var n = 0; n <=254; n++){
newPlaceholder = inputSt.split("%").join(n)
}
}
输出是1.1.0.0,然后是1.1.1.1,等等。
答案 0 :(得分:2)
此分析器版本使用递归来执行IP创建。它将对小数点进行分割,然后递归地遍历令牌以查看它们是否为%,如果是,则将它们交换为[0,tokenLimit],直到耗尽所有可能性为止。
为了不让浏览器崩溃,我将tokenLimit设置为11,而不是255。已在逻辑中添加了注释,以进行详细说明。
var test = '1.1.%.%';
var tokens = test.split( '.' );
var tokenLimit = 11;
// start the recursion loop on the first token, starting with replacement value 0
makeIP( tokens, 0, 0 );
function makeIP ( tokens, index, nextValue ) {
// if the index has not advanced past the last token, we need to
// evaluate if it should change
if ( index < tokens.length ) {
// if the token is % we need to replace it
if ( tokens[ index ] === '%' ) {
// while the nextValue is less than our max, we want to keep making ips
while ( nextValue < tokenLimit ) {
// slice the tokens array to get a new array that will not change the original
let newTokens = tokens.slice( 0 );
// change the token to a real value
newTokens[ index ] = nextValue++;
// move on to the next token
makeIP( newTokens, index + 1, 0 );
}
} else {
// the token was not %, move on to the next token
makeIP( tokens, index + 1, 0 );
}
} else {
// the index has advanced past the last token, print out the ip
console.log( tokens.join( '.' ) );
}
}
答案 1 :(得分:1)
因此,您不想通过分割'%'字符来增加数值。最好按八位字节分割:
def FindBatchSize(model):
"""#model: model architecture, that is yet to be trained"""
import os, sys, psutil, gc, tensorflow, keras
import numpy as np
from keras import backend as K
BatchFound= 16
try:
total_params= int(model.count_params()); GCPU= "CPU"
#find whether gpu is available
try:
if K.tensorflow_backend._get_available_gpus()== []:
GCPU= "CPU"; #CPU and Cuda9GPU
else:
GCPU= "GPU"
except:
from tensorflow.python.client import device_lib; #Cuda8GPU
def get_available_gpus():
local_device_protos= device_lib.list_local_devices()
return [x.name for x in local_device_protos if x.device_type == 'GPU']
if "gpu" not in str(get_available_gpus()).lower():
GCPU= "CPU"
else:
GCPU= "GPU"
#decide batch size on the basis of GPU availability and model complexity
if (GCPU== "GPU") and (os.cpu_count() >15) and (total_params <1000000):
BatchFound= 64
if (os.cpu_count() <16) and (total_params <500000):
BatchFound= 64
if (GCPU== "GPU") and (os.cpu_count() >15) and (total_params <2000000) and (total_params >=1000000):
BatchFound= 32
if (GCPU== "GPU") and (os.cpu_count() >15) and (total_params >=2000000) and (total_params <10000000):
BatchFound= 16
if (GCPU== "GPU") and (os.cpu_count() >15) and (total_params >=10000000):
BatchFound= 8
if (os.cpu_count() <16) and (total_params >5000000):
BatchFound= 8
if total_params >100000000:
BatchFound= 1
except:
pass
try:
#find percentage of memory used
memoryused= psutil.virtual_memory()
memoryused= float(str(memoryused).replace(" ", "").split("percent=")[1].split(",")[0])
if memoryused >75.0:
BatchFound= 8
if memoryused >85.0:
BatchFound= 4
if memoryused >90.0:
BatchFound= 2
if total_params >100000000:
BatchFound= 1
print("Batch Size: "+ str(BatchFound)); gc.collect()
except:
pass
memoryused= []; total_params= []; GCPU= "";
del memoryused, total_params, GCPU; gc.collect()
return BatchFound
#####################################################################################################
#####################################################################################################
您有八位字节0-3(因为它是一个数组)。然后,您可以进行递归if语句检查通配符,并随您的需要递增。
var octets = inputSt.split('.');
显然,此代码尚未完成。但这应该使您朝正确的方向前进。
提示-您要支持1-4个通配符。因此,最好创建一个递增单个八位位组的函数。如果八位位组不是带通配符的最后一个八位位组,则可以再次调用同一函数。该功能的肉在下面。我会让您弄清楚在何处以及如何执行单个增量。
for (var i = 0; i < octets.length; i++) {
if (octets[i].match('%')) {
Do some incrementing...
}
}