Sunday 27 May 2018

Linux Fundamentals for Networking- Part 2

SORT:
    • 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)
$ps | sort -k 3
  • Sorts the list of process in order of the 3rd column
$ cat text1.log text2.log | sort -k 1
  • Sorts the cat of two files on the first letter of each line

WC:
    • 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 byte counts)
-l (Print the new line counts)
-w (Print the word counts)

More and Less Command:
  • The “more” command is used to view text output of commands or files one page at a time. When the user is ready for the next block of text, they press the space bar.
  • Less extends the functionality of more by adding forward and backwards movement, the ability to search multiple files, view gzipped files without uncompressing them first, set marks for navigation and can invoke an editor when viewing files.

Head and Tail Command:
  • tail prints the last 10 lines of each FILE to standard output.: tail myfile.txt
    • To print last 100 lines, we can use tail myfile.txt -n 100
  • head, by default, prints the first 10 lines of each FILE to standard output.

Help for Linux Commands:
  • Man: If you don’t know the exact command name for a specific functionality, use man -k option to locate the command.
$ man UNIXCOMMAND
  • Whatis: To display a single line description about the command, you can use whatis command.
$ whatis cron
  • cron (8)         - daemon to execute scheduled commands (Vixie Cron)
  • Use -h or –help option of the command itself: Sometimes you may know the functionality of a command very well, but cannot recollect all the available options for a specific command. Use -h option of the command to review all available options of the command.
$ UNIXCOMMAND -h
  • Info: When you are not able to find the required information from the Unix man page, try the info documents using the Unix info command as shown below.
$ info UNIXCOMMAND

Different Ways to see the running processes:
Ps
Top
  • Works like PS only
  • But, it is Real-time
  • Lists out CPU percentage usage also- so we can check whether our foreground process is only taking more memory
Memory Leak:
At the end of activity, application must release the memory
But, if the releasing doesn’t happen correctly, then, memory accumulation happens over a long time which results in a leak.
Due to this, memory starvation happens for other process

To see memory, 
show version (EOS command to show RAM memory)
Go to bash and then $df  or $df -h(df=diskspace for filesystem. Used to see flash and also all other filesystem mounts)

To see the agent logs, 
Go to bash and then $/var/log/agents and then $ls
It shows the log for currently running process

To see system logs,
Go to $/var/log/messages


OTHER COMMANDS:
System Crash dump is mounted in /var/core . It is a complete mirror image of system.
If any agent log becomes very large, we compress it using tar, gz, zip, etc

If we want to get more space, we clear varcore and other logs

To search for files, we use find command (it is important). For example, we can use find to find the debug.log file. 

$History command is used to see what we have executed on the shell (everyone’s session commands also shown as it is linux). It is linux command but it is also present in EOS CLI (due to linux integration) as #show history. It lists only of our current session, not everyone.

Environment Variables - The binaries are stored at a specific location and whenever a process asks for the file, it tells to look at the specific location using environment variables. To see all env variables, use $printenv
  • $echo $PATH where PATH is an environment variable name and echo is just to print
  • $TEMP – location where processes can store temporary files
  • $HOME - indicate where a user's home directory is located in the file system.
  • $PWD- show present working directory
  • $SHELL contains the path to the user's preferred shell
  • $TERM contains the type of the running terminal, e.g. xterm-256color.
  • $VISUAL contains command to run the full-fledged editor that is used for more demanding tasks, such as editing mail (e.g., vi, vim, emacs etc).
  • https://wiki.archlinux.org/index.php/environment_variables

BASHRC file:
  • Located in user’s home directory
  • .bashrc is a shell script that Bash runs whenever it is started interactively. 
  • You can edit it to set our environment variables
  • For example, we can set aliases in .bashrc for very long commands. It works only for us.
  • Note that alias works in EOS also and here it works for everyone

Installing Packages:
  • Fedora uses YUM Package Manager
  • Here, the package is RPM format
  • Inside Linux, everything is managed as a file including processes, devices, drivers, folders, etc…So, we can vi anything
  • Install using the command in bash: $rpm -i <package>.rpm

Background and Foreground Process:
  • Unlike with a foreground process, the shell does not have to wait for a background process to end before it can run more processes.
  • To run a command as a background process, type the command and add a space and an ampersand to the end of the command. For example:
$ command1 &
Here, While that is running in the background, the shell prompt will return.
  • To start a foreground process, enter a command at the prompt, e.g.,
$ command1
Here, the next prompt will not appear until command1 finishes running.
We can see routing tables in Linux using route or netstat -rn. It is called as Kernel Route Table and the routes are called bash routes
EOS maintains its own route tables.
Loopback Address:
  • IPC mechanisms like sockets and pipes use this
  • Used by developers to test their applications. For example, two applications on same machine can open a socket to loopback address and read data b/w them.
  • Testing the IP stack
  • Inside EOS, loopback address is a unique identifier for each switch
Currently, we have 550k routes on the internet
Tcpdump:
  • It is used to capture packets. (It can be used even from EOS and the command varies slightly)
  • For single interface, $tcpdump -i et 1/1
  • To write the output to a file, $tcpdump -i ma 1 -w /mnt/flash/filename.pcap
  • To see the capture for all interfaces, $sudo tcpdump
  • To listen to the traffic before it even goes to the CPU, $tcpdump -i fabric. Used for seeing vlan tagging.
To copy a file from linux to laptop.
  • $scp filename.pcap admin@switch1:/users/admin/Desktop
  • Note that the ‘:’ after ip address is compulsory in linux and not needed in EOS
To copy a file from laptop to linux,
  • $scp admin@my-laptop:/Users/admin/Desktop/filename.pcap .
  • The ‘.’ as the destination means it will copy the file to the current directory in linux
Accessing switch using ftp instead of ssh and copy the file to laptop:
  • admin$sftp root@switch1
sftp> ls
sftp> get filename.pcap
sftp> exit
admin$ ls | grep filename.pcap
  • The file will be there in our computer .ie. we transfered the file from switch to laptop using ftp
To connect to a local switch instead of remote switch, 
  • We first configure our ip address manually to the same subnet as the switch
192.168.1.2 and 255.255.255.0 where the ‘*.*.*.2’ is the port number on the switch
  • Then, we $ssh admin@192.168.1.100 using your password. Ie. we connect to the management port of the switch

No comments:

Post a Comment