Perl ã§ãã¼ã«ã«ã®ã¢ãã¬ã¹ãåå¾ãã
ã¾ã Linux ã触ãå§ãã¦éããªãé ã«ããµã¼ãã¼ãæ§ç¯ãã¦ãã¦ãã¼ã«ã«ã® IP ã¢ãã¬ã¹ãã·ã§ã«ã¹ã¯ãªããããå©ç¨ããå¿ è¦ããã£ã¦ãã©ããã£ã¦åå¾ããã¹ãã ãããã¨å°ä¸æéæ©ãã ã®ã§ããçµå±åããã Perl ã®æ£è¦è¡¨ç¾ã§ ifconfig ã parse ãããã¨ãããã¾ãããioctl() ã使ã£ã¦ããã¤ã¹ãæä½ããå¿ è¦ããããã¨ãããã¨ãç¥ã£ãã®ã¯æ°å¹´å¾ãå²ã¨æè¿ã®ãã¨ã§ãããªãã¨ãããã¨ã§ãããã
ã§ã¯ãPerl 㧠IP ã¢ãã¬ã¹ãåå¾ããå ´åã§ããã¢ã¸ã¥ã¼ã«ã使ã£ã¦ããã®ã§ããã° IO::Interface ãããã ããã¨æã£ã¦ãã¾ããIO::Interface 㯠Pure Perl ã§ã¯ããã¾ããããXS 㧠ioctl() ãå¼ã³åºãã¦ããã®ã§æ¯è¼çé«éä¸ã¤ç´ ç´ãªå®è£ ã ã¨æãã¾ãã
#!/usr/local/bin/perl use strict; use warnings; use IO::Handle; use IO::Interface::Simple; STDOUT->printf("%s\n", IO::Interface::Simple->new(shift || 'eth0')->address);
ã§
% perl ip_address.pl eth0 192.168.100.2
ã¨åºã¾ãã
ãã®ã¢ã¸ã¥ã¼ã«ã¯ã»ã¼ãioctl() ã«ãããããã¯ã¼ã¯ã¤ã³ã¿ãã§ã¼ã¹æä½ã®ã©ãããã¨ããä½ç½®ã¥ãã§ãã®ã§ããã¼ãã¦ã§ã¢ã¢ãã¬ã¹ãã®ã»ãã®æ å ±ãåå¾ã§ãã¾ããä½è 㯠CGI.pm ã Perlãããã¯ã¼ã¯ããã°ã©ãã³ã°âã½ã±ããã®ä½¿ãæ¹ããã¯ã©ã¤ã¢ã³ã/ãµã¼ãã¼ã·ã¹ãã ã®éçºã¾ã§ ã§ããªãã¿ã® Lincoln D. Stein æ°ã§ãã
ä»ã«ã CPAN ã«ã¯ Net::Addresss::Ethernet ãªã©ãããã¾ããããã¡ã㯠Net::Ifconfig::Wrapper ã使ã£ã¦ ifconfig ã®åºåã parse ãã¦ãã¾ããOS ãã¨ã®åºåã®éããªã©ãå¸åããããã«çµæ§æ³¥èãã³ã¼ãã«ãªã£ã¦ããããã§ããXS ã§ãåé¡ãªãç°å¢ã§ããã° IO::Interface ã®ã»ããè¯ãã§ãããã
PerlãEmacsã®ãããªãªãã§ãã§ãããã¼ã«ã¯åã£ã¦å½ç¶ãsedã§åã¤ã¨ãããééå³ã§ããä¾ãã¦è¨ãã°ãæ¿ããã§ãã¼ã«ã«ãªãå¤åçã°ãã使ã£ã¦æã¡åãã®ãPerlãEmacsã§ãã¹ãã¬ã¼ãä¸æ¬ãã¹ãã©ã¤ã¯åè² ã§æã¡åãã®ãsedã®ä¸çã§ãã
æ¿çªããã³ãã¬ã¼ã¹ã¯å°å¦çã®é ã«ãã£ã¦ä»¥æ¥ãªã®ã§ã»ã¼è¨æ¶ã®å½¼æ¹ã§ãããããã§ã¯æç¡ãè¨ãããã¹ã©ã¤ãã¼ãã®ãããªæ¹æ³ãç´¹ä»ãã¦ã¿ã¾ããã
追è¨
Linux ãªã sysfs 㧠/sys/class/net/eth0 ãããåãããã¨ã³ã¡ã³ããããã ãã¾ãããç¥ããªãã£ã! sysfs çµç±ã§ã¯ IP ã¢ãã¬ã¹ã¯åããªã?