Jump to content


Photo
* * * * * 3 votes

Shell tricks


  • Please log in to reply
83 replies to this topic

#41 iceni

iceni

    SUPR3M3 31337 Mack Daddy P1MP

  • Members
  • 400 posts

Posted 04 February 2007 - 09:37 PM

i've got this command as an alias i made :D
watch --interval=2 "sudo netstat -apn -l -A inet"

this is the alias
alias net1='watch --interval=2 "sudo netstat -apn -l -A inet"'

someone said they don't like aliases, but i think that one is important to be able to run quickly if you want to see what's happening on the network. it refreshes every 2 seconds 8)

Edited by iceni, 04 February 2007 - 09:42 PM.


#42 pbx43k

pbx43k

    Mack Daddy 31337

  • Members
  • 241 posts

Posted 04 February 2007 - 11:43 PM

Ever accidentally cat'd a binary file to your terminal and ended up with a bunch of junk characters filling your screen and making the terminal useless? Try using the reset command to restore the correct character set.

#43 Ohm

Ohm

    I could have written a book with all of these posts

  • Members
  • 3,209 posts
  • Gender:Male
  • Location:Maine, USA

Posted 05 February 2007 - 01:46 AM

That sudo one is semi-dangerous, about as dangerous as leaving a root terminal up on your screen. Anyone can walk up, ^C out of it, and have passwordless sudo since you've been preventing it from timing out by re-launching the command every 2 minutes. Of course, sudo being the leet tool that it is gives you plenty of config options to get around that.

#44 Daniel Holiday

Daniel Holiday

    I broke 10 posts and all I got was this lousy title!

  • Members
  • 16 posts

Posted 10 February 2007 - 01:43 AM

Ugh, I don't want to talk about all the times I've seen some jackass dev get in a hurry, and chmod -R 777 an entire /var/www directory... All it takes is one time doing that, and the security of your system can be seriously compromised.


Have you been watching me?

#45 mecca_

mecca_

    DDP Fan club member

  • Members
  • 54 posts

Posted 10 February 2007 - 02:45 AM

Hello everyone, thanks for the good information here!

I'm not really sure if you would want to consider this a "trick" or not but it's gotten me out of some sticky situations when sshing from one machine with my shell set as bash to another machine with my shell set as certain versions of ksh.

Basically if you find yourself in a situation where the backspace key is no longer working, and you don't feel like pressing ctrl-h to erase things all day, you can use "stty" to remap the erase key to the backspace button.

$ stty erase <press the key you wish you map erase to, usually backspace>

You can use stty to remap various other things as well (man stty =)

---

Oh yeah, another thing that may come in handy. (again sorry this may not be considered a trick)
On linux the 'killall' command will let you kill all of the processes with a given name. On some Solaris machines killall will flat out kill all processes... including init. :blowfuse:
A handy string of commands to use when you have a bunch of stray processes out there but you can't use killall is the following:

$ ps -aef | grep <process name> | awk '{print $2}' | xargs kill -9

Again not a trick, but a common, helpful, string that people may end up using :)

#46 dual

dual

    BinRev veteran

  • Agents of the Revolution
  • 1,196 posts
  • Gender:Male

Posted 11 February 2007 - 08:36 AM

Thanks for the leet :> trick. Btw, echo > file != :> file. echo leaves a newline in the file where :> leaves nothing. Prove it with od...

luser@windows:~$ echo >| test.txt
luser@windows:~$ od -h test.txt 
0000000	  0a00														
0000001
luser@windows:~$ :>| test.txt
luser@windows:~$ od -h test.txt
luser@windows:~$

If you "set -o noclobber" then you have to use >| file.

#47 Ohm

Ohm

    I could have written a book with all of these posts

  • Members
  • 3,209 posts
  • Gender:Male
  • Location:Maine, USA

Posted 11 February 2007 - 03:31 PM

Try echo -n >file instead. No newline.

#48 livinded

livinded

    Dangerous free thinker

  • Agents of the Revolution
  • 1,942 posts
  • Location:~/

Posted 12 February 2007 - 09:15 AM

Here are the slides from a great presentation on writing optimized shellscripts. http://netmeister.or...useless_use.pdf

#49 operat0r

operat0r

    Dangerous free thinker

  • Members
  • 793 posts
  • Location:ops

Posted 16 February 2007 - 02:27 PM

wget -q -O 1.html "http://rds.yahoo.com/S=96062857/K=$1+$2/v=2/SID=e/TID=I047_86/l=IPGN/R=1/SIG=1451curee/EXP=1126931656/*-http%3A//images.search.yahoo.com/search/images?p=$1+$2&sm=Yahoo%21+Search&toggle=1&ei=UTF-8&&vm=p&prev_sBL=off&safesearch=1&imgsz=large&b=41"
grep 'fullsize' 1.html  | sed 's/.*imgurl=/<img src="http:\/\//g' | sed -e 's/pg&rurl.*/pg">/g' | sed -e "s/%2F/\//g" |  sed -e "s/%2520/_/g" |  sed -e "s/%7E/~/g" | sed -e "s/%27/'/g" | sed -e "s/%40/@/g" >> $1-$2.html

http://rmccurdy.com/scripts/

#50 Octal

Octal

    Dangerous free thinker

  • Members
  • 886 posts

Posted 19 February 2007 - 12:50 PM

Look at this:
<user@dv5000t ~/> wtf
usage: wtf [-f dbfile] [-t type] [is] <acronym>
<user@dv5000t ~/$ wtf lol
LOL: laughing out loud
<user@dv5000t ~/>$ wtf rofl
ROFL: rolling on floor laughing
haha, that's a sweet command.

Edited by Octal, 19 February 2007 - 12:52 PM.


#51 iceni

iceni

    SUPR3M3 31337 Mack Daddy P1MP

  • Members
  • 400 posts

Posted 13 August 2007 - 10:56 AM

here's my terminal doing some tricks B) lol

Posted Image

#52 ragweed

ragweed

    SUPR3M3 31337 Mack Daddy P1MP

  • Members
  • 484 posts

Posted 22 August 2007 - 07:06 PM

I don't think these really qualify as 'tricks' but
echo "/blah/whatever" | xargs ls -ln
cat /dev/null > somefile
cat /dev/ttyS0 (reading from a serial device)

#53 inaequitas

inaequitas

    SUP3R 31337

  • Members
  • 158 posts

Posted 22 August 2007 - 10:37 PM

Whenever I get bored with the music selection I have I improvise:

dd if=/dev/urandom of=/dev/dsp

Ctrl + C to stop listening...

#54 n3xg3n

n3xg3n

    "I Hack, therefore, I am"

  • Members
  • 960 posts
  • Country:
  • Gender:Male
  • Location:(703)

Posted 23 August 2007 - 09:41 PM

Whenever I get bored with the music selection I have I improvise:

dd if=/dev/urandom of=/dev/dsp

Ctrl + C to stop listening...


Is it bad if that tells you kill specific people :blink:

my "trick"

echo `curl -s http://www.binrev.com/forums/ | tr "<p>" "\ " | tr "</p>" "\ " | grep "users online"`
:lol:

#55 stansmith

stansmith

    H4x0r

  • Members
  • 30 posts

Posted 14 December 2007 - 12:24 PM

DONT LOOK AT THIS IF YOU THINK YOU MIGHT ACTUALLY TRY IT

as root....

Spoiler



... i typed this on purpose the other night before i reformatted, all my shell commands were deleted

#56 Mikhail

Mikhail

    SCRiPT KiDDie

  • Members
  • 23 posts
  • Location:Long Beach, Ca

Posted 17 December 2007 - 03:18 AM

or dd if=/dev/zero of=/dev/hda
for a lowlevel format / self-destruct basically; sure is a good way to securely delete your HD
hey you could even try dd if=/dev/urandom of=/dev/dsp same command using I/O redirection is cat /dev/urandom > /dev/dsp

#57 mobilec

mobilec

    SCRiPT KiDDie

  • Members
  • 25 posts
  • Location:Ottawa, Canada

Posted 18 December 2007 - 11:04 AM

Sorry if this was mentioned before:
Anything within `` gets executed, e.g.:
$ HELLO="`echo Hello`"
$ echo "$HELLO"
Hello


#58 iceni

iceni

    SUPR3M3 31337 Mack Daddy P1MP

  • Members
  • 400 posts

Posted 18 December 2007 - 12:52 PM

i use this script, below, to unpack archives. i made an alias for it, so i only have to type 'unpack whatever_archive'
this is the alias -
alias unpack='/home/iceni60/scripts/unpack2dir.sh'
#! /bin/sh
# #############################################################################

	   NAME_="unpack2dir"
	   HTML_="uncompress unpack script"
	PURPOSE_="unpack zip, tar, tgz, tar.gz, tar.bz2, tar.z to a dir of the same name as archive prefix"
   SYNOPSIS_="$NAME_ [-vhlr] <file> [file...]"
   REQUIRES_="standard GNU commands"
	VERSION_="1.2"
	   DATE_="1999-09-20; last update: 2006-02-03"
	 AUTHOR_="Dawid Michalczyk <dm@eonworks.com>"
		URL_="www.comp.eonworks.com"
   CATEGORY_="compress"
   PLATFORM_="Linux"
	  SHELL_="bash"
 DISTRIBUTE_="yes"

# #############################################################################
# This program is distributed under the terms of the GNU General Public License

# HISTORY:
# 2006-02-03 v1.2 - added the -C flag to tar options. Otherwise tar would not 
#			extract to a dir with different name then the one found in the 
#			archive.

usage () {

echo >&2 "$NAME_ $VERSION_ - $PURPOSE_
Usage: $SYNOPSIS_
Requires: $REQUIRES_
Options:
	 -r, remove the compressed file after extraction
	 -v, verbose
	 -h, usage and options (help)
	 -l, see this script"
	exit 1
}

# args check
[ $# -eq 0 ] && { echo >&2 missing argument, type $NAME_ -h for help; exit 1; }

# var init
rmf=
verbose=

# option and argument handling
while getopts vhlr options; do

	case $options in
		r) rmf=on;;
		v) verbose=on;;
		h) usage;;
		l) more $0; exit 1;;
	   \?) echo invalid argument, type $NAME_ -h for help; exit 1;;
	esac

done
shift $(( $OPTIND - 1 ))

mkdirf() {

	# usage: fnc <file_prefix> <file>

	[ -d $1 ] && { echo "${NAME_}: skipping ${2} - dir ${1} already exist"; continue; }
	#echo $1
	mkdir $1
#	[[ $verbose ]] && echo "${NAME_}: unpacking "$2
}

file_getDirname() {

	local _dir="${1%${1##*/}}"
	[ "${_dir:=./}" != "/" ] && _dir="${_dir%?}"
	echo "$_dir"

}

file_getBasename() {

	local _name="${1##*/}"
	echo "${_name%$2}"

}

clean() {

	# usage <exit_status> <dir_to_rm>

	[[ $1 != 0 ]] && rmdir $2 # remove empty dir if unpacking went wrong
	[[ $1 == 0 && $verbose ]] && echo "${NAME_}: unpacking " ${dir}/${a}
	[[ $rmf ]] && rm -f -- $a

}

start_dir=$(pwd)

for a in "$@"; do

	cd $start_dir		
	fname=$(file_getBasename $a)
	dir=$(file_getDirname $a)
	cd $dir
	a=$fname

	case $a in


		# zip
		*.[zZ][iI][pP])
			mkdirf ${a/.[zZ][iI][pP]/} $a
			unzip -qq $a -d ${a/.[zZ][iI][pP]/}
			clean $? ${a/.[zZ][iI][pP]/}
		   ;;

		# tar
		*.[tT][aA][rR])
			mkdirf ${a/.[tT][aA][rR]/} $a
			tar -xf $a -C ${a/.[tT][aA][rR]/}/
			clean $? ${a/.[tT][aA][rR]/}
		   ;;

		# tgz
		*.[tT][gG][zZ])
			mkdirf ${a/.[tT][gG][zZ]/} $a
			tar -xzf $a -C ${a/.[tT][gG][zZ]/}
			clean $? ${a/.[tT][gG][zZ]/}
		   ;;

		# tar.gz 
		*.[tT][aA][rR].[gG][zZ])
			mkdirf ${a/.[tT][aA][rR].[gG][zZ]/} $a
			tar -xzf $a -C ${a/.[tT][aA][rR].[gG][zZ]/}/
			clean $? ${a/.[tT][aA][rR].[gG][zZ]/}
		   ;;

		# tar.bz2
		*.[tT][aA][rR].[bB][zZ]2)
			mkdirf ${a/.[tT][aA][rR].[bB][zZ]2/} $a
			tar -xjf $a -C ${a/.[tT][aA][rR].[bB][zZ]2/}/
			clean $? ${a/.[tT][aA][rR].[bB][zZ]2/}
		   ;;

		# tar.z
		*.[tT][aA][rR].[zZ])
			mkdirf ${a/.[tT][aA][rR].[zZ]/} $a
			tar -xZf $a -C ${a/.[tT][aA][rR].[zZ]/}/
			clean $? ${a/.[tT][aA][rR].[zZ]/}
		   ;;


		*) echo "${NAME_}: $a not a compressed file or lacks proper suffix";;

	esac

done


#59 iceni

iceni

    SUPR3M3 31337 Mack Daddy P1MP

  • Members
  • 400 posts

Posted 18 December 2007 - 01:01 PM

here are some more aliases i've got -

# screenshots
alias screenshot='import -window root ~/Desktop/`date +%Y%m%d%H%M`.png'

# System info
alias cpuu="ps -e -o pcpu,cpu,nice,state,cputime,args --sort pcpu | sed '/^ 0.0 /d'"
alias memu='ps -e -o rss=,args= | sort -b -k1,1n | pr -TW$COLUMNS'
alias pg='ps aux | grep'  #requires an argument

# interactive
alias cp='cp -vi'
alias mv='mv -vi'
alias rm='mv --target-directory=$HOME/.Trash/'

# network
alias net1='watch --interval=2 "sudo netstat -apn -l -A inet"'
alias net2='watch --interval=2 "sudo netstat -anp --inet --inet6"'  
alias net3='sudo lsof -i'
alias ping='ping -c 10'
alias currports='wine /home/iceni60/Desktop/Desktop_Folder/Network_Tools/currports/cports.exe'
alias winwhois='wine /home/iceni60/Desktop/Desktop_Folder/Network_Tools/win32whois_0_9_13/win32whois.exe'
alias xnews='wine /home/iceni60/Desktop/Desktop_Folder/Network_Tools/XNews/XNEWS.EXE'
alias whois='whois -H'

# chmod and permissions commands
alias mx='chmod a+x'
alias 000='chmod 000'
alias 644='chmod 644'
alias 755='chmod 755'
alias perm='stat --printf "%a %n \n "' # requires a file name e.g. perm file

# lynx web browser
alias bbc='lynx http://news.bbc.co.uk/text_only.stm'
alias google='lynx http://google.co.uk'

here are some functions
function	ff			   { find . -name $@ -print; }

function	mfloppy		  { mount /dev/fd0 /mnt/floppy; }
function	umfloppy		 { umount /mnt/floppy; }

function	mdvd			 { mount -t iso9660 -o ro /dev/dvd /mnt/dvd; }
function	umdvd			{ umount /mnt/dvd; }

function	mcdrom		   { mount -t iso9660 -o ro /dev/cdrom /mnt/cdrom; }
function	umcdrom		  { umount /mnt/cdrom; }

function	psa			  { ps aux $@; }
function	psu			  { ps  ux $@; }

# clock - A bash clock that can run in your terminal window. 
clock () 
{ 
while true;do clear;echo "===========";date +"%r";echo "===========";sleep 1;done 
}

netinfo ()
{
echo "--------------- Network Information ---------------"
/sbin/ifconfig | awk /'inet addr/ {print $2}'
echo ""
/sbin/ifconfig | awk /'Bcast/ {print $3}'
echo ""
/sbin/ifconfig | awk /'inet addr/ {print $4}'

# /sbin/ifconfig | awk /'HWaddr/ {print $4,$5}'
echo "---------------------------------------------------"
}


#60 Octal

Octal

    Dangerous free thinker

  • Members
  • 886 posts

Posted 30 December 2007 - 05:58 PM

DONT LOOK AT THIS IF YOU THINK YOU MIGHT ACTUALLY TRY IT

as root....

Spoiler



... i typed this on purpose the other night before i reformatted, all my shell commands were deleted

So when you make an alias the command gets executed?

Anyway, that doesn't delete all the files on the harddrive. It skips a few, because they are being used.




BinRev is hosted by the great people at Lunarpages!