📄 ip2hex.pl
字号:
#! /usr/bin/perl## Convert static IP address /netmask (dot notation) to "define.h"/"ip.inc"#sub usage { print STDERR "usage: $ego [-v] [-define] [-bat] [-equ] [-binutils] x.y.z.w\n"; print STDERR " $ego [-v] [-define] [-bat] [-equ] [-binutils] \@file\n"; print STDERR " $ego [-v] [-define] [-bat] [-equ] [-binutils] - < file\n"; exit 1;}$ego = $0;$ego =~ s/.*[\/\\]//;$define = 0;$bat = 0;$equ = 0;$verbose = 0;while ($ARGV[0] =~ /^-/ && $ARGV[0] ne '-') { $opt = shift; if ($opt eq '-define') { $define = 1; } elsif ($opt eq '-bat') { $bat = 1; } elsif ($opt eq '-equ') { $equ = 1; } elsif ($opt eq '-binutils') { $binutils = 1; } elsif ($opt eq '-v') { $verbose = 1; } else { &usage; }}$ip = shift;&usage if ($ip eq '');($ip, $netmask, $gateway, $point) = &getip($ip);exit(1) if ($ip eq '');(@a) = split(/\./,$ip) ;$ip = sprintf("%d.%d.%d.%d", $a[0], $a[1], $a[2], $a[3]);(@b) = split(/\./,$netmask) ;$netmask = sprintf("%d.%d.%d.%d", $b[0], $b[1], $b[2], $b[3]);(@c) = split(/\./,$gateway) ;$gateway = sprintf("%d.%d.%d.%d", $c[0], $c[1], $c[2], $c[3]);(@d) = split(/\./,$point) ;$point = sprintf("%d.%d.%d.%d", $d[0], $d[1], $d[2], $d[3]);if ($define) { printf("#define STATIC_IP_MSB 0%02X%02Xh\t/* static IP $ip */\n", $a[0], $a[1]); printf("#define STATIC_IP_LSB 0%02X%02Xh\n", $a[2], $a[3]); printf("#define STATIC_NM_MSB 0%02X%02Xh\t/* static MASK $netmask */\n", $b[0], $b[1]); printf("#define STATIC_NM_LSB 0%02X%02Xh\n", $b[2], $b[3]); printf("#define STATIC_GW_MSB 0%02X%02Xh\t/* static GW $gateway */\n", $c[0], $c[1]); printf("#define STATIC_GW_LSB 0%02X%02Xh\n", $c[2], $c[3]); printf("#define STATIC_PT_MSB 0%02X%02Xh\t/* static PT $point */\n", $d[0], $d[1]); printf("#define STATIC_PT_LSB 0%02X%02Xh\n", $d[2], $d[3]);} elsif ($bat) { print "set IP=$ip\n"; print "set NM=$netmask\n"; print "set GW=$gateway\n"; print "set PT=$point\n";} elsif ($equ) { if ($binutils) { printf(".global\tIP_MSB, IP_LSB\n"); printf(".equ\tIP_MSB, 0x%02X%02X\t; static IP $ip\n", $a[0], $a[1]); printf(".equ\tIP_LSB, 0x%02X%02X\n", $a[2], $a[3]); printf(".global\tNM_MSB, NM_LSB\n"); printf(".equ\tNM_MSB, 0x%02X%02X\t; static MASK $netmask\n", $b[0], $b[1]); printf(".equ\tNM_LSB, 0x%02X%02X\n", $b[2], $b[3]); printf(".global\tGW_MSB, GW_LSB\n"); printf(".equ\tGW_MSB, 0x%02X%02X\t; static GW $gateway\n", $c[0], $c[1]); printf(".equ\tGW_LSB, 0x%02X%02X\n", $c[2], $c[3]); printf(".global\tPT_MSB, PT_LSB\n"); printf(".equ\tPT_MSB, 0x%02X%02X\t; static PT $point\n", $d[0], $d[1]); printf(".equ\tPT_LSB, 0x%02X%02X\n", $d[2], $d[3]); } else { printf("public\tIP_MSB, IP_LSB\n"); printf("IP_MSB\tequ\t0%02X%02Xh\t; static IP $ip\n", $a[0], $a[1]); printf("IP_LSB\tequ\t0%02X%02Xh\n", $a[2], $a[3]); printf("public\tNM_MSB, NM_LSB\n"); printf("NM_MSB\tequ\t0%02X%02Xh\t; static MASK $netmask\n", $b[0], $b[1]); printf("NM_LSB\tequ\t0%02X%02Xh\n", $b[2], $b[3]); printf("public\tGW_MSB, GW_LSB\n"); printf("GW_MSB\tequ\t0%02X%02Xh\t; static GW $gateway\n", $c[0], $c[1]); printf("GW_LSB\tequ\t0%02X%02Xh\n", $c[2], $c[3]); printf(".public\tPT_MSB, PT_LSB\n"); printf("PT_MSB\tequ\t0%02X%02Xh\t; static PT $point\n", $d[0], $d[1]); printf("PT_LSB\tequ\t0%02X%02Xh\n", $d[2], $d[3]); }} else { if ($binutils) { printf("\t.word\t0x%02X%02X\t; IP $ip\n", $a[0], $a[1]); printf("\t.word\t0x%02X%02X\n", $a[2], $a[3]); printf("\t.word\t0x%02X%02X\t; MASK $netmask\n", $b[0], $b[1]); printf("\t.word\t0x%02X%02X\n", $b[2], $b[3]); printf("\t.word\t0x%02X%02X\t; GW $gateway\n", $c[0], $c[1]); printf("\t.word\t0x%02X%02X\n", $c[2], $c[3]); printf("\t.word\t0x%02X%02X\t; PT $point\n", $d[0], $d[1]); printf("\t.word\t0x%02X%02X\n", $d[2], $d[3]); } else { printf("\tdw\t0%02X%02Xh\t; IP $ip\n", $a[0], $a[1]); printf("\tdw\t0%02X%02Xh\n", $a[2], $a[3]); printf("\tdw\t0%02X%02Xh\t; MASK $netmask\n", $b[0], $b[1]); printf("\tdw\t0%02X%02Xh\n", $b[2], $b[3]); printf("\tdw\t0%02X%02Xh\t; GW $gateway\n", $c[0], $c[1]); printf("\tdw\t0%02X%02Xh\n", $c[2], $c[3]); printf("\tdw\t0%02X%02Xh\t; PT $point\n", $d[0], $d[1]); printf("\tdw\t0%02X%02Xh\n", $d[2], $d[3]); }}if ($verbose) { print STDERR "ip: $ip mask: $netmask gw: $gateway pt: $point\n";}## Resolve IP address#sub getip { local($f) = @_; local($s); $ip = ''; if ($f =~ /^[@]/ || $f eq '-') { if ($f ne '-') { $f =~ s/^[@]//; if (!open(IP, "<$f")) { print STDERR "Can't open IP address file \"$f\"!\n"; return ''; } } while(1) { if ($f ne '-') { last if (!($s = <IP>)); } else { last if (!($s = <>)); } ##print STDERR "$s"; $s =~ s/[\s\r\n]+$//; $s =~ s/\s*[#].*$//; next if ($s eq ''); if ($s =~ /^\s*(\d+[.]\d+[.]\d+[.]\d+)[.\r\n\s\d]*$/) { $ip = $s; } } } else { $ip = $f; } ##print STDERR "IP/NM=$ip\n"; if ($ip =~ /^(\d+[.]\d+[.]\d+[.]\d+)\s+(\d+[.]\d+[.]\d+[.]\d+)\s+(\d+[.]\d+[.]\d+[.]\d+)\s+(\d+[.]\d+[.]\d+[.]\d+)$/) { return ($1, $2, $3, $4); } elsif ($ip =~ /^(\d+[.]\d+[.]\d+[.]\d+)\s+(\d+[.]\d+[.]\d+[.]\d+)\s+(\d+[.]\d+[.]\d+[.]\d+)$/) { return ($1, $2, $3, '0.0.0.0'); } elsif ($ip =~ /^(\d+[.]\d+[.]\d+[.]\d+)\s+(\d+[.]\d+[.]\d+[.]\d+)$/) { return ($1, $2, '0.0.0.0', '0.0.0.0'); } elsif ($ip =~ /^(\d+[.]\d+[.]\d+[.]\d+)$/) { return ($1, '255.255.255.0', '0.0.0.0', '0.0.0.0'); } else { print STDERR "IP address and/or netmask ($ip) not of the form x.y.z.w!\n"; return ('', ''); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -