Linux: Find my IP address using Perl at a shell prompt
Q. How do I find out my IP address assigned to eth0 or ra0 interface using perl?
A. If you need to know the IP address of the UNIX / Linux machine you are running on, use the following perl one liner. Perl don't have any inbuilt facility but combination of ifconfig command ans shell pipes you can craft something as follows to display your system IP address:
Find my IP address using Perl One liner and shell pipes
Type the following command at a shell prompt:
ifconfig -a | perl -ne 'if ( m/^\s*inet (?:addr:)?([\d.]+).*?cast/ ) { print qq($1\n); exit 0; }'
Subscribe to our free e-mail newsletter or RSS feed to get all updates.
You can Email this page to a friend.
Related Linux / UNIX FAQ:
- Sending mail with Perl mail script
- How do I install a Perl Module?
- Perl sprintf: How do I use sprintf in a perl script?
- FreeBSD install Perl language
- Locate files on linux, FreeBSD and UNIX system
Discussion on This FAQ
Leave a Reply
We encourage your comments, and suggestions. But please stay on topic, be polite, and avoid spam. Please do not use the comment form to ask for help / question. Ask your question on the excellent Linux tech support forum. Thank you very much for stopping by our site!
Tags: combination, find_my_ip_address, inet_addr, interface, linux_machine, my_ip_address, Perl, pipes, qq, shell, unix_linux ~ Last updated on: October 18, 2007



October 23rd, 2007 at 1:26 pm
thanks really helpful!
November 7th, 2007 at 10:42 pm
For non-root users, put /sbin/ in front of the ifconfig line, so it becomes
/sbin/ifconfig -a | perl -ne 'if ( m/^\s*inet (?:addr:)?([\d.]+).*?cast/ ) { print qq($1\n); exit 0; }’