Tag Archive for Bash

Linux Rename File to Current Date/Time

mv filename "`date +%Y%m%d_%H`_filename"

source

File per Table

#!/bin/bash

if [ "$1" = "" ]; then
PASSWD=
else
PASSWD="-p$1"
fi

#
# DB_IGNORE_LIST is for those schemas which we do not want dump the data
#
DB_IGNORE_LIST="^information_schema$"
USER=root
SOCKET="/tmp/mysql.sock"

#
# Really shouldn't need to do much editing below here.
#
DB_LIST=`mysql -u root -S $SOCKET -e "SHOW DATABASES" -B --skip-column-names | egrep -v "$DB_IGNORE_LIST"`
for db in $DB_LIST
do
TABLE_LIST=`mysql -u root -S $SOCKET -B --skip-column-names -e "SHOW TABLES" $db`
for table in $TABLE_LIST
do
mkdir -p $db
mysqldump -S $SOCKET -u $USER $PASSWD $db $table> $db/$table.sql
done
done

source

ssh via proxy

install corkscrew : sudo apt-get install corkscrew (under ubuntu/debian)

edit your ~/.ssh/config file:

Host *
ProxyCommand corkscrew proxy.server.com 3128 %h %p

source

bash lines for simple statistics on SSH break-in attempts

# Amount per source IP:
cat /var/log/sshd/* | grep 'Invalid'|rev|cut -d ' ' -f 1 | rev | sort | uniq -c | sort -n

# Amount per day:
cat /var/log/sshd/* | grep 'Invalid' | tr -s ' ' | cut -d ' ' -f 1-2 | sort | uniq -c

# The usernames they try:
cat /var/log/sshd/* | grep 'Invalid'|rev|cut -d ' ' -f 3| rev | sort | uniq -c | sort -r -n | less

#The source IPs of accepted logins (to look for things not you)
egrep 'Accepted (keyboard|publi)[^ ]+ for' /var/log/sshd/* |
sed -r 's/(.*from[ ])([0-9.]+)([ ]port.*)/2'/ | sort | uniq -c | sort -n

# Same IP list, but with hostnames instead of counts
# (assuming 'host' is your reverse lookup utility)
egrep 'Accepted (keyboard|publi)[^ ]+ for' /var/log/sshd/* |
sed -r 's/(.*from[ ])([0-9.]+)([ ]port.*)/2'/ | sort | uniq | xargs -n 1 host

source

Compute Directory Size Recursively with du

du . | awk '{sum+=$1} END {printf("%12.0f
", sum*512)}'

source

Deleting a file which is listed in svn status

svn status | awk '/Action/{print $2}' | xargs rm

source

Alias a command and save it in .profile

echo "alias name='command string'" >> ~/.profile

source

Find c99Shell PHP trojan hiding within PHP files

#!/usr/bin/env bash
# Identifies instances of the c99Shell PHP trojan within PHP files
FIND_LOC=${1:-/} # Root the find by the a directory provided as argument or default to root
echo "Starting search from $FIND_LOC..."
find $FIND_LOC -type f -iname '*.php' -exec grep -qi 'C99Shell' '{}' ; -print
echo "Complete"
: # clean exit

source

Tipical bash script option menu

#!/bin/bash

while [ "$#" -gt 0 ]
do
case $1 in
-h | --help)
echo "Show program help for $(basename $0)"
shift
;;
-l | --list)
echo "List the options"
shift
;;
*)
echo "Other options"
shift
;;
esac
done

source

Post to Twitter from the shell

#!/usr/bin/env ruby
#Usage:
# ruby tweet.rb username:password "status message"

def tweet (arguments)
user, twit = arguments
response = `curl -s -u #{user} -d status="#{twit}" <a href="http://twitter.com/statuses/update.xml" >http://twitter.com/statuses/update.xml</a> | grep truncated`
unless (response =~ /truncated>false</)
puts "fail: Tweet failed.  Check your user name and password."
end
end

if (ARGV[1].length > 140)
overlimit = ARGV[1].length - 140
offendingSubstring = ARGV[1][140, ARGV[1].length-1]
puts %Q{FAIL: #{overlimit} characters over the 140-character limit:}
puts %Q{	"...#{offendingSubstring}"}
elsif (ARGV.length != 2) ||
(ARGV[1] == "")
puts %Q{usage: ruby tweet.rb <username> "This will be tweeted to Twitter."}
else
tweet(ARGV)
end

source