1IO::Interface(3) User Contributed Perl Documentation IO::Interface(3)
2
3
4
6 IO::Interface - Perl extension for access to network card configuration
7 information
8
10 # ======================
11 # the new, preferred API
12 # ======================
13
14 use IO::Interface::Simple;
15
16 my $if1 = IO::Interface::Simple->new('eth0');
17 my $if2 = IO::Interface::Simple->new_from_address('127.0.0.1');
18 my $if3 = IO::Interface::Simple->new_from_index(1);
19
20 my @interfaces = IO::Interface::Simple->interfaces;
21
22 for my $if (@interfaces) {
23 print "interface = $if\n";
24 print "addr = ",$if->address,"\n",
25 "broadcast = ",$if->broadcast,"\n",
26 "netmask = ",$if->netmask,"\n",
27 "dstaddr = ",$if->dstaddr,"\n",
28 "hwaddr = ",$if->hwaddr,"\n",
29 "mtu = ",$if->mtu,"\n",
30 "metric = ",$if->metric,"\n",
31 "index = ",$if->index,"\n";
32
33 print "is running\n" if $if->is_running;
34 print "is broadcast\n" if $if->is_broadcast;
35 print "is p-to-p\n" if $if->is_pt2pt;
36 print "is loopback\n" if $if->is_loopback;
37 print "is promiscuous\n" if $if->is_promiscuous;
38 print "is multicast\n" if $if->is_multicast;
39 print "is notrailers\n" if $if->is_notrailers;
40 print "is noarp\n" if $if->is_noarp;
41 }
42
43
44 # ===========
45 # the old API
46 # ===========
47
48 use IO::Socket;
49 use IO::Interface qw(:flags);
50
51 my $s = IO::Socket::INET->new(Proto => 'udp');
52 my @interfaces = $s->if_list;
53
54 for my $if (@interfaces) {
55 print "interface = $if\n";
56 my $flags = $s->if_flags($if);
57 print "addr = ",$s->if_addr($if),"\n",
58 "broadcast = ",$s->if_broadcast($if),"\n",
59 "netmask = ",$s->if_netmask($if),"\n",
60 "dstaddr = ",$s->if_dstaddr($if),"\n",
61 "hwaddr = ",$s->if_hwaddr($if),"\n";
62
63 print "is running\n" if $flags & IFF_RUNNING;
64 print "is broadcast\n" if $flags & IFF_BROADCAST;
65 print "is p-to-p\n" if $flags & IFF_POINTOPOINT;
66 print "is loopback\n" if $flags & IFF_LOOPBACK;
67 print "is promiscuous\n" if $flags & IFF_PROMISC;
68 print "is multicast\n" if $flags & IFF_MULTICAST;
69 print "is notrailers\n" if $flags & IFF_NOTRAILERS;
70 print "is noarp\n" if $flags & IFF_NOARP;
71 }
72
73 my $interface = $s->addr_to_interface('127.0.0.1');
74
76 IO::Interface adds methods to IO::Socket objects that allows them to be
77 used to retrieve and change information about the network interfaces on
78 your system. In addition to the object-oriented access methods, you
79 can use a function-oriented style.
80
81 THIS API IS DEPRECATED. Please see IO::Interface::Simple for the
82 preferred way to get and set interface configuration information.
83
84 Creating a Socket to Access Interface Information
85 You must create a socket before you can access interface information.
86 The socket does not have to be connected to a remote site, or even used
87 for communication. The simplest procedure is to create a UDP protocol
88 socket:
89
90 my $s = IO::Socket::INET->new(Proto => 'udp');
91
92 The various IO::Interface functions will now be available as methods on
93 this socket.
94
95 Methods
96 @iflist = $s->if_list
97 The if_list() method will return a list of active interface names,
98 for example "eth0" or "tu0". If no interfaces are configured and
99 running, returns an empty list.
100
101 $addr = $s->if_addr($ifname [,$newaddr])
102 if_addr() gets or sets the interface address. Call with the
103 interface name to retrieve the address (in dotted decimal format).
104 Call with a new address to set the interface. In the latter case,
105 the routine will return a true value if the operation was
106 successful.
107
108 my $oldaddr = $s->if_addr('eth0');
109 $s->if_addr('eth0','192.168.8.10') || die "couldn't set address: $!";
110
111 Special case: the address of the pseudo-device "any" will return
112 the IP address "0.0.0.0", which corresponds to the INADDR_ANY
113 constant.
114
115 $broadcast = $s->if_broadcast($ifname [,$newbroadcast]
116 Get or set the interface broadcast address. If the interface does
117 not have a broadcast address, returns undef.
118
119 $mask = $s->if_netmask($ifname [,$newmask])
120 Get or set the interface netmask.
121
122 $dstaddr = $s->if_dstaddr($ifname [,$newdest])
123 Get or set the destination address for point-to-point interfaces.
124
125 $hwaddr = $s->if_hwaddr($ifname [,$newhwaddr])
126 Get or set the hardware address for the interface. Currently only
127 ethernet addresses in the form "00:60:2D:2D:51:70" are accepted.
128
129 $flags = $s->if_flags($ifname [,$newflags])
130 Get or set the flags for the interface. The flags are a bitmask
131 formed from a series of constants. See "Exportable constants"
132 below.
133
134 $ifname = $s->addr_to_interface($ifaddr)
135 Given an interface address in dotted form, returns the name of the
136 interface associated with it. Special case: the INADDR_ANY
137 address, 0.0.0.0 will return a pseudo-interface name of "any".
138
139 EXPORT
140 IO::Interface exports nothing by default. However, you can import the
141 following symbol groups into your namespace:
142
143 :functions Function-oriented interface (see below)
144 :flags Flag constants (see below)
145 :all All of the above
146
147 Function-Oriented Interface
148 By importing the ":functions" set, you can access IO::Interface in a
149 function-oriented manner. This imports all the methods described above
150 into your namespace. Example:
151
152 use IO::Socket;
153 use IO::Interface ':functions';
154
155 my $sock = IO::Socket::INET->new(Proto=>'udp');
156 my @interfaces = if_list($sock);
157 print "address = ",if_addr($sock,$interfaces[0]);
158
159 Exportable constants
160 The ":flags" constant imports the following constants for use with the
161 flags returned by if_flags():
162
163 IFF_ALLMULTI
164 IFF_AUTOMEDIA
165 IFF_BROADCAST
166 IFF_DEBUG
167 IFF_LOOPBACK
168 IFF_MASTER
169 IFF_MULTICAST
170 IFF_NOARP
171 IFF_NOTRAILERS
172 IFF_POINTOPOINT
173 IFF_PORTSEL
174 IFF_PROMISC
175 IFF_RUNNING
176 IFF_SLAVE
177 IFF_UP
178
179 This example determines whether interface 'tu0' supports multicasting:
180
181 use IO::Socket;
182 use IO::Interface ':flags';
183 my $sock = IO::Socket::INET->new(Proto=>'udp');
184 print "can multicast!\n" if $sock->if_flags & IFF_MULTICAST.
185
187 Lincoln D. Stein <lincoln.stein@gmail.com> Copyright 2001-2014, Lincoln
188 D. Stein.
189
190 This library is distributed under the Perl Artistic License 2.0. Please
191 see LICENSE for more information.
192
194 For feature requests, bug reports and code contributions, please use
195 the GitHub repository at https://github.com/lstein/LibIO-Interface-Perl
196
198 perl(1), IO::Socket(3), IO::Multicast(3), IO::Interface::Simple
199
200
201
202perl v5.28.0 2018-07-14 IO::Interface(3)