Tuesday, May 27, 2008

Check OS Script ...

I had to check all hosts in a list , if its windows or unix.

Here's what helps identify whether it is Unix or Windows,
Note: Our Windows systems did not have ssh daemon running,


bash-2.03$ cat checkos.sh
#!/bin/sh
for host in `cat list_of_host`
do
if version=`(sleep 1 ; echo "") | telnet $host 22 | grep -i SSH`
then
echo "$host $version" >> HOSt-SSH-Running
else
echo "$host" >> HOST-SSH-Not-running
fi
done


The above scripts verify if the list of host in file has ssh daemon
running on port 22, in case if it finds running then it appends
that host to HOSt-SSH-Running and rest in HOST-SSH-Not-running.

Sunday, May 25, 2008

Finding Common Occurence in Files and Writing to a New file !

I wanted to compare lines in 2 files and write things which
are common in a different file.

Eg,
File1 (has line like below)
===========================
host1
host2
host3
etc.....


File2 (has line like below)
===========================
host2
host3
host4
host5
etc ...

Which ever is common among file1 and file 2 should be written to file3

Grep does it :

grep -f file1 file2 > file3

file3 will have host2, host3 from above example shown under it.

Tuesday, May 20, 2008

Shell Script Comparing files ...

I have a file (file1) which is like

host1
host2
host3
host4
the list goes on............


Now I want the above lines in files to be compared with files under
/opt/new/*

File names under /opt/new are as below:
Dev
Prod
QA

And suppose host1 from file1 is found under Dev(file under /opt/new)
than it should write under a seperate file New-list as host1-DEV

Ans:

while read line; do echo ${line}-$(grep -l $line /opt/new/*); done < file1 > new-file

The above line reads from file1 and compares the list of hosts given under it with
all files under /opt/new/* and if the host on file matches on any of file.

Then it writes to a new file (new-file) as
host-filename, here host is the one listed in file1 and filename is
the file under which it found the host .

eg if it finds host1 under dev file (/opt/new)

It will write as host1-dev under new-file !