I have this project Using Linux OS I need help wirh this Lab assignment : which
ID: 3878767 • Letter: I
Question
I have this project Using Linux OS
I need help wirh this Lab assignment :
which Linux command to use for each task?
Discover the following information about the Course Server’s network connection:
Number of network devices
Name of each network device
IP address (xxx.xxx.xxx.xxx) assigned to each network device (if present)
IPv6 address (xxxx::xxxx:xx:xxxx:xxxx/64) assigned to each network device (if present)
MAC (hardware) address (xx:xx:xx:xx:x:xx) assigned to each network device
Broadcast address (xxx.xxx.xxx.xxx) assigned to each network device (if present)
Subnet Mask (xxx.xxx.xxx.xxx) assigned to each network device (if present)
In your home directory, perform the following steps
Create a folder named lab2
In the lab2 folder
Create an empty file named myPets (do NOT use a text editor)
Store the text Dog: Morris in the myPets file (do NOT open the file)
Display the contents of the myPets file (do NOT open the file)
Using the sed command, change Dog to Cat in the myPets file (do NOT open the file)
Display the contents of the myPets file (do NOT open the file)
For each month of the year, count how many files in the /bin directory have a timestamp for that month
Note 1: Perform the counts for all the months using a single command.
Note 2: The length of a month name in a timestamp is exactly three (3) letters.
Note 3: Linux is case-sensitive (Jan is not equal to jan).
Note 4: The output of the command should match the following format exactly:
Jan: x
Feb: x
Mar: x
Thanks.
Explanation / Answer
You can get the list of network devices using multiple commands . Most commonly used commands are ifconfig and ip.
You can use one of the multiple commands listed below to get the desired output.
Find the name of each device:
1. ifconfig -a | sed 's/[ ].*//;/^$/d' ( using regular expressions)
2.ifconfig -a | sed 's/[ ].*//;/^(lo|)$/d' ( command to list without local host network)
3. ip -o link show | awk '{print $2}' ( using ip command)
3. ip -o link show | awk '{print $2,$9}' ( command to print with state of the network device)
ip -o link show | awk '{print $2,$9}'
lo: UNKNOWN
eth0: UP
umber of network devices:
Append the above commands with wc -l ( word count command - counts the number of words in output)
1. ifconfig -a | sed 's/[ ].*//;/^$/d' | wc -l
2.ifconfig -a | sed 's/[ ].*//;/^(lo|)$/d' | wc -l
3. ip -o link show | awk '{print $2}' | wc -l
3. ip -o link show | awk '{print $2,$9}' | wc -l
I have 2 versions of Linux which produces slightly different formats of output as shwon below:
1. ifconfig -a
eth0 Link encap:Ethernet HWaddr 00:50:56:A5:75:77
inet addr:10.79.160.81 Bcast:10.79.160.255 Mask:255.255.255.0
inet6 addr: 2606:b400:410:b60:4000::81/64 Scope:Global
inet6 addr: fe80::250:56ff:fea5:7577/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:4834347379 errors:0 dropped:431 overruns:0 frame:0
TX packets:2424786724 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:4347779371527 (3.9 TiB) TX bytes:3272064466331 (2.9 TiB)
lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
inet6 addr: ::1/128 Scope:Host
UP LOOPBACK RUNNING MTU:65536 Metric:1
RX packets:4288 errors:0 dropped:0 overruns:0 frame:0
TX packets:4288 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:679115 (663.1 KiB) TX bytes:679115 (663.1 KiB)
2. ifconfig -a
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 10.176.225.134 netmask 255.255.254.0 broadcast 10.176.225.255
inet6 fe80::898a:d56c:73ee:85c8 prefixlen 64 scopeid 0x20<link>
ether 00:15:5d:e0:c5:03 txqueuelen 1000 (Ethernet)
RX packets 8099314 bytes 2691648164 (2.5 GiB)
RX errors 0 dropped 11 overruns 0 frame 0
TX packets 6890944 bytes 3285367296 (3.0 GiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
eth1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.100.160 netmask 255.255.255.0 broadcast 192.168.100.255
inet6 fe80::49db:a2fa:b483:c3e6 prefixlen 64 scopeid 0x20<link>
ether 00:15:5d:e0:c5:05 txqueuelen 1000 (Ethernet)
RX packets 103611781 bytes 6814141553 (6.3 GiB)
RX errors 0 dropped 42190 overruns 0 frame 0
TX packets 659 bytes 30746 (30.0 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
inet6 ::1 prefixlen 128 scopeid 0x10<host>
loop txqueuelen 0 (Local Loopback)
RX packets 9457541 bytes 2062199229 (1.9 GiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 9457541 bytes 2062199229 (1.9 GiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
virbr0: flags=4099<UP,BROADCAST,MULTICAST> mtu 1500
inet 192.168.122.1 netmask 255.255.255.0 broadcast 192.168.122.255
ether 52:54:00:f2:e8:22 txqueuelen 0 (Ethernet)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
virbr0-nic: flags=4098<BROADCAST,MULTICAST> mtu 1500
ether 52:54:00:f2:e8:22 txqueuelen 500 (Ethernet)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
Providing commands for both these formats. First command is used to extract information from 1st format whereas 2nd command is used in case of 2nd format
Find the IP address assigned to each network:
1. ifconfig -a | grep 'inet addr:' | awk '{print $2}' | cut -d: -f2
2. ifconfig -a | grep inet | grep -v inet6 | awk '{print $2}'
IPV6 address:
1. ifconfig -a | grep inet6 | awk '{print $2}'
2. ifconfig -a | grep 'inet6 addr:' | awk '{print $3}'
MAC address:
1. ifconfig -a | grep 'HWaddr' | awk '{print $NF}'
2. ifconfig -a | grep ether | awk '{print $2}'
BROADCAST:
ifconfig -a | grep Bcast | awk '{print $3}' | cut -d: -f2
ifconfig -a | grep broadcast| awk '{print $NF}'
Create folder: mkdir lab2
traverse to directory : cd lab2
Create empty file: touch myPets
Store the contents in the file: echo "Dog:Morris" >> myPets
Display contents: cat myPets
Using sed: sed 's/Dog/Cat/g' -i myPets
#cat myPets
Cat:morris
Command to count the number of files with timestamp entry of each month in a single command:
ls -al - long listing of files in a given directory
awk - extract every column or line from the output
uniq -c - Filter repeated lines and count the number of occurances of every input
ls -al | awk '{print $6}' | uniq -c
1
2 Jan
14 Oct
1 Jan
18 Oct
1 Dec
16 Oct
1 Dec
1 Jan
1 Oct
1 Nov
9 Oct
To obtain output as per required format:
awk '{printf("%s:%s ",$2,$1)}' - Obtain output as per required format. Replace first column with second and vice versa
ls -al /bin | awk '{print $6}' | uniq -c | awk '{printf("%s:%s ",$2,$1)}'
Jan:2
Oct:14
Jan:1
Oct:18
Dec:1
Oct:16
Dec:1
Jan:1
Oct:1
Nov:1
Oct:9