CyberKeeda In Social Media
Showing posts with label Linux Cheat Codes. Show all posts
Showing posts with label Linux Cheat Codes. Show all posts

How to scan IP addresses details on your network using NMAP

 


You know using Linux is a kind fun, think about a requirement and you can see a wide number opensource tools gives you wings to your idea, no hurdles just go with your goal, they all will support you..

I would like to share you, what made me search internet and write this blog post.

So within my Lab environment, it's a very frequent task to configure, update IP configuration of other virtual machines, so to tackle this task, I have already written an Ansible Role, which basically configures the IP address for the host which has existing dhcp address assigned to it.

Now still there are some information I need to provide ansible before I proceed to run the playbook and the information it needs is, I need to manually look for free IPs in my current network.

So I was curious how to scan my network for used and free IP addresses, thus I surfed the internet and found, my friendly network troubleshooting tool NMAP gives the insight about it.

Let's see what command can be used to find those details.

Using below one lines to search for used IPs within your network.

$ nmap -sP 192.168.29.0/24

Output


Starting Nmap 6.40 ( http://nmap.org ) at 2022-06-16 17:10 IST
Nmap scan report for 192.168.29.1
Host is up (0.0078s latency).
Nmap scan report for 192.168.29.9
Host is up (0.0050s latency).
Nmap scan report for 192.168.29.21
Host is up (0.0043s latency).
Nmap scan report for 192.168.29.30
Host is up (0.0015s latency).
Nmap done: 256 IP addresses (4 hosts up) scanned in 2.59 seconds

Now let's scan again the same network and look for the listening ports along with the host ip

$ sudo nmap -sT 192.168.29.0/24

Output

Starting Nmap 6.40 ( http://nmap.org ) at 2022-06-16 17:17 IST
Nmap scan report for 192.168.29.1
Host is up (0.0061s latency).
Not shown: 992 filtered ports
PORT     STATE  SERVICE
80/tcp   open   http
443/tcp  open   https
1900/tcp open   upnp
2869/tcp closed icslap
7443/tcp open   oracleas-https
8080/tcp open   http-proxy
8200/tcp closed trivnet1
8443/tcp open   https-alt
MAC Address: AA:HA:IC:PF:P3:C1 (Unknown)

Nmap scan report for 192.168.29.9
Host is up (0.0083s latency).
Not shown: 998 closed ports
PORT    STATE SERVICE
80/tcp  open  http
554/tcp open  rtsp
MAC Address: 14:07:o8:g5:7E:99 (Private)

Nmap scan report for 192.168.29.21
Host is up (0.0051s latency).
Not shown: 998 closed ports
PORT   STATE SERVICE
22/tcp open  ssh
80/tcp open  http
MAC Address: 08:76:20:00:75:D5 (Cadmus Computer Systems)

Nmap scan report for 192.168.29.25
Host is up (0.0057s latency).
Not shown: 999 filtered ports
PORT    STATE SERVICE
135/tcp open  msrpc
MAC Address: F0:76:30:60:8E:21 (Unknown)

Nmap scan report for 192.168.29.30
Host is up (0.0018s latency).
Not shown: 997 closed ports
PORT     STATE SERVICE
22/tcp   open  ssh
8000/tcp open  http-alt
8080/tcp open  http-proxy

Nmap done: 256 IP addresses (5 hosts up) scanned in 7.84 seconds

If you need additional details like Host OS details and some more, then run the scan again with below command

$ sudo nmap -sT -O 192.168.29.0/24

Output

Nmap scan report for 192.168.29.30
Host is up (0.00026s latency).
Not shown: 997 closed ports
PORT     STATE SERVICE
22/tcp   open  ssh
8000/tcp open  http-alt
8080/tcp open  http-proxy
Device type: general purpose
Running: Linux 3.X
OS CPE: cpe:/o:linux:linux_kernel:3
OS details: Linux 3.7 - 3.9
Network Distance: 0 hops

Hope this post will help you in some sort !
Read more ...

How to remove last character from the last line of a file using SED

 

This could be very relatable hack for you as we all are dealing with JSON object now a days, and during automation using bash aka shell scripts, we may need to parse our json data.

Okay so here is the data, and what I have

$ cat account_address.txt

"59598532c58EBeB13A70a37159F0C3AB2e0aB623": { "balance": "10000" },
"A281753296De2A35c2Ae6D613b317b71F76F6aE2": { "balance": "10000" },
"2eAc363b2ffAfbc9b5dE9E2004057a778313d4Ac": { "balance": "10000" },
"3FD7893E53D35A93A240Be3B4112A24746F8d858": { "balance": "10000" },
"dfd46B5F7B194133C48562d84A970358E13d64f7": { "balance": "10000" },
"8F3D701F3963d41935C4D2FeeFb3E072FBc613Ee": { "balance": "10000" },
And  here is the data, and what I need.
$ cat account_address.txt

"59598532c58EBeB13A70a37159F0C3AB2e0aB623": { "balance": "10000" },
"A281753296De2A35c2Ae6D613b317b71F76F6aE2": { "balance": "10000" },
"2eAc363b2ffAfbc9b5dE9E2004057a778313d4Ac": { "balance": "10000" },
"3FD7893E53D35A93A240Be3B4112A24746F8d858": { "balance": "10000" },
"dfd46B5F7B194133C48562d84A970358E13d64f7": { "balance": "10000" },
"8F3D701F3963d41935C4D2FeeFb3E072FBc613Ee": { "balance": "10000" }

Using SED one liner, we can do this stuff.

$ cat account_address.txt | sed '$ s/.$//'


That's it !

Read more ...

Linux : Create dummy file of any size for test purpose using fallocate

Requirement :
Create dummy/fake file of desired size using Linux terminal
    There might be some test requirement to mimic the prod setup, in my case I have to write a script to validate files before downloading, instead of making request to prod site I just created a local setup and placed similar dummy files to replicate the prod environment.
Let's know the one linux one liner using tool fallocate to create dummy file of desired size.

  • Fallocate is the Linux terminal utility.

One Liner
# fallocate -l <size_of_file> <desired_name_of_file>

  • Fallocate is the Linux terminal utility.

Syntax Template

# fallocate -l 15M myfile.img

  • <size_of_file >
    • in GB and MB : M for MB and G for GB
    • example : 15M for 15 MB and 5G for 5GB
  • <desired_name_of_file> 
    • It can be anything with our without extension : myfile.tar or myfile.img or myfile or anything

Using Linux for loop and Seq to generate a series of file.
Example : 
  • Below one liner can be used to generate 100 files of 15mb each with suffix changes as file count number.

Syntax Template

# for val in `seq 100`; do fallocate -l 15M demo060621000$val.tar; done;

Output : File starts with name 
demo0606210001.tar and ends with demo0606210099.tar

Syntax Output

# ls -ltr

-rw-r--r-- 1 root root 15728640 Jun 6 23:25 demo0606210001.tar -rw-r--r-- 1 root root 15728640 Jun 6 23:25 demo0606210002.tar -rw-r--r-- 1 root root 15728640 Jun 6 23:25 demo0606210003.tar -rw-r--r-- 1 root root 15728640 Jun 6 23:25 demo0606210004.tar -rw-r--r-- 1 root root 15728640 Jun 6 23:25 demo0606210005.tar -rw-r--r-- 1 root root 15728640 Jun 6 23:25 demo0606210006.tar -rw-r--r-- 1 root root 15728640 Jun 6 23:25 demo0606210007.tar -rw-r--r-- 1 root root 15728640 Jun 6 23:25 demo0606210008.tar -rw-r--r-- 1 root root 15728640 Jun 6 23:25 demo0606210009.tar -rw-r--r-- 1 root root 15728640 Jun 6 23:25 demo06062100010.tar -rw-r--r-- 1 root root 15728640 Jun 6 23:25 demo06062100011.tar -rw-r--r-- 1 root root 15728640 Jun 6 23:25 demo06062100012.tar -rw-r--r-- 1 root root 15728640 Jun 6 23:25 demo06062100013.tar -rw-r--r-- 1 root root 15728640 Jun 6 23:25 demo06062100014.tar -rw-r--r-- 1 root root 15728640 Jun 6 23:25 demo06062100015.tar -rw-r--r-- 1 root root 15728640 Jun 6 23:25 demo06062100016.tar -rw-r--r-- 1 root root 15728640 Jun 6 23:25 demo06062100017.tar -rw-r--r-- 1 root root 15728640 Jun 6 23:25 demo06062100018.tar -rw-r--r-- 1 root root 15728640 Jun 6 23:25 demo06062100019.tar -rw-r--r-- 1 root root 15728640 Jun 6 23:25 demo06062100020.tar -rw-r--r-- 1 root root 15728640 Jun 6 23:25 demo06062100021.tar -rw-r--r-- 1 root root 15728640 Jun 6 23:25 demo06062100022.tar -rw-r--r-- 1 root root 15728640 Jun 6 23:25 demo06062100023.tar

Read more ...

Linux - Count no of files in a folder by day.

 


One Liner to count the number of files in a directory by date.

We often got this requirement where we have to deal with file counts, we have a huge list of files within a folder and we want to count the number of files created by date.

One Liner Linux CLI command

# find . -type f -printf '%TY-%Tm-%Td\n' | sort | uniq -c

 
Output for above command will look something like below.

1 2019-07-03 1 2019-08-08 6 2019-08-13 1 2019-08-15 1 2019-09-10 2 2019-09-11 1 2019-09-23 1 2019-10-22 1 2019-10-25 1 2019-10-29 1 2019-12-05 1 2020-03-04 2 2020-03-30 1 2020-04-07 11 2020-04-08 2 2020-04-09 1 2020-04-21 1 2020-04-26 2 2020-04-30 430 2020-05-06 1 2020-05-20 4 2020-05-26 951 2020-07-01 434 2020-07-02 1 2020-07-05 2 2020-07-06 100 2020-07-15 1 2020-07-28 6 2020-07-29 1 2020-08-01 2 2020-09-03


Let's break out the command and understand one by one highlighted in pale Yellow.

find . -type f

# find . -type f -printf '%TY-%Tm-%Td\n' | sort | uniq -c

Find . -type f 

Find command will fetch only the files ( -type f ) within present directory ( . )


-printf '%TY-%Tm-%Td\n'

# find . -type f -printf '%TY-%Tm-%Td\n' | sort | uniq -c

-printf '%TY-%Tm-%Td\n' will prints the modification time of files in e.g. 2020-04-26 format

sort | uniq -c
# find . -type f -printf '%TY-%Tm-%Td\n' | sort | uniq -c

sort : It will sorts the output 
uniq -c : It will count the sorted output by date.


Feel free to use it,  thank me later ;)

Read more ...

BASH Shell Scripting Cheat Sheet : Part 1




Commonly used UNIX Commands!!

Cheat sheet to use within your shell scripts


1.How to display the 10th line of a file?
head -10 filename | tail -1
2. How to remove the header from a file?
sed -i '1 d' filename
3. How to remove the footer from a file?
sed -i '$ d' filename
4. Write a command to find the length of a line in a file?
The below command can be used to get a line from a file.
sed –n '<n> p' filename
We will see how to find the length of 10th line in a file
sed -n '10 p' filename|wc -c
5. How to get the nth word of a line in Unix?
cut –f<n> -d' '
6. How to reverse a string in unix?
echo "java" | rev
7. How to get the last word from a line in Unix file?
echo "unix is good" | rev | cut -f1 -d' ' | rev
8. How to replace the n-th line in a file with a new line in Unix?
sed -i'' '10 d' filename       # d stands for delete
sed -i'' '10 i new inserted line' filename     # i stands for insert
9. How to check if the last command was successful in Unix?
echo $?
Any integer apart from 0 indicates failure or the last command was unsuccessful
10. Write command to list all the links from a directory?
ls -lrt | grep "^l"
11. How will you find which operating system your system is running on in UNIX?
uname -a
12. Create a read-only file in your home directory?
touch file; chmod 400 file
13. How do you see command line history in UNIX?
The 'history' command can be used to get the list of commands that we are executed.
14. How to display the first 20 lines of a file?
By default, the head command displays the first 10 lines from a file. If we change the option of head, then we can display as many lines as we want.
head -20 filename
An alternative solution is using the sed command
sed '21,$ d' filename
The d option here deletes the lines from 21 to the end of the file
15. Write a command to print the last line of a file?
The tail command can be used to display the last lines from a file.
tail -1 filename
Alternative solutions are:
sed -n '$ p' filename
awk 'END{print $0}' filename
16. How do you rename the files in a directory with _new as suffix?
ls -lrt|grep '^-'| awk '{print "mv "$9" "$9".new"}' | sh
17. Write a command to convert a string from lower case to upper case?
echo "apple" | tr [a-z] [A-Z]
18. Write a command to convert a string to Initcap.
echo apple | awk '{print toupper(substr($1,1,1)) tolower(substr($1,2))}'
19. Write a command to redirect the output of date command to multiple files?
The tee command writes the output to multiple files and also displays the output on the terminal.
date | tee -a file1 file2 file3
20. How do you list the hidden files in current directory?
ls -a | grep '^\.'
21. List out some of the Hot Keys available in bash shell?
Ctrl+l - Clears the Screen.
Ctrl+r - Does a search in previously given commands in shell.
Ctrl+u - Clears the typing before the hotkey.
Ctrl+a - Places cursor at the beginning of the command at shell.
Ctrl+e - Places cursor at the end of the command at shell.
Ctrl+d - Kills the shell.
Ctrl+z - Places the currently running process into background.

22. How do you make an existing file empty?
cat /dev/null >  filename
23. How do you remove the first number on 10th line in file?
sed '10 s/[0-9][0-9]*//' < filename
24. What is the difference between join -v and join -a?
join -v : outputs only matched lines between two files.
join -a : In addition to the matched lines, this will output unmatched lines also.
25. How do you display from the 5th character to the end of the line from a file?
cut -c 5- filename
26. Display all the files in current directory sorted by size?
ls -l | grep '^-' | awk '{print $5,$9}' |sort -n|awk '{print $2}'
27. Write a command to search for the file 'map' in the current directory?
find -name map -type f
28. How to display the first 10 characters from each line of a file?
cut -c -10 filename
29. Write a command to remove the first number on all lines that start with "@"?
sed '\,^@, s/[0-9][0-9]*//' < filename
30. How to print the file names in a directory that has the word "term"?
grep -l term *
The '-l' option make the grep command to print only the filename without printing the content of the file. As soon as the grep command finds the pattern in a file, it prints the pattern and stops searching other lines in the file.
31. How to run awk command specified in a file?
awk -f filename
32. How do you display the calendar for the month march in the year 1985?
The cal command can be used to display the current month calendar. You can pass the month and year as arguments to display the required year, month combination calendar.
cal 03 1985
This will display the calendar for the March month and year 1985.
33. Write a command to find the total number of lines in a file?
wc -l filename
Other ways to pring the total number of lines are
awk 'BEGIN {sum=0} {sum=sum+1} END {print sum}' filename
awk 'END{print NR}' filename
34. How to duplicate empty lines in a file?
sed '/^$/ p' < filename
35. Explain iostat, vmstat and netstat?
Iostat: reports on terminal, disk and tape I/O activity.
Vmstat: reports on virtual memory statistics for processes, disk, tape and CPU activity.
Netstat: reports on the contents of network data structures.
36. How do you write the contents of 3 files into a single file?
cat file1 file2 file3 > file
37. How to display the fields in a text file in reverse order?
awk 'BEGIN {ORS=""} { for(i=NF;i>0;i--) print $i," "; print "\n"}' filename

38. Write a command to find the sum of bytes (size of file) of all files in a directory.
ls -l | grep '^-'| awk 'BEGIN {sum=0} {sum = sum + $5} END {print sum}'

39. Write a command to print the lines which end with the word "end"?
grep 'end$' filename
The '$' symbol specifies the grep command to search for the pattern at the end of the line.
40. Write a command to select only those lines containing "july" as a whole word?
grep -w july filename
The '-w' option makes the grep command to search for exact whole words. If the specified pattern is found in a string, then it is not considered as a whole word. For example: In the string "mikejulymak", the pattern "july" is found. However "july" is not a whole word in that string.
41. How to remove the first 10 lines from a file?
sed '1,10 d' < filename
42. Write a command to duplicate each line in a file?
sed 'p' < filename
43. How to extract the username from 'who am i' comamnd?
who am i | cut -f1 -d' '
44. Write a command to list the files in '/usr' directory that start with 'ch' and then display the number of lines in each file?
wc -l /usr/ch*
Another way is
find /usr -name 'ch*' -type f -exec wc -l {} \;
45. How to remove blank lines in a file ?
grep -v ‘^$’ filename > new_filename
46. How to display the processes that were run by your user name ?
ps -aef | grep <user_name>
47. Write a command to display all the files recursively with path under current directory?
find . -depth -print
48. Display zero byte size files in the current directory?
find -size 0 -type f
49. Write a command to display the third and fifth character from each line of a file?
cut -c 3,5 filename
50. Write a command to print the fields from 10th to the end of the line. The fields in the line are delimited by a comma?
cut -d',' -f10- filename


Read more ...

How to add Hosts and Host groups into Ansible Server



Ansible Initial Host Configuration.

Ansible knows the hosts and hostgroups from a host file by default it is located as /etc/ansible/hosts.

Read more ...

DPKG Cheat Sheet


DPKG [ dpkg ] is a traditional yet powerful CLI tool for the debian based Linux Distribution.
It is used to install/manage individual packages.

Here are some useful dpkg commands which you can use as a cheat code.




SyntaxDescriptionExample
dpkg -i {.deb package}Install the packagedpkg -i zip_virtualbox-5.amd64.deb
dpkg -i {.deb package}Upgrade package if it is installed else install a fresh copy of packagedpkg -i virtualbox-5.amd64.deb
dpkg -R {Directory-name}Install all packages recursively from directorydpkg -R /tmp/downloads
dpkg -r {package}Remove/Delete an installed package except configuration filesdpkg -r zip
dpkg -P {package}Remove/Delete everything including configuration filesdpkg -P apache-perl
dpkg -lList all installed packages, along with package version and short descriptiondpkg -l
dokg -l | less
dpkg -l '*apache*'
dpkg -l | grep -i 'sudo'
dpkg -l {package}List individual installed packages, along with package version and short descriptiondpkg -l apache-perl
dpkg -L {package}Find out files are provided by the installed package i.e. list where files were installeddpkg -L apache-perl
dpkg -L perl
dpkg -c {.Deb package}List files provided (or owned) by the package i.e. List all files inside debian .deb package file, very useful to find where files would be installeddpkg -c virtualbox-5.amd64.deb
dpkg -S {/path/to/file}Find what package owns the file i.e. find out what package does file belongdpkg -S /bin/netstat
dpkg -S /sbin/ippool
dpkg -p {package}Display details about package package group, version, maintainer, Architecture, display depends packages, description etcdpkg -p lsof
dpkg -s {package} | grep StatusFind out if Debian package is installed or not (status)dpkg -s lsof | grep Status
Read more ...
Designed By Jackuna