-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathethalias.pl
executable file
·99 lines (79 loc) · 1.67 KB
/
ethalias.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#!/usr/bin/perl -w
### Easy way to create eth alias for Centos/RHEL
# Author: Tryggvi Farestveit <tryggvi@ok.is>
# License: GPLv2
use strict;
use Getopt::Long;
## Settings
my $base = "/etc/sysconfig/network-scripts";
## Global variables
my ($o_verb, $o_help, $o_eth, $o_ip, $o_mask);
## Funtions
sub check_options {
Getopt::Long::Configure ("bundling");
GetOptions(
'v' => \$o_verb, 'verbose' => \$o_verb,
'h' => \$o_help, 'help' => \$o_help,
'e:s' => \$o_eth, 'eth:s' => \$o_eth,
'i:s' => \$o_ip, 'ip:s' => \$o_ip,
'm:s' => \$o_mask, 'mask:s' => \$o_mask,
);
if(defined ($o_help)){
help();
exit 1;
}
if(!defined($o_ip) || !defined($o_mask) || !defined($o_eth)){
help();
exit 1;
}
}
sub CreateAlias($$$){
my ($eth, $ip, $mask) = @_;
my @IpSplit = split("[\.]", $ip);
my $IpLastPart = $IpSplit[3];
my $NewEth = $eth.":".$IpLastPart;
my $filename = $base."/"."ifcfg-".$NewEth;
if(-e $filename){
print "$filename already exists\n";
exit 1;
} else {
print "Writing to $filename\n";
open(C, ">$filename");
print C <<EOF;
DEVICE=$NewEth
BOOTPROTO=none
NM_CONTROLLED=yes
ONBOOT=yes
TYPE=Ethernet
IPADDR=$ip
NETMASK=$mask
USERCTL=no
EOF
close(C);
}
}
sub help() {
print "$0\n";
print <<EOT;
-e ETH, --eth ETH
Base nic name ex. eth0
-i IP, --ip IP
IP address
-m MASK, --netmask MASK
Netmask
-v, --verbose
print extra debugging information
-h, --help
print this help message
EOT
}
sub print_usage() {
print "Usage: $0 [-v] ]\n";
}
## Main
check_options();
if(!-e $base){
print "Directory $base does not exists\n";
exit 1;
}
CreateAlias($o_eth, $o_ip, $o_mask);