## Functions from my bashrc file ## Dont hold me responsible for your mistakes! ## Please don't run this file! Pull the functions from it! # Define some colors first: red='\e[0;31m' RED='\e[1;31m' blue='\e[0;34m' BLUE='\e[1;34m' cyan='\e[0;36m' CYAN='\e[1;36m' NC='\e[0m' # No Color ## Example: mvit /path/path/path/target target2 (but will be in /path/path/path/) function mvit() { a_=$(dirname "$1") b_=$(basename "$1") cd "$a_" && mv -i "$b_" "$2" && cd - &>/dev/null } # calc in shell calc(){ perl -wlne'print eval'; } export -f calc # whats playing on xmms, needs xmms plugin called xmms-infopipe function playing() { if [ -p /tmp/pipe name goes here ] then echo -e $red ;cat /tmp/xmms-info | sed -e '1,2d' -e '4,6d' -e '8d' -e '10,12d' ; echo -e $NC fi } function space() { echo -e $red; df -hP | sed -e '1s/Filesystem/ Filesystem/'; echo -e $NC } # whos hogging the hdd hog directory user hog() { find $1 -type d -xdev -user $2 | sed -e 's/ /\\ /g' -e "s/[']/\\\'/g" | xargs du -sm | sort -g } # useful for reading folders full of 2-5 line test files,quickly. function read_all() { for i in * do less $i && read $a done } # find a file function ff() { find . -name '*'$1'*' ; } # find a file # find a file and run $2 on it function fe() { find . -name '*'$1'*' -exec $2 {} \; ; } function fstr() # find a string in a set of files { if [ "$#" -gt 2 ]; then echo "Usage: fstr \"pattern\" [files] " return; fi SMSO=$(tput smso) RMSO=$(tput rmso) find . -type f -name "${2:-*}" -print | xargs grep -sin "$1" | \ sed "s/$1/$SMSO$1$RMSO/gI" } # cut last n lines in file, 10 by default function cuttail() { nlines=${2:-10} sed -n -e :a -e "1,${nlines}!{P;N;D;};N;ba" $1 } # move filenames to lowercase function lowercase() { for file ; do filename=${file##*/} case "$filename" in */*) dirname==${file%/*} ;; *) dirname=.;; esac nf=$(echo $filename | tr A-Z a-z) newname="${dirname}/${nf}" if [ "$nf" != "$filename" ]; then mv "$file" "$newname" echo "lowercase: $file --> $newname" else echo "lowercase: $file not changed." fi done } # swap 2 filenames around function swap() { local TMPFILE=tmp.$$ mv $1 $TMPFILE mv $2 $1 mv $TMPFILE $2 } # repeat n times command function repeat() { local i max max=$1; shift; for ((i=1; i <= max ; i++)); do # --> C-like syntax eval "$@"; done } function ask() { echo -n "$@" '[y/n] ' ; read ans case "$ans" in y*|Y*) return 0 ;; *) return 1 ;; esac } # End of bashrc Jump to directories in CDPATH ----------------------------- export CDPATH=.:~:/home/user/Desktop:/home/user/Documents Correct minor spelling errors ----------------------------- shopt -s cdspell Some alias play --------------- alias ducks='du -chs * |sort -rn |head -11' alias whatup='netstat -tupan|grep ESTAB' alias lr='ls -lR | more' # recursice ls alias lx='ls -lXB' # sort by extension alias t='tree -Cs | more' alias j='jobs -l' alias x='clear && ls -d /proc/* |grep [0-9] |wc -l; ps ax |wc -l' # compares count in proc to ps, good warning of intrustion. GENERAL IDEAS ------------- Add this to /etc/inputrc for custom shortcut keys in shell. I'll give 3, the syntax is straight forward, this should atleast get a person started. Control-n: "netstat -tupan \C-m" Control-h: "history \C-m" Control-w: "ifconfig | tail -10 && iwconfig wlan0 \C-m" # needs a wireless connection Is a computer next to your bed? Then use a joystick for a snooze button style alarm on the pc. :) I use these tools: kalarm, mpg123, joyd Xconsole setup -------------- 1. (as root) mkfifo /dev/xconsole chmod 640 /dev/xconsole chown root.tty /dev/xconsole 2. then in /etc/syslog.conf *.err;auth,authpriv.none |/dev/xconsole or (recommended) *.* |/dev/xconsole 3.then add tty to your user's group 4.then at command line do a xconsole -file /dev/xconsole Also, go ahead and hit ctrl s or ctrl r in xconsole, cool huh. Commands to backup your sony playstation games ---------------------------------------------- cdrdao read-cd --read-raw --datafile my_game_rip.bin --device 0,3,0 --driver generic-mmc-raw my_game_rip.toc cdrdao write --eject --speed 16 --device 0,0,0 --driver generic-mmc my_game_rip.toc Html to txt with lynx --------------------- lynx -dump file.html > file.txt KDE hack only ------------- #This one merges the term to the kmail composer. #!/bin/sh #ckmail #Requires kmail to be up an running usage_() { echo -e "\nckmail -t TO -c CC -s SUBJECT | -h --help" echo -e "Then enter msg and ^d to bring up kmail composer window that's ready to send.\n" exit } if [ $# -eq "0" ] then usage_ fi mailit_() { echo -e "\nEnter message and ^d to bring up kmail composer window that's ready to send.\n" dcop kmail default openComposer "$TO" "$CC" "" "$SUBJECT" "$(cat)" 0 "" &>/dev/null exit } while getopts ":t:c:s:h" Option do case $Option in t) TO="$OPTARG";; c) CC="$OPTARG";; s) SUBJECT="$OPTARG";; h) usage_;; help) usage_;; -help) usage_;; * ) usage_;; esac done shift $(($OPTIND - 1)) if [ "$TO" = "" ];then echo -e "\nMust have a receiving [TO] address" usage_ elif [ "$SUBJECT" = "" ];then echo -e "\nMust have a [SUBJECT] entry" usage_ fi mailit_ exit KDE hack only ------------- # Copy standard input or a string to the KDE clipboard. function klip() { if [[ $# -eq 0 ]]; then dcop klipper klipper setClipboardContents "$(cat)" else dcop klipper klipper setClipboardContents "$*" fi } Misc dcop calls --------------- dcop ksmserver ksmserver logout 0, 0, 0 [Will bring up logout dialog box] dcop kdesktop KScreensaverIface lock [Locks desktop] dcop kwin KWinInterface setCurrentDesktop 2 [Switches to desktop 2] dcop kdesktop KBackgroundIface setWallpaper /path/to/file 1 [Background for desktop 1] dcop kicker kicker popupKMenu 0 [Brings up main menu] Much like Xmessage and Xdialog (my favorite, Xdialog is for sure worth investigating). kwrapper `kdialog --textbox "/path/to/textfile" 900 600` Check out at command line: kdialog --help-all Ssh tricks from the book linux server hacks ------------------------------------------- #ssh-to #!/bin/sh ssh `basename $0` $* create links to ssh-to in the same directory, example; ln -s /bin/ssh-to /bin/server then server command RCG, from the book linux server hacks ------------------------------------- #!/usr/bin/perl -w use strict; use Term::ANSIColor qw(:constants); my %target = (); while (my $arg = shift) { my $clr = shift; if(($arg =~ /^-/) | (!$clr)) { print "Usage: rcg [regex] [color] [regex] [color] ...\n"; exit; } # # Ugly, lazy, pathetic hack here # $target{$arg} = eval($clr); } my $rst = RESET; while(<>) { foreach my $x (keys(%target)) { s/($x)/$target{$x}$1$rst/g; } print; } usage rcg sendmail MAGENTA < /var/log/messages | less -r or rcg '\d+\.\d+\.\d+\.\d+' GREEN < /var/log/maillog or chain colors together tail -50 /var/log/messages | rcg WARNING 'BOLD . YELLOW . ON_RED' Createing a filesystem within a file ------------------------------------ Create the file, below will do nicely. You can set count to as big or small as you want. 131072 will create a file 128 meg. # dd if=/dev/zero of=linux.ex2 bs=1024 count=131072 Create the filesystem, below will do that. Just answer yes to the complaint about not being a block device. # mke2fs linux.ex2 Mount the new file. You will need loopback support, either as a module or built in the the kernel. # mount linux.ex2 /mnt -o loop=/dev/loop0 And you are done. You can also encrypt this file with gnupg when unmounted. Moving Directory Trees ---------------------- This was taken from lockergnome Sometimes you need to copy every file in a directory to another. The co needs to include symbolic links, permissions, etc. As could be expected, have several options in Linux for accomplishing this task. First, you could use the cp command, adding the -r option to assure that everything is copied recursively. That doesn't really guarantee that you'll everything, though. cp has a difficult time with symlinks. Here's a better command using tar that will accomplish precisely what you want to acco cd /home/tony/newfiles tar cf - . | (cd ./archive; tar xvf -) First you're changing to the newfiles subdirectory in my home directory. Then [c]reates a [f]ile from the entire contents of the /home/tony/newfiles/archive directory. This file never actually gets stored anywhere, though, as it's passed directly to a subshell. The subshell changes directories [cd] to the archive subdirectory within /home/tony/newfiles [./archive] and untars [x] [v]erbose the [f]ile passed to it from the original tar command. You end up with a precise copy of everything from /home/tony/newfiles in /home/tony/newfiles/archive, symlinks, permissions and all. It's almost like running a whole block without ever touching the ground. Add kmail support to firefox ---------------------------- firefox in address bar: about:config then change/create the following network.protocol-handler.expose.mailto ==true network.protocol-handler.external.mailto ==true network.protocol-handler.app.mailto string=kmail Sometimes a root kit will change ls but not echo, so ls won't be accurate, but echo may. The following line will compare results in a directory from ls and echo. The only lame thing with this technique is that echo doesn't see the .hidden files, where as ls will. Thinking about this though, why would it be needed to .hide a file when the attackers rootkit hides common files anyways? So, this could help. this uses the $red and $NC color declorations at the top of this file. Rid these below; not needed. seta=$(ls | xargs | tr -d "/" | tr -d "|" | wc -c); setb=$(echo * | wc -c); echo -e $red"\nls set= $seta \necho set= $setb\n"$NC; unset seta setb I've added this into my habit of clearing the screen using an alias of c. Below is the alias I use, clears the screen compares open files in proc to what's reported by ps (first number column) compares ls to echo in current directory (second column set) and kills esd (keeps poping up on my system from galeon, drives me nuts!) alias c='clear && ls -d /proc/* |grep [0-9] |wc -l | tr -d "\n"; seta=$(/bin/ls --color=auto | wc -c);echo -e $red"\tls = $seta"$NC;ps ax |wc -l | tr -d "\n"; \ setb=$(echo * | wc -c);echo -e $red"\techo = $setb"$NC; pgrep esd && killall esd'