In the use of Gearman do distributed processing, each machine needs to register a separate job as a feedback, in order to facilitate, Gearman::worker script register_function code, and then think of the use of their own IP address as job name.
So how do you get native IP as a func in a worker script?
The first approach, the simplest, calls the shell:
Copy Code code as follows:
$ip = ' ifconfig eth0|grep-oe ' ([0-9]{1,3}\.?) {4} ' |head-n 1 ';
Note: Here the input is fixed, so the simple [0-9]{1,3}, if it is in the Web program and other places to verify IP, need more rigorous!
Or:
Copy Code code as follows:
$ip = ' ifconfig eth0|awk-f: '/inet addr/{split ($2,a, "");p rint A[1];exit} ' ";
Well, it's not Perl, and it's not very good to call the external shell frequently.
The second type:
Copy Code code as follows:
Open FH, "ifconfig eth0|";
while (<FH>) {
Last Unless/inet addr: (\d{1,3}\.?) {4}) /;
print $;
}
It looks a little Perl, though essentially the same as the call shell and the grep method above.
A third, a little more Perl, reads the file purely:
Copy Code code as follows:
Open FH, ' < ', '/etc/sysconfig/network-scripts/ifcfg-eth0 ';
while (<FH>) {
Next unless/ipaddr\s*=\s* (\s+)/;
print $;
}
Further, if not necessarily the RH system, but also to read/etc/issue, to determine whether the network configuration file is/etc/sysconfig/network-script/ifcfg-eth0 or/etc/network/interfaces or other, Then write different processing methods according to the different distributions ... Well, is this going to write your own module?
Well, everyone to fully appreciate the charm of CPAN, to search, find a sys::hostip, sys::hostaddr, Net::inetface and other modules.
The fourth kind:
Copy Code code as follows:
Use SYS::HOSTADDR;
My $interface = sys::hostaddr->new (IPV => ' 4 ', Interface => ' eth0 ');
Print $interface->main_ip;
But look in the PM file, Khan, these modules are called Ifconfig command, but according to the distribution of the different packaging.
The fifth kind:
Copy Code code as follows:
Perl-mposix-msocket-e ' My $host = (uname) [1];p rint inet_ntoa (scalar gethostbyname ($host)) ';
However, there are children's shoes said, this may be due to hostname reasons, resulting in the acquisition of 127.0.0.1 ...
Then finally there is a trick. The Strace ifconfig command allows you to see that Linux is essentially a network interface IP access accomplished through the IOCTL command. So, we also use IOCTL is!
The sixth type is as follows:
Copy Code code as follows:
#!/usr/bin/perl
Use strict;
Use warnings;
Use Socket;
Require ' sys/ioctl.ph ';
Sub Get_ip_address ($) {
My $pack = Pack ("A *", shift);
My $socket;
Socket ($socket, af_inet, Sock_dgram, 0);
IOCTL ($socket, SIOCGIFADDR (), $pack);
Return Inet_ntoa (substr ($pack, 20,4));
};
Print get_ip_address ("eth0");
The advantage of this is that only the core module is invoked, and when the script is distributed, no other modules are attached.
Note: This is actually based on a network of a PY script modified
The PY version is as follows:
Copy Code code as follows:
#!/usr/bin/python
Import socket
Import Fcntl
Import struct
def get_ip_address (ifname):
s = socket.socket (socket.af_inet, socket. SOCK_DGRAM)
Return Socket.inet_ntoa (Fcntl.ioctl (
S.fileno (),
0x8915, # SIOCGIFADDR
Struct.pack (' 256s ', ifname[:15])
) [20:24])
Print get_ip_address (' eth0 ')
December 19, 2012 Increase:
Found for Logstash's input/file.rb.
Ruby version of:
Copy Code code as follows:
#!/usr/bin/ruby
Require ' socket '
siocgifaddr = 0x8915 # get PA Address
def get_ip_address (Iface)
Begin
Sock = Udpsocket.new
BUF = [Iface, ""].pack (' a16h16 ')
Sock.ioctl (SIOCGIFADDR, buf);
Sock.close
Buf[20..24].unpack ("CCCC"). Join (".")
Rescue
Nil
End
End
if $ = = __file__
Puts Get_ip_address (' eth0 ')
End
But look at the puppet or use Ifconfig method, we have time to search the relevant content.