1834226 Members
2506 Online
110066 Solutions
New Discussion

Re: text or binary file

 
SOLVED
Go to solution
Ridzuan Zakaria
Frequent Advisor

text or binary file

Hi everyone,

I would like to write a script that search for restricted word such as 'PASSWORD' in text file not executable or binary file.

How do I tell if the file is binary or text?

I plan to use find and grep command to navigate through directories to search for word password.

Thanks.
quest for perfections
7 REPLIES 7
Sanjay_6
Honored Contributor
Solution

Re: text or binary file

Hi,

from another forum solutions.

http://forums1.itrc.hp.com/service/forums/questionanswer.do?threadId=368008

Hope this helps.

Regds
john korterman
Honored Contributor

Re: text or binary file

Hi,
use the file command, e.g.:
# file * | grep text | awk '{print $1}'

regards,
John K.
it would be nice if you always got a second chance
Steven E. Protter
Exalted Contributor

Re: text or binary file

The file command gives you the attributes of the file.

Try this:

file /usr/bin/scp

answer includes the word binary

file /etc/issue

answer includes the word text

You should be able to run through a lot of files and find what you need. This seems like a security scan of some sort.

SEP
Steven E Protter
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com
Sundar_7
Honored Contributor

Re: text or binary file

I can suggest a quick and dirty script - take it from there :-)

# vgdisplay 2>/dev/null | grep "VG Name" | awk '{print $3}' | xargs -n1 | while read VG
do
VGNAME=$(echo "$VG" | sed 's/\/dev\///')
vgdisplay -v $VG | egrep "LV Name|Current LE" > /root/$VGNAME-LV.list
done

Now once you are done restoring the ignite in the DR server

# for VGS in $(ls /root/*LV.list)
do
cat VGS | xargs -n2 | while read LV SIZE
do
VGNAME=$(echo "$VGS" | sed 's/\-LV\.list//')
LVNAME=$(echo "$LV" | sed 's/\/dev\/vg.*\///')
lvcreate -l $SIZE -n $LVNAME /dev/$VGNAME
done
done

Remember, the above excerpt will not take care of striping or extend allocation policies or mirroring. But one would assume that is not critical for the DR tests.

I am not sure the above script is not going to work either :-).
Learn What to do ,How to do and more importantly When to do ?
Sundar_7
Honored Contributor

Re: text or binary file

Sorry, the above post is not for you !
Learn What to do ,How to do and more importantly When to do ?
Bill Hassell
Honored Contributor

Re: text or binary file

Unfortunately, the file command uses special magic (actually, it uses /etc/magic) to figure out whether a file is text (or dozens of other formats). The problem is that there are NO flags to identify whether a file contains all ASCII (/etc/motd), some ASCII (/etc/lvmtab), or no meaningful ASCII (random data file). The problem with file is that a ps (postscript) file is ASCII, just as an awk program files is ASCII. So looking for only files that are text will miss hundreds of ASCII files.

What would make more sense is to include only normal files (find ... -type f ...) and then read the files using the strings command and follow that with grep, something like this:

find /etc -type f | while read
do
CNT=$(strings $REPLY | grep -c password)
if [ $CNT -ne 0 ]
then
echo
echo $REPLY
strings $REPLY | grep password
fi
done

You can type all the commands in at the shell prompt or paste them into a file and run the file. Change the first line (find /etc) to find whatever filesystem you are searching. Note that find / is very bad for performance on a multi-user server. It's best to look in likely locations, not in CDROMs, NFS mounted filesystems, database directories, etc.


Bill Hassell, sysadmin
Ridzuan Zakaria
Frequent Advisor

Re: text or binary file

Thank. I am using suggestion by Sanjay.
quest for perfections