[英]Printing IP address of Nic card to bash using script
currently I am working on a school assignment on Linux using Vim where I have to write a script that displays the user that is currently logged in, the time and date, and list only the IP address of the Nic card. 目前,我正在使用Vim在Linux上进行学校作业,我必须编写一个脚本来显示当前登录的用户,时间和日期,并仅列出Nic卡的IP地址。 I have everything working except for the IP address part.
除了IP地址部分,我所有工作正常。 If anyone could help I would appreciate it greatly.
如果有人可以提供帮助,我将不胜感激。
Edit to include the code I have at the moment. 编辑以包括我目前的代码。
#!/bin/bash
Time=$(date)
IP=$(ifconfig ens33)
echo "The following user is currently logged in $USER"
echo ""
echo "The current time is $Time"
echo ""
echo "The IP information is $IP"
You can filter the result of ifconfig
using awk
like this (IPv4): 您可以使用
awk
(IPv4)过滤ifconfig
的结果:
$ ifconfig ens33 | awk '/inet addr/{print substr($2, 6)}'
Result: 结果:
10.10.xx.xx
10.10.xx.xx
inet addr:
represents IPv4 address. inet addr:
代表IPv4地址。
inet6 addr:
represents IPv6 address. inet6 addr:
代表IPv6地址。
这条线
IP=$(ifconfig ens33| grep inet | sed 's/ */ /' | cut -d" " -f3)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.