Input/Output & Error redirection in Kali Linux | CEHv10 (in 2020)



3
515

Learn the fantastic ways to redirecting input, output and error to somewhere else rather than the Kali Linux terminal. Also, I will make you understand the usefulness of pipe and the grep. This video tutorial is a part of Ethical Hacking, Penetration Testing with Kali Linux and Cyber Security course in which you should understand about the commands in Linux. Type echo hello. Echo is a command that is used to display a line of text. Here, I gave the command as an input through the terminal, it was executed by the system and we can see the text hello is printed on the screen as an output. But I want to redirect it to a file instead of printing it on screen. So for this we can do as, echo hello greater than operator and the file name, let’s give it as file.txt. Now, you we are not getting anything as an output on the terminal. It’s because the output is redirected to the file or it’s written on the file instead of displaying here. If we read the file using cat and the file name that is file.txt, we can see the text ‘hello’ is written there. We can also check it using the GUI. For this click on files, here you can see the file file.txt. Double click on the file to open it. The text is there. Now, again I write a line of text echo hi ! how r u ? and redirect it to the same file that is file.txt. Verify with cat and the file name. As we can see, the previous line of text is replaced with the later one. Or, the previous redirection is destroyed and the last one is done. If you want to append all input to the same file, rather than replacing its contents, you use the double greater than operator as, I do here. I give the command as echo, then write a line of text ‘Can you please give me your introduction?” then append to the file with , then give the file name that is file.txt. Verifying with cat and the file name, we can see, both the lines are there. This is how you can add multiple lines to the file instead of replacing them. This is just an example of output redirection. This technique will be very helpful to you when you want to write your output to a file while performing any scan or anything like that and you want to save the output to a file for documentation and future reference. Okay now, let’s see how to do error redirection. For error redirection demo, I’m gonna a create a user account in my Kali box. To create an user account or to add an user, we use useradd command followed by the username. I want to create a user account in the name sakriya, so I do it as useradd sakriya. Also I create password for this user and for that I use passwd command and the username that is sakriya. I give the password. I use cat to read the file /etc/passwd. The file passwd which is inside /etc/ directory, contains all the user’s information. Here, as we can see the user sakriya is there. Then I login to the user as su – sakriya. We can see just the $ symbol, because the user account has not got the home directory so. Let’s not take care about that. Now, let’s work for error redirection. I use grep –R sakriya /etc. Don’t worry about grep and what it is. We’ll learn about grep later in this video. Just take it as a command. What I am doing with this command is, I am giving a task to this user to read all files under each directory of /etc/ directory. Here, as you can see, so many things could not be executed and I see so many error messages, such as Permission Denied. But some of the tasks are performed and there are some processes without error. I want the system only to display what things were executed successfully and redirect all the error messages to a file, so that the error messages will not be displayed here on the screen, instead they will be written on a file. To do this, I run the command set as grep –R sakriya /etc/ 2, greater than symbol, /dev/null. Here, grep is the command to filter out a certain command or keyword and give result related to the keyword. –R is to search for the keyword sakriya even in the sub directory from the main directory /etc/, and redirect if any error is there to the file null which is inside the /dev directory. Now, let’s see how input can be redirected. If I run ls –l /etc/ it long lists the files and directories of the /etc directory. There are so many files and directories. If I execute ls –l /etc/ | wc, it first takes input to long list the contents of the /etc directory. Now, let’s understand the grep with piping. First of all, I run a command set using the grep for example ls –l /etc | grep host. Here, ls –l long lists the contents of the directory /etc/ and then the pipe redirects the output of this command as input to grep and grep filters out the keyword host from the input and prints stuffs having the keyword host as an output.

Published by: Network Heros Published at: 2 years ago Category: علمی و تکنولوژی