简体   繁体   中英

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. I have everything working except for the IP address part. 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):

$ ifconfig ens33 | awk '/inet addr/{print substr($2, 6)}'

Result:

10.10.xx.xx

inet addr: represents IPv4 address.
inet6 addr: represents IPv6 address.

这条线

IP=$(ifconfig ens33| grep inet | sed 's/ */ /'  | cut -d" " -f3)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM