netmask en décimal

Pierre Bouvier

Membre actif
31 Mars 2001
559
0
Bonjour

quant on fait un ifconfig, l'ip apparait en décimal mais pas le masque. Quelqu'un sait-il comment faire apparaitre ce masque en décimal ?
 
Je suppose que c'est de cette ligne que tu parles :

inet 10.0.0.20 netmask 0xffffff00 broadcast 10.0.0.255

En bon matheux, je répondrai qu'on peut toujours faire le calcul, mais je ne suis pas sûr que cette réponse te satisfasse
cool.gif
smile.gif
...

Bob
 
gagné !

oui moi aussi je fais le calcul et puis à force on fait la correspondance.

c'est plus pour avoir quelque chose d'un peu plus clair.
 
En quelques minutes, et c'est pas très joli, pour l'interface en0

<font class="small">Code:</font><hr /><pre> #!/usr/bin/perl

$ifconf = `ifconfig en0`;

@netmask = ($ifconf =~ /netmask 0x(\w\w)(\w\w)(\w\w)(\w\w)/);

$netmask2 = '';
for (my $i = 0; $i &lt; @netmask; $i++) {
$nu = $netmask[$i];
$nu = hex($nu);
if ($i != @netmask - 1) {
$dot = '.';
} else {
$dot = '';
}
$netmask2 .= sprintf "%d$dot", $nu;
}

$ifconf =~ s/0x[\S]+/$netmask2/g;

print $ifconf;
</pre><hr />

-l
 
Bon, un peu (petit) mieux

<font class="small">Code:</font><hr /><pre> #!/usr/bin/perl

$string = `ifconfig`;


print batch_hex_ip_to_dec($string);

# batch_hex_ip_to_dec(string)
# string: string to search and replace ips for
# returns: string with ip in dec format
sub batch_hex_ip_to_dec
{
my $string = shift;
my @arr = split /(0x\w{8})/ig, $string;
for (my $i = 0; $i &lt; @arr; $i++) {
if ($arr[$i] =~ /0x\w{8}/i) {
$arr[$i] = hex_ip_to_dec($arr[$i]);
}
}
return join('', @arr);
}


# hex_ip_to_dec(ip)
# ip: string, hex format, eg 0xffffff00
# returns: string, dec format, eg 255.255.255.0
sub hex_ip_to_dec
{
my $ip = shift;
my $r= '';
my @vals = ($ip =~ /0x(\w{2})(\w{2})(\w{2})(\w{2})/i);
for (my $i = 0; $i &lt; @vals; $i++) {
$r .= hex_to_dec($vals[$i]);
unless ($i == @vals - 1) { $r .= '.'; }
}
return $r;
}

# hex_to_dec(number)
# number: hex number to convert
# returns: decimal value
sub hex_to_dec
{
return sprintf "%d", hex(shift);
}
</pre><hr />
 
Bon, un peu (petit) mieux

<font class="small">Code:</font><hr /><pre> #!/usr/bin/perl

$string = `ifconfig`;


print batch_hex_ip_to_dec($string);

# batch_hex_ip_to_dec(string)
# string: string to search and replace ips for
# returns: string with ip in dec format
sub batch_hex_ip_to_dec
{
my $string = shift;
my @arr = split /(0x\w{8})/ig, $string;
for (my $i = 0; $i &lt; @arr; $i++) {
if ($arr[$i] =~ /0x\w{8}/i) {
$arr[$i] = hex_ip_to_dec($arr[$i]);
}
}
return join('', @arr);
}


# hex_ip_to_dec(ip)
# ip: string, hex format, eg 0xffffff00
# returns: string, dec format, eg 255.255.255.0
sub hex_ip_to_dec
{
my $ip = shift;
my $r= '';
my @vals = ($ip =~ /0x(\w{2})(\w{2})(\w{2})(\w{2})/i);
for (my $i = 0; $i &lt; @vals; $i++) {
$r .= hex_to_dec($vals[$i]);
unless ($i == @vals - 1) { $r .= '.'; }
}
return $r;
}

# hex_to_dec(number)
# number: hex number to convert
# returns: decimal value
sub hex_to_dec
{
return sprintf "%d", hex(shift);
}
</pre><hr />

Bonjour, où place tu ce code ?
 
Euh... Ça date de 9 ans pratiquement. :)

SInon, ça doit se mettre dans un fichier, tout bonnement.
 
Là, je suis sur Linux. Je verrai demain ;)
 
De fait, ça marche bien. J'ai juste modifié le début pour pouvoir utiliser les mêmes paramètres que la commande ifconfig (pratique pour ne voir qu'une seule interface) :
Bloc de code:
#!/usr/bin/perl

$sCMD = "ifconfig";
foreach $argnum (0 .. $#ARGV) {
  $sCMD = "$sCMD $ARGV[$argnum]";
}

$string = `$sCMD`;

print batch_hex_ip_to_dec($string);

# batch_hex_ip_to_dec(string)
# string: string to search and replace ips for
# returns: string with ip in dec format
sub batch_hex_ip_to_dec
{
  my $string = shift;
  my @arr = split /(0x\w{8})/ig, $string;
  for (my $i = 0; $i < @arr; $i++) {
    if ($arr[$i] =~ /0x\w{8}/i) {
    $arr[$i] = hex_ip_to_dec($arr[$i]);
    }
  }
  return join('', @arr);
}


# hex_ip_to_dec(ip)
# ip: string, hex format, eg 0xffffff00
# returns: string, dec format, eg 255.255.255.0
sub hex_ip_to_dec
{
  my $ip = shift;
  my $r= '';
  my @vals = ($ip =~ /0x(\w{2})(\w{2})(\w{2})(\w{2})/i);
  for (my $i = 0; $i < @vals; $i++) {
    $r .= hex_to_dec($vals[$i]);
    unless ($i == @vals - 1) { $r .= '.'; }
  }
  return $r;
}

# hex_to_dec(number)
# number: hex number to convert
# returns: decimal value
sub hex_to_dec
{
  return sprintf "%d", hex(shift);
}