Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

UNIX/LINUX Please answer questions in their entirety . Answers are case-sensitiv

ID: 3770007 • Letter: U

Question

UNIX/LINUX

Please answer questions in their entirety. Answers are case-sensitive.

1)   What command will search the file /tmp/MCSE for the pattern “MCSE”?

2)   Write a single command line that will change the permissions on your home directory so that you and members of your group can change into it, but others cannot.

3)   What command will show the amount of free disk space on the file systems attached to the server?

4)   Read the man page for tcpdump and provide the command and accompanying options required to read the file named /tmp/tcpdump.dmp, suppress name resolution (avoid DNS lookups), and print each packet in both hex and ASCII.

5)   Write a single command line that defines the variables N=444, M=555, O as the product (multiple) of N and M, and then echoes the value of O.

6)   Write a single command line that uses netstat and grep to count all connections in a state of “ESTABLISHED” and outputs a single number representing how many connections are currently open to the server.

7)   Write a short, interactive shell script that
a.   Asks the user for a numeric variable A
b.   Asks the user for a numeric variable B
c.   Echoes the text “A minus B equals:”
d.   Prints the value of a variable C which is A minus B

8)   Write a short, interactive shell script that:
a.   Asks the user to enter an IP address
b.   Performs a host lookup on the IP address and returns a name if available.
Note: I will test this using 72.14.204.104

9)   Write a short, interactive shell script that
a.   Asks the user to enter a host name (like www.google.com)
b.   Performs a traceroute on the host and then outputs a single line stating “The host %HOSTNAME is ____ Hops Away” where the ____ is the number of hops. (I will test this using www.google.com)

Explanation / Answer

UNIX/LINUX
Please answer questions in their entirety. Answers are case-sensitive.
1) What command will search the file /tmp/MCSE for the pattern “MCSE”?
2) Write a single command line that will change the permissions on your home directory so that you and members of your group can change into it, but others cannot.
3) What command will show the amount of free disk space on the file systems attached to the server?
4) Read the man page for tcpdump and provide the command and accompanying options required to read the file named /tmp/tcpdump.dmp, suppress name resolution (avoid DNS lookups), and print each packet in both hex and ASCII.
5) Write a single command line that defines the variables N=444, M=555, O as the product (multiple) of N and M, and then echoes the value of O.
6) Write a single command line that uses netstat and grep to count all connections in a state of “ESTABLISHED” and outputs a single number representing how many connections are currently open to the server.
7) Write a short, interactive shell script that
a. Asks the user for a numeric variable A
b. Asks the user for a numeric variable B
c. Echoes the text “A minus B equals:”
d. Prints the value of a variable C which is A minus B
8) Write a short, interactive shell script that:
a. Asks the user to enter an IP address
b. Performs a host lookup on the IP address and returns a name if available.
Note: I will test this using 72.14.204.104
9) Write a short, interactive shell script that
a. Asks the user to enter a host name (like www.google.com)
b. Performs a traceroute on the host and then outputs a single line stating “The host %HOSTNAME is ____ Hops Away” where the ____ is the number of hops. (I will test this using www.google.com)


The Linux sort command
The Linux sort command can be used to sort the contents of a file in a number of ways. By default, the Linux sort command sorts the contents in alphabetical order depending on the first letter in each line. For example, the sort /etc/passwd command would sort all users by username.
Important options of the sort are
• -b (Ignores spaces at beginning of the line)
• -d (Uses dictionary sort order and ignores the punctuation)
• -f (Ignores caps)
• -i (Ignores nonprinting control characters)
• -m (Merges two or more input files into one sorted output)
• -r (Sorts in reverse order)
• -u (If line is duplicated only display once)
The Linux wc command
The Linux wc (word count) command, can return the number of lines, words, and characters in a file. Important options of the Linux wc command are
• -c (Print the byte counts)
• -m (Print the character counts)
• -l (Print the new line counts)
• -w (Print the word counts)
The Linux grep command
The Linux grep command uses a search term to look through a file. The Linux grep command can parse lines based on text or RegEx. By default the Linux grep command search for patterns case-sensitively.
Important options of the grep command
• -e (Used to specify a pattern)
• -i (Case insensitive search)
• -c (Print a count of matching lines)
• -v (invert search - Returns lines that do not match, instead of lines that match.)
• -w (Matches only when the input text consists of full words)
• -x (Should match the entire line)
• --color (Colorize output)
• -l (Instead of normal output, prints just the names of input files containing the pattern)
• -L (Prints the names of input files that contain no matches)
• -o (Prints only the text that matches, instead of the whole line of input)
• -q (Suppresses output. Useful in finding the exit status (0 for success if a match is found, 1 for no match found, 2 if the program cannot run because of an error)).
"^" character is used as anchor, to find the lines which begin which the following text. Example – “^RedHat”
"$" character is used as anchor, to find the lines which end with the preceding text. Example – "RHEL5$"
To search for lines which contains numbers, use RegEx expression range "[0-9]"
To search for lines which contain small case letters, use RegEx expression range "[a-z]"
To search for lines which contain caps letters, use RegEx expression range "[A-Z]"
Note: You should be familiar with basic Linux command to learn this lesson. Click the following link to learn basic Linux commands.
The chmod(1) command is used to change permission. The simplist way to use the chmod command is to add or subtract the permission to a file. A simple plus or minus is used to add or sumtract the permission.
You may want to prevent yourself from changing an important file. Remove the write permission of the file "myfile" with the command
chmod -w myfile
If you want to make file "myscript" executable, type
chmod +x myscript
You can add or remove more than one of these attributes at a time
chmod -rwx file
chmod +wx file
You can also use the "=" to set the permission to an exact combination This command removes the write and execute permisison, while adding the read permission:
chmod =r myfile
Note that you can change permissons of files you own. That is, you can remove all permissions of a file, and then add them back again. You can make a file "read only" to protect it. However, making a file read only does not prevent you from deleting the file. That's because the file is in a directory, and directories also have read, write and execute permission. And the rules are different. Read on.
The Linux security model is based on the one used on UNIX systems, and is as rigid as the UNIX security model (and sometimes even more), which is already quite robust. On a Linux system, every file is owned by a user and a group user. There is also a third category of users, those that are not the user owner and don't belong to the group owning the file. For each category of users, read, write and execute permissions can be granted or denied.
We already used the long option to list files using the ls -l command, though for other reasons. This command also displays file permissions for these three user categories; they are indicated by the nine characters that follow the first character, which is the file type indicator at the beginning of the file properties line. As seen in the examples below, the first three characters in this series of nine display access rights for the actual user that owns the file. The next three are for the group owner of the file, the last three for other users. The permissions are always in the same order: read, write, execute for the user, the group and the others. Some examples:

marise:~> ls -l To_Do
-rw-rw-r-- 1 marise users 5 Jan 15 12:39 To_Do
marise:~> ls -l /bin/ls
-rwxr-xr-x 1 root root 45948 Aug 9 15:01 /bin/ls*
The first file is a regular file (first dash). Users with user name marise or users belonging to the group users can read and write (change/move/delete) the file, but they can't execute it (second and third dash). All other users are only allowed to read this file, but they can't write or execute it (fourth and fifth dash).
The second example is an executable file, the difference: everybody can run this program, but you need to be root to change it.
The Info pages explain how the ls command handles display of access rights in detail, see the section What information is listed.
For easy use with commands, both access rights or modes and user groups have a code. See the tables below.
Table 3-7. Access mode codes
Code Meaning
0 or - The access right that is supposed to be on this place is not granted.
4 or r read access is granted to the user category defined in this place
2 or w write permission is granted to the user category defined in this place
1 or x execute permission is granted to the user category defined in this place
Table 3-8. User group codes
Code Meaning
u user permissions
g group permissions
o permissions for others
This straight forward scheme is applied very strictly, which allows a high level of security even without network security. Among other functions, the security scheme takes care of user access to programs, it can serve files on a need-to-know basis and protect sensitive data such as home directories and system configuration files.
The netstat command is used to query the routing table of the local host and that status of TCP/IP networking. In Solaris the command is located in the /usr/bin directory. In Linux /bin/netstat. Options are pretty much common (which is a rare thing smile emoticon

When used with the -i option, netstat displays the state of the Ethernet interfaces, with r option it displayed routing information and with -s option statistical information:
• netstat -i # the state of the Ethernet interfaces
• netstat -r # displays routing info
• netstat -s # statistical information
One of the more useful options is:
netstat -pa
The `-p` options tells it to try to determine what program has the socket open, which is often very useful info. For example, someone nmap's their system and wants to know what is using port 666 for example. Running netstat -pa will show you its satand running on that tcp port.
One of the most twisted, but useful invocations is:
netstat -a -n|grep -E "^(tcp)"| cut -c 68-|sort|uniq -c|sort -n
This will show you a sorted list of how many sockets are in each connection state. For example:
9 LISTEN
21 ESTABLISHED
The exact syntax of this command is Unix flavor dependent. In general, it can provide information on:
• Active TCP connections at this local host.
• State of all TCP/IP servers on this local host and the sockets used by them.
• Devices and links used by TCP/IP.
• The IP routing tables (gateway tables) in use at this local host.
Typical usage
• netstat -r The -r switch can be used to display TCP the routing table.
o netstat -rn same but without hostname lookup
• netstat -punta List externally connected processes:
• netstat -nap List all connected processes:
• netstat -s Show network statistics:
• netstat -a -i eth0 Kernel interface table info
• # netstat -tap |grep LISTEN
*:exec *:* LISTEN 988/inetd
*:login *:* LISTEN 988/inetd
*:shell *:* LISTEN 988/inetd
*:printer *:* LISTEN 988/inetd
*:time *:* LISTEN 988/inetd
*:x11 *:* LISTEN 1462/X
*:http *:* LISTEN 1078/httpd
bigcat:domain *:* LISTEN 956/named
bigcat:domain *:* LISTEN 956/named
*:ssh *:* LISTEN 972/sshd
*:auth *:* LISTEN 388/in.identd
*:telnet *:* LISTEN 988/inetd
*:finger *:* LISTEN 988/inetd
*:sunrpc *:* LISTEN 1290/portmap
*:ftp *:* LISTEN 988/inetd
*:smtp *:* LISTEN 1738/sendmail: accepting connections
*:694 *:* LISTEN 1319/rpc.mountd
*:netbios-ssn *:* LISTEN 422/smbd

More extended list of typical idioms can be found at Sun Microsystems - BigAdmin Shell Commands:]
o netstat -a | grep EST | wc -l # Displays number active established connections to the localhost
o netstat -a | more /* Show the state of all the sockets on a machine */
o netstat -i /* Show the state of the interfaces used for TCP/IP traffice */
o netstat -k hme0 # Undocumented netstat command */
o netstat -np # Similar to arp -a without name resolution */
Displaying the Routing Table
netstat with the –r option displays the kernel routing table in the way similar to parameters supplied to the route command. For example on Red Hat linux 5.6 nestat -rn will display:
# netstat -rn
Kernel IP routing table
Destination Gateway Genmask Flags MSS Window irtt Iface
99.89.234.138 44.29.2.1 255.255.255.255 UGH 0 0 0 eth1
10.201.44.100 44.29.1.3 255.255.255.255 UGH 0 0 0 eth0
44.29.129.2 44.29.1.3 255.255.255.255 UGH 0 0 0 eth0
10.201.13.251 10.193.5.1 255.255.255.255 UGH 0 0 0 eth1
64.44.240.26 44.29.2.1 255.255.255.255 UGH 0 0 0 eth1
99.89.234.134 44.29.2.1 255.255.255.255 UGH 0 0 0 eth1
64.44.240.27 44.29.2.1 255.255.255.255 UGH 0 0 0 eth1
44.29.129.0 44.29.2.1 255.255.255.0 UG 0 0 0 eth1
44.29.2.0 0.0.0.0 255.255.255.0 U 0 0 0 eth1
44.29.1.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0
10.193.5.0 0.0.0.0 255.255.255.0 U 0 0 0 eth1
10.201.145.0 44.29.2.1 255.255.255.0 UG 0 0 0 eth1
69.254.0.0 0.0.0.0 255.255.0.0 U 0 0 0 eth0
10.0.0.0 10.193.5.1 255.0.0.0 UG 0 0 0 eth1
127.0.0.0 0.0.0.0 255.0.0.0 U 0 0 0 lo
0.0.0.0 10.193.5.1 0.0.0.0 UG 0 0 0 eth1
The –n option suppresses resolution of hostnames and displays all IPs in numeric form, rather than the symbolic host and network names.
The second column shows the gateway to which the routing entry points. If no gateway is used, an asterisk is printed with -n option and 0.0.0.0 with the -rn option.
The third column shows the “generality” of the route, i.e., the network mask for this route. Routing table displayed by netstat is sorted in reverse length of the netmask order. Host routes have netmask 255.255.255.255 and displayed first.
When given an IP address to find a suitable route for, the kernel iterates through each entry of the routing table taking the bitwise AND of the address and the genmask before comparing it to the target of the route.
The fourth column displays the following flags that describe the route:
• G -- The route uses a gateway.
• U -- The interface to be used is up.
• H -- Only a single host can be reached through the route. In this case Genmask column is always 255.255.255.255
• D -- This route is dynamically created. It is set if the table entry has been generated by a routing daemon like gated or by an ICMP redirect message
• M -- This route is set if the table entry was modified by an ICMP redirect message.
• ! -- The route is a reject route and datagrams will be dropped.
The next three columns show the MSS, Window and irtt that will be applied to TCP connections established via this route. The MSS is the Maximum Segment Size and is the size of the largest datagram the kernel will construct for transmission via this route. The Window is the maximum amount of data the system will accept in a single burst from a remote host. The acronym irtt stands for “initial round trip time.” The TCP protocol ensures that data is reliably delivered between hosts by retransmitting a datagram if it has been lost. The TCP protocol keeps a running count of how long it takes for a datagram to be delivered to the remote end, and an acknowledgement to be received so that it knows how long to wait before assuming a datagram needs to retransmitted; this process is called the round-trip time. The initial round-trip time is the value that the TCP protocol will use when a connection is first established. For most network types, the default value is okay, but for some slow networks, notably certain types of amateur packet radio networks, the time is too short and causes unnecessary retransmission. The irtt value can be set using the route command. Values of zero in these fields mean that the default is being used.
Finally, the last field displays the network interface that this route will use.


#!/bin/bash

first_num=0
second_num=0

echo -n "Enter the first number --> "
read first_num
echo -n "Enter the second number -> "
read second_num

echo "first number + second number = $((first_num + second_num))"
echo "first number - second number = $((first_num - second_num))"
echo "first number * second number = $((first_num * second_num))"
echo "first number / second number = $((first_num / second_num))"
echo "first number % second number = $((first_num % second_num))"
echo "first number raised to the"
echo "power of the second number = $((first_num ** second_num))"



#!/bin/bash
# Version 1
read -p "Enter any Internet address name : " domainname
host "${domainname}"


#!/bin/bash
# Version 1
read -p "Enter any Internet domain name : " domainname
nslookup "${domainname}"