Search This Blog
Tuesday, 16 October 2018
Ansible Command to copy files to destination
Friday, 28 September 2018
SHELL SCRIPTS
#!/bin/ksh
echo "Enter your password: "
stty -echo
read -s SSHPASS
export SSHPASS
stty echo
for host in `cat host.txt`
do
sshpass -e ssh-copy-id -f ${host} -o StrictHostKeyChecking=no
done
SSHPASS=""
List the hostnames in host.txt file
2) Shell Script to use rsync to copy the files to remote servers
#!/bin/sh
## Author: Pavan Bandaru
## Date: 9/28/2018
set -x
display_usage() {
echo "This script must be run with ...."
echo -e "\nUsage:\n$0 [hostfile.txt] \n"
}
if [ $# -eq 0 ]; then
display_usage
exit 1
fi
hostfile=$1
for host in `cat<$hostfile`
do
hostname=$(echo $host|awk -F"," '{print $1}')
echo "------------------------------------------------"
echo "connecting to $hostname"
rsync -r localdir ${hostname}:/home/pavan/
RESULT=$?
if [ $RESULT -ne 0 ]; then
echo Login Failed
fi
done
Note: Keep the remote host list on the host file.
script to test remote tcp connection on multiple hosts
Use the following script to validate tcp connection on remote hosts.
Step 1) Generate ssh keys on your local machine
run command: ssh-keygen -t rsa
Step 2) Use the following script to establish ssh connectivity to all remote servers.
#!/bin/ksh
## Author: Pavan Bandaru
##List the hostnames in host.txt file
echo "Enter your password: "
stty -echoread -s SSHPASS
export SSHPASS
stty echo
for host in `cat host.txt`
do
sshpass -e ssh-copy-id -f ${host} -o StrictHostKeyChecking=no
done
SSHPASS=""
#!/bin/ksh
## Author: Pavan Bandaru
display_usage() {
echo "This script must be run with ...."
echo -e "\nUsage:\n$0 [inputfile.txt] \n"
}
if [ $# -eq 0 ]; then
display_usage
exit 1
fi
inputfile=$1
echo "------------------------------------------"
for host in `cat<$inputfile`
do
hostname=$(echo $host | awk -F"," '{print $1}')
remotehost=$(echo $host | awk -F"," '{print $2}')
remoteip=$(echo $host | awk -F"," '{print $3}')
echo $hostname
echo $remotehost
echo $remoteip
ssh -T $hostname << EOSSH
timeout 2 bash -c 'echo >/dev/tcp/$remotehost/$remoteip' && echo "connection status: success" || echo "connection status: failed"
EOSSH
echo "-----------------------------------------"
done
Shell Script to use rsync to copy the files to remote servers
## Author: Pavan Bandaru
## Date: 9/28/2018
set -x
display_usage() {
echo "This script must be run with ...."
echo -e "\nUsage:\n$0 [hostfile.txt] \n"
}
if [ $# -eq 0 ]; then
display_usage
exit 1
fi
hostfile=$1
for host in `cat<$hostfile`
do
hostname=$(echo $host|awk -F"," '{print $1}')
echo "------------------------------------------------"
echo "connecting to $hostname"
rsync -r localdir ${hostname}:/home/pavan/
RESULT=$?
if [ $RESULT -ne 0 ]; then
echo Login Failed
fi
done
Tuesday, 25 September 2018
Shell Script to copy ssh keys to remote Servers
echo "Enter your password: "
stty -echo
read -s SSHPASS
export SSHPASS
stty echo
for host in `cat host.txt`
do
sshpass -e ssh-copy-id -f ${host} -o StrictHostKeyChecking=no
done
SSHPASS=""
Tuesday, 1 May 2018
Thursday, 12 April 2018
Wednesday, 17 May 2017
UNIX Commands
UNIX Principles
Ex: date
Tue Jan 17 13:06:18 EST 2017
date+ format
$date "+%Y-%m-%d"
2017-05-17
$date "+%Y/%m/%d"
2017/05/17
$date "+%m-%d-%Y"
05-17-2017
$date "+%A %m-%d-%Y"
Wednesday 02-10-2017
$date "+%B %m-%d-%Y"
February 02-10-2017
$date "+%m-%d-%y "
02-10-17
cal : To display the current month calendar.
Ex: cal
January 2017
Su Mo Tu We Th Fr Sa
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31
Ex: hostname
oraclelinux
Ex: uname
Linux
Ex: uname -a
Linux oraclelinux 3.8.13-44.1.1.el6uek.x86_64 #2 SMP Wed Sep 10 06:10:25 PDT 2014 x86_64 x86_64 x86_64 GNU/Linux
Ex: ifconfig eth0 | grep "inet addr"
inet addr:192.168.220.128 Bcast:192.168.220.255 Mask:255.255.255.0
Ex: logname
pbandaru
Ex: who
pbandaru pts/0 2017-01-17 11:28 (192.168.220.1)
pbandaru pts/1 2017-01-17 12:08 (192.168.220.1)
Ex: w
13:37:19 up 2:11, 2 users, load average: 0.00, 0.01, 0.05
USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
pbandaru pts/0 192.168.220.1 11:28 7.00s 0.45s 0.06s w
vsindhu pts/1 192.168.220.2 12:08 1:27m 0.03s 0.03s -bash
root pts/2 192.168.220.3 13:40 3.00s 0.03s 0.03s -bash
Ex: who am i
pbandaru pts/0 2017-01-17 11:28 (192.168.220.1)
Create a new file:
Syn: cat>filename
<Enter the content from the Key board>
How to open a file:
How to redirect content from multiple files into one file
How to display the file with line numbers
Ex: $cat -n filename
1 Hi how are you?
2 I am fine
Creating multiple directories:
rm -rf filename
It delete file with a conformation message yes or no. Deletes the file if you select yes option and no for not deleting the file.
To come out of current working directory.
f = force delete
r = recursive delete
It deletes file at any cost even if the user doesn't have read/write permissions.
Syn:$less filename
ctl+c
output: 1065 7588 49412 sample [ Line count, Word Count, Character Count ]
2]$zcat: This is used for to open zip file in readable format.
4] $zip: This command is also used to zip a file but the file format would be .zip
Syn:$zip options filename.zip filename .....
Ex: $zip sample.zip sample
Output: sample.zip (File extension is .zip)
Ex: $zip -r sample sample1 sample2 sample2
Output file: sample.zip ( sample.zip contains sample1, sample2 and sample3)
5] $unzip: This is command is used to unzip a file which is in .zip format
Syn:$unzip filename
Ex:$unzip sample.zip
Output: sample
Ex: #userdel pavan
Ex: #groupadd weblogic
6]Add an existing user to a Group
Syn: usermod -a -G groupname username
Ex: usermod -a -G weblogic pavan ( pavan will be added to weblogic group)
7] To view a users groups
Syn: id username
Ex: id pavan
uid=505(pavan) gid=506(pavan) groups=506(pavan),505(weblogic)
8] Change user primary Group
Syn: usermod -g groupname username
Ex: usermod -g weblogic pavan ( pavan's primary group will be changed as weblogic)
id pavan
uid=505(pavan) gid=505(weblogic) groups=505(weblogic)
9] To view user group names
Syn: groups username
Ex: groups pavan
Output: pavan : weblogic jboss ( pavan user groups are : weblogc and jboss)
10] To view all the groups in the system
Syn: groups
Ex: groups
Output: weblogic jboss
11] Add a new user and assign to the group at once
Syn: useradd -g groupname username
Ex: useradd -g jboss kalyan
( * ? - [] . )
1] grep pavan sample – It will search pavan string in a sample file
It is used to search the string in multiple files.
3)grep –n pavan sample -- Prints lines along with line numbers
By default it sorts the file contents based on ascii value
130
150
1
230
24
25
26
27
38
678
output:
1
100
130
150
230
24
25
26
27
38
678
24
25
26
27
38
100
130
150
230
678
Note : File should be in sorted order first
Piping:
Default Directory Permisions rwx|rwx|r-x [ 775 ]
Others - o
Permission
- deny permission
= assign permission
ls -l sample
(1)$ chmod g+x sample
It will assign write permission only and removes other permission
octal code:
chown oracle sample
-rw-r--r-- 1 oracle root 0 Feb 1 02:37 sample
-rw-r--r--. 1 oracle oracle 0 Feb 1 02:37 sample
Disk Related Commands:
df:
This command is used to find disk space of the file System.
Syn: df
[root@oraclelinux ~]# df
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/sda1 82438832 3456896 74771248 5% /
df -h:
This command is used to find disk space of the file System in human readable format
[root@oraclelinux ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda1 79G 3.3G 72G 5% /
tmpfs 870M 0 870M 0% /dev/shm
df -k:
This command is used to find disk space of the file system in Kilo bytes.
[root@oraclelinux ~]# df -kFilesystem 1K-blocks Used Available Use% Mounted on
/dev/sda1 82438832 3456912 74771232 5% /
tmpfs 890204 0 890204 0% /dev/shm
This command is used to find disk space of the file system Kilo bytes in human
Syn:df -kh
[root@oraclelinux ~]# df -kh
It displays diskspace of all file systems
df -a:
Syn:df -a
[root@oraclelinux ~]# df -a
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/sda1 82438832 3456904 74771240 5% /
proc 0 0 0 - /proc
sysfs 0 0 0 - /sys
devpts 0 0 0 - /dev/pts
tmpfs 890204 0 890204 0% /dev/shm
none 0 0 0 - /proc/sys/fs/binfmt_misc
df -i
This command is used to find disk space in Inodes ,I used, Ifree ,I use
Syn:df -i.
[root@oraclelinux ~]# df -i
Filesystem Inodes IUsed IFree IUse% Mounted on
/dev/sda1 5242880 119160 5123720 3% /
tmpfs 222551 1 222550 1% /dev/shm
du:
This command is used to find disk usage of the file System.
Syn: du
[root@oraclelinux ~]# du
4 ./.local/share
8 ./.local
8 ./.gconfd
4 ./dir2
4 ./.kde
4 ./.gconf
4 ./dir1
140
du -sh dir
It displays dir directory size in human readable format
[root@oraclelinux ~]# du -sh dir1
4.0K dir1
du -sh
It displays total size of the directory in human readable format
Syn: du -sh
du -sh *
Syn: du -sh *
[root@oraclelinux ~]# du -sh *
4.0K anaconda-ks.cfg
4.0K dir1
4.0K dir2
52K install.log
12K install.log.syslog
4.0K number.txt
4.0K sample1.txt
4.0K sample.txt
du -s:
It display total size of the current directory
Syn: du -s
[root@oraclelinux ~]# du -s
140 .
Syn: du -a
[root@oraclelinux ~]# du -all
52 ./install.log
4 ./.local/share
8 ./.local
4 ./.gconfd/saved_state
8 ./.gconfd
4 ./.bashrc
12 ./install.log.syslog
4 ./.bash_profile
4 ./dir2
4 ./.tcshrc
4 ./.cshrc
0 ./.kde/tmp-oraclelinux
4 ./.kde
4 ./.bash_logout
4 ./.gconf
140 .
This is used for to show current running process.
Syn: ps
[root@oraclelinux ~]# ps
PID TTY TIME CMD
15343 pts/2 00:00:00 bash
15575 pts/2 00:00:00 ps
ps -aux:
UID PID PPID C STIME TTY TIME CMD
root 1 0 0 Jan30 ? 00:00:02 /sbin/init
root 1 0.0 0.0 19408 1528 ? Ss Jan30 0:02 /sbin/init
root 2 0.0 0.0 0 0 ? S Jan30 0:00 [kthreadd]
root 3 0.0 0.0 0 0 ? S Jan30 0:00 [ksoftirqd/0]
root 5 0.0 0.0 0 0 ? S< Jan30 0:00 [kworker/0:0H]
root 7 0.0 0.0 0 0 ? S< Jan30 0:00 [kworker/u:0H]
[root@oraclelinux ~]# ps -ef
This command is used to see every process in the system using BSD Syntax
Syn:ps -aux
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
It is used find the cpu usage, memory usage, uptime, load average and process states
Syn:Top
top - 18:42:13 up 1 day, 23:39, 3 users, load average: 0.13, 0.04, 0.05
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
nohup:
This command will execute the process even if you logout from the System.
Syn: nohup cmd &
top:
Tasks: 94 total, 1 running, 93 sleeping, 0 stopped, 0 zombie
Cpu(s): 0.0%us, 0.3%sy, 0.0%ni, 99.7%id, 0.0%wa, 0.0%hi, 0.0%si, 0.0%st
Mem: 1780408k total, 482940k used, 1297468k free, 93720k buffers
Swap: 20970492k total, 0k used, 20970492k free, 168220k cached
16178 root 20 0 98.1m 4736 3728 S 0.3 0.3 0:01.61 sshd
1 root 20 0 19408 1528 1224 S 0.0 0.1 0:02.46 init
2 root 20 0 0 0 0 S 0.0 0.0 0:00.12 kthreadd
3 root 20 0 0 0 0 S 0.0 0.0 0:00.08 ksoftirqd/0
5 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 kworker/0:0H
Ex: nohup sleep 300 &
KILL
kill -9 pid
To kill multiple process
kill -9 pid1 pid2 pid3 pid4
[root@oraclelinux ~]# nohup sleep 600 &
[1] 16577
[root@oraclelinux ~]# ps -ef | grep sleep
root 16577 16555 0 19:15 pts/0 00:00:00 sleep 600
root 16579 16555 0 19:15 pts/0 00:00:00 grep sleep
[root@oraclelinux ~]# kill -9 16577
[root@oraclelinux ~]# ps -ef | grep sleep
root 16582 16555 0 19:15 pts/0 00:00:00 grep sleep
[1]+ Killed nohup sleep 600
Network Related Commands
netstat ( network statistics):
This command is used to display the all Ip- address and port number.
Syn: netstat
This is used to connect the remote servers.
Syn:telnet ip-address port
ftp ( file transfer protocol )
This is used to transfer the files in one system to another system.
Syn: ftp Ip-address.
ping:
This is used to cheek the network connectivity from source system to destination system.
Syn:ping Ipaddress
C:\Users\pavan>ping 192.168.224.129
Pinging 192.168.224.129 with 32 bytes of data:
Ping statistics for 192.168.224.129:
Reply from 192.168.224.129: bytes=32 time<1ms TTL=64
Reply from 192.168.224.129: bytes=32 time<1ms TTL=64
Reply from 192.168.224.129: bytes=32 time<1ms TTL=64
Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
Minimum = 0ms, Maximum = 0ms, Average = 0ms
scp:
This is used to secure copy the files in unix system.
Syn:scp your_username@remotehost:filename /dirpath
find:
This command is used to search files.
Syn:file <path> -name Filename
tar:
tar command is primary archive utility.
Syn:tar cmd
tar -cvf:
This is used to create the all files stored in one file (or) Backup file purpose , you can use this one.
syn:tar -cvf file.tar f1 f2 f3 f4.
tar -xvf:
This is used to extract the all files stored in one file.
crontab
How to delete files which are older than 15 days.
find . -type f -mtime +20 ! -name '*.log' >>list
ls -lrt $(<list)
rm -rf $(<list)
Interview Questions
<><><><><><><>
find / -name sample.txt
2.Display the numbers in sorted order
sort -nu
3.Display the updated lines in file
tail -f filename
4.How to zip a Directory
gzip -r /hom/bea/app
5.How to kill a prosess.
kill -9 pid
6.Display all java process in unix.
ps -ef |grep java
7.How to delete 10 lines in vi editor.
10dd
8.How to search a string and replace tin vi editor.
:"%s/old string /new string/ gi"
9.How to search a string in vi editor from bottom to top.
?string name
10.How to insert a line above the current line in vi editor .
esc+o
11.How to rename a file .
mv file1 file2
12. copy the content one directory to another directory.
cp -r dir1/file1 dir2/file2
13. How can delete blank lines in directory.
grep -v "^$" sample > temp
mv temp sample
14.How can replace astring in unix.
sed "s/oldstring/newstring/g" filename
15.Syntax for zip and unzip afile.
gzip filename
guzip filename.gz
16.How can display top 10 disk usage files.
du -sh *|sort -nr|head -10
17.How to retrieve a field in a file.
cut -f 1,2 stud.
18.what is command search a string in a file.
grep
19).How to go to the end of the line in Vi editor?
G
20).Copy 10 line in vi editor?
10yy
21).How to go to insert mode in vi editor?
Esc+i
22).How to search a string in a file(filename=sample,string=weblogic)?
grep weblogic sample
23).How to copy file from one unix to other unix system and syntax (file= sample.txt, target host=192.168.11.128,target file path=/home/bea)?
scp -rp sample.txt username@192.168.11.128:/home/bea
24).Copy file from path to other tree structure is given below?
25).How to find out CPU utiligation?
top
26).How to execute a script using nohup (script name=startweblogic.sh)?
nohup ./startWeblogic.sh &
27).Which command is used to search and replace a string?
sed
28).wich command is used to search a string in multiple files?
fgrep
A) /weblogic
2)how to save and quit from vi Editor's?
a)wq
3)what are the Advantages of nohup command?
A)nohup will execute the process if you layout system.
Syn:nohup &.
4)Differance b/w the ping and tracert?
Traceroute takes advantage of TTL, time to live, to determine the address of each hop between souce and destination. Each router decrements TTL before forwarding it, this mechanism is designed to prevent packets from circulating endlessly. When a router is unable to decrement TTL it sends a message back to the originator. This provides the address of each router. With that knowledge a ping request is sent to each other.
5)How to execute unix commands in vi?
A) :! unix command
6)tar dir1 dir2 dir3 and new_dir?
A)syn:tar -cvf new_dir.tar dir1 dir2 dir3.
7)How to display the ipaddress and portnumber?
A)netstat -anp
8)how to delete directory with recursion and force?
A)rm -rf filename.
9)How to Open a file with page to page ?
A)more filename
10)How to Hide a file (file name=tuxedo)?
A)mv tuxedo .tuxedo
11)How to do undo in vi Editor's?
A)u
12)How to goto 100 Line in Vi Editor's?
A)100L
13)How to display last 100 Line from a file?
A)tail -100 filename
14)How to Reterive the fields from a file?
A)cut
15)How to zip a directory?(/home/directory)?
A)gzip -r /home/directory
16)How to go to end of the line in Vi?
A)G
17)Display the directiory count in current dir?
A)ls -lrt !wc -l
18)How to display all files ending with "log"?
19)How to appeand data to the existing file?
A)cat>>filename
20)How to find out the diskspace of the fileSystem?
A)df -sh
21)write A syntax for the scp Commands?
A) Scp filename root@ipaddress:filename.
22)How to display the updated lines in file ?
A)tail -f filename.
23)How to display the ipaddress and portnumber?
A)netstat -anp
24)which command is used to connect to the remote server?
A)telenet ipaddress.
25)How to repalce and String in vi Editor's?
A) sed %s/oldstring/newstring/g
25)How to repalce and String in unix?
A) sed s/oldstring/newstring/g filename
26)How to display top 10 lines from a file?
A) head -10 filename.
27)syntax for tar and untar a file?
A)tar -cvf filename.tar file1 file2 file3.
tar -Xvf filename.tar
28)How to display hiddean a file?
A)ls -a.
29)How to Delete a blank line from a filename?
A)grep "^v" sample >temp
mv temp filename
30)How to display all cuurent running process?
A)ps -ef
31)syntax for zip and unzip file?
A)gzip filename.
gunzip filename.gz

