Jump to content
wilsontc

Useful things to put in your shell rc

Recommended Posts

Hello guys,

 

When I was using Exetel as my ISP, I had written a shell function to display my remaining Internet quota for the month. I recently updated it as I switched to Netspace, so I thought I might share this in case someone finds it useful. For the Exetel version, see here.

 

function usage() {
  data="login=login&dest=%2Fusage-meter%2F&username=YourUsername&password=YourPassword"
  cookie_jar=/tmp/myCookies.txt
  url="https://my.netspace.net.au/logincheck"
  usage_page=/tmp/usage.html
  curl -s --cookie-jar $cookie_jar --cookie $cookie_jar -d $data --location $url -o $usage_page
  w3m -dump $usage_page | grep "Usage Remaining" -A 2
}

I put this in my shell rc file, and then I can view my remaining usage:

 

timothy@lear:~$ usage
Period   Usage Remaining
Peak	 24295 Mb
Off Peak 25547 Mb
timothy@lear:~$

I also have a function to backup my imap mailbox:

function backupmail() {
  server1="mail.myDomain.com"
  server2="localhost"
  users=(timothy mina katherine)
  passwords1=(rempw1 rempw2 rempw3)
  passwords2=(localpw1 localpw2 localpw2)

  for ((i=1;i<${#users}+1;i++)) do
	~/opt/local/bin/imapsync --host1 $server1 \
			 --port1 993 \
			 --host2 $server2 \
			 --port2 993 \
			 --ssl1 \
			 --ssl2 \
			 --noauthmd5 \
			 --user1 $users[$i]@myDomain.com \
			 --user2 $users[$i] \
			 --password1 $passwords1[$i] \
			 --password2 $passwords2[$i] \
			 --authmech2 plain
  done
}

Periodically I run this command which downloads the mail to a locally running instance of Dovecot on my machine.

 

Finally, I have a small function to remove trailing "u1conflict" recursively from files in the current directory. If anyone uses Ubuntu One, you might find this handy!

 

function nou1conflict() {
  zmv '(**/)(*).u1conflict' '$1$2'
}

What things do you guys have in your shell rc?

Edited by wilsontc

Share this post


Link to post
Share on other sites

Hey Man! that's beautiful.

 

I want to check it out.

 

Cheers Glenn

 

(edit) I have a bunch of aliases that saves me typing common commands.

 

There's a lot of commented stuff that I have no idea about. lol.

Edited by GlennsPref

Share this post


Link to post
Share on other sites

I had no idea that you could declare functions in a shell rc file. Obvious really. And that is one useful function to add (for Australians at least). Thanks for sharing.

 

I prefer not to use aliases. I feel it's best to use the commands intended on a fresh install of any distro of GNU/Linux.

 

I try to keep my configs simple yet somewhat stylish. This is my ~/.bashrc:

# Change bash prompt.
export PS1='\[\e[32;1m\]\u\[\e[34;1m\]@\[\e[36;1m\]\H \[\e[34;1m\]\W\[\e[32;1m\] $ \[\e[0m\]'

# Set custom colors for files in lists
export LS_OPTIONS='--color=auto'
eval `dircolors`
alias ls='ls $LS_OPTIONS'
   
# This script needs to be on the bottom of the file or RH updates will fsck it up
# Source global definitions
if [ -f /etc/bashrc ]; then
	. /etc/bashrc
fi

Share this post


Link to post
Share on other sites

I had no idea that you could declare functions in a shell rc file. Obvious really. And that is one useful function to add (for Australians at least). Thanks for sharing.

You're welcome! If you feel this would be useful for you, I'm happy to help you out with modifying it for your ISP (if you're in a country which has quotas like Australia!)

 

I prefer not to use aliases. I feel it's best to use the commands intended on a fresh install of any distro of GNU/Linux.

Well, I totally disagree with you there, I think aliases are very, very useful, and should not be overlooked. Perhaps you don't use the command line very often?

 

In addition to the usual suspects:

alias mv='mv -v'
alias ll='ls -l'
alias la='ls -la'

I also have an alias for connecting to my router on it's serial interface:

alias router='sudo cu -s 9600 -l /dev/ttyS0'

Aliases can also be used to prevent silly typo's:

alias cd..='cd ..'

Share this post


Link to post
Share on other sites

I use the command line more often than I touch the mouse. :)

 

Still not too interested in adding options to the basic commands using alias ... but I do use the commands "df -ah" and "ps a" quite a bit. I could alias those, but I doubt I would remember my alias as well as I can remember "df -ah" and "ps a" ...

 

I really like your second example ("router") ... I think I might consider adding an "alias opt='mount -o loop /dev/sr0 /media/dvd'" (but it still might be faster to simply open and close the tray if a disc isn't already mounted) or an "alias mnt='mount /dev/sdb1 /media/qd'" to /root/.bashrc ...

 

And that third example is quite brilliant. Sometimes when I've been using DosBox or Windows command for a while, I get caught up in the "dir" and "cd.." habits. Cheers.

Share this post


Link to post
Share on other sites

mate, on the subject of open source...

 

When I say I left a whole lot of stuff out, well it seems it's mostly blog stuff....I'm still learning!...(?) I hope...~/zsh.rc

# Lines configured by zsh-newuser-install
HISTFILE=~/.histfile
HISTSIZE=1000000
SAVEHIST=600000
setopt appendhistory beep extendedglob nomatch notify
#bindkey -v #set below
# End of lines configured by zsh-newuser-install
# The following lines were added by compinstall
zstyle :compinstall filename '/home/glenn/.zshrc'

autoload -Uz compinit
compinit
# End of lines added by compinstall
#
# .zshrc file for zsh 4.0
#
# .zshrc is sourced in interactive shells.  It
# should contain commands to set up aliases, functions,
# options, key bindings, etc.
#

# Set a colour prompt #  home		by wilsontc  modified and adopted 1st-oct2008 GlennsPref
autoload colors zsh/terminfo
if [[ "$terminfo[colors]" -ge 8 ]]; then
	colors
fi
	for color in RED GREEN YELLOW BLUE MAGENTA CYAN WHITE; do
		eval PR_$color='%{$terminfo[bold]$fg[${(L)color}]%}'
		eval PR_LIGHT_$color='%{$fg[${(L)color}]%}'
		(( count = $count + 1 ))
	done
PR_NO_COLOR="%{$terminfo[sgr0]%}"
if [ `whoami` = 'root' ]; then
	PS1="$PR_RED%n$PR_NO_COLOR@$PR_BLUE%m%u$PR_NO_COLOR:$PR_CYAN%2c$PR_NO_COLOR%(!.#.$) "
else
	PS1="$PR_GREEN%n$PR_NO_COLOR@$PR_BLUE%m%u$PR_NO_COLOR:$PR_CYAN%2c$PR_NO_COLOR%(!.#.$) "
fi
# Put a clock on the right hand prompt
RPS1="$PR_CYAN(%D{%d-%m %H:%M})$PR_NO_COLOR"

# Search path for the cd command
cdpath=(.. ~ ~/src ~/zsh)

# Use hard limits, except for a smaller stack and no core dumps
unlimit
limit stack 8192
limit core 0
limit -s

# Set up aliases #		to get root access via sudo instead of "access denied"
alias su='su -p'
alias man='sudo man'
alias cat='sudo cat'
alias locate='sudo locate'
alias lsusb -v='sudo lsusb -v'
alias lsusb='sudo lsusb'
alias lsmod='sudo lsmod'
alias lspci -v='sudo lspci -v'
alias lspci='sudo lspci'
alias fw-reset='sudo fw-reset'
alias service='sudo service'
alias netstat='sudo netstat -anu'
alias fslint='sudo /usr/bin/fslint-gui'
alias organise-rpms= 'sh ~/build/rpms/organise-rpms'
alias gen-hdlist2= 'sh ~/build/rpms/gen-hdlist2'
alias smart='sudo smart'
alias ifup='sudo ifup'
alias ifdown='sudo ifdown'
alias ifconfig='sudo ifconfig'
alias updatedb='sudo updatedb'
alias rpm='sudo rpm'
alias urpmi.update='sudo urpmi.update'
alias urpmi='sudo urpmi --noclean'
alias urpme='sudo urpme'
alias mv='nocorrect mv'	   # no spelling correction on mv
alias cp='nocorrect cp'	   # no spelling correction on cp
alias mkdir='nocorrect mkdir' # no spelling correction on mkdir
#alias j=jobs
#alias pu=pushd
#alias po=popd
#alias d='dirs -v'
#alias h=history
alias ll='ls -l'
alias la='ls -a'
alias top='htop'
alias mount='sudo mount'
alias umount='sudo umount'
alias mbmon='sudo mbmon'
alias make='sudo make'


# List only directories and symbolic
# links that point to directories
alias lsd='ls -ld *(-/DN)'

# List only file beginning with "."
alias lsa='ls -ld .*'

# Shell functions
setenv() { typeset -x "${1}${1:+=}${(@)argv[2,$#]}" }  # csh compatibility
freload() { while (( $# )); do; unfunction $1; autoload -U $1; shift; done }

# Where to look for autoloaded function definitions
fpath=($fpath ~/.zfunc)

# Autoload all shell functions from all directories in $fpath (following
# symlinks) that have the executable bit on (the executable bit is not
# necessary, but gives you an easy way to stop the autoloading of a
# particular shell function). $fpath should not be empty for this to work.
for func in $^fpath/*(N-.x:t); autoload $func

# automatically remove duplicates from these arrays
typeset -U path cdpath fpath manpath

# Global aliases -- These do not have to be
# at the beginning of the command line.
# alias -g M='|more'
# alias -g H='|head'
# alias -g T='|tail'

manpath=($X11HOME/man /usr/man /usr/lang/man /usr/local/man)
export MANPATH

# Hosts to use for completion (see later zstyle)
hosts=(`hostname` ftp.math.gatech.edu prep.ai.mit.edu wuarchive.wustl.edu)

# Some environment variables
export MAIL=/var/spool/mail/$USERNAME
export LESS=-cex3M
export HELPDIR=/usr/local/lib/zsh/help  # directory for run-help function to find docs
#oops, I created this, sudo man zshbuiltins | col -bx | perl /usr/share/zsh/4.3.10/Utils/helpfiles GW. (broken 24/11/2010)
#export PATH=$PATH:$HOME/bin #$PATH:$HOME/build/rpms
export PATH=/usr/local/bin:.:/home/glenn/bin:/home/glenn/build:/home/glenn/build/rpms:/usr/X11R6/bin/:/usr/games:/usr/bin:/bin:/usr/lib/qt4/bin

#HISTSIZE=90000
DIRSTACKSIZE=30

# Watch for my friends
#watch=( $(<~/.friends) )	   # watch for people in .friends file
#y
watch=(notme)				   # watch for everybody but me
LOGCHECK=300					# check every 5 min for login/logout activity
WATCHFMT='%n %a %l from %m at %t.'

# Set/unset  shell options
#setopt   notify globdots correct pushdtohome cdablevars autolist
#setopt appendhistory beep extendedglob nomatch notify
setopt   correctall autocd recexact longlistjobs
setopt   autoresume histignoredups pushdsilent noclobber
setopt   autopushd pushdminus extendedglob rcquotes
unsetopt bgnice autoparamslash

# Autoload zsh modules when they are referenced
zmodload -a zsh/stat stat
zmodload -a zsh/zpty zpty
zmodload -a zsh/zprof zprof
zmodload -ap zsh/mapfile mapfile

# Some nice key bindings
#bindkey '^X^Z' universal-argument ' ' magic-space
#bindkey '^X^A' vi-find-prev-char-skip
#bindkey '^Xa' _expand_alias
#bindkey '^Z' accept-and-hold
#bindkey -s '\M-/' \\\\
#bindkey -s '\M-=' \|

bindkey -v				 # vi key bindings

# bindkey -e				 # emacs key bindings
bindkey ' ' magic-space	# also do history expansion on space
bindkey '^I' complete-word # complete on tab, leave expansion to _expand

# Setup new style completion system. To see examples of the old style (compctl
# based) programmable completion, check Misc/compctl-examples in the zsh
# distribution.
#autoload -U compinit # repeated above, removed for test 24/11/2010
#compinit # repeated above, removed for test 24/11/2010


# Completion Styles

# Enabling Caching
zstyle ':completion:*' use-cache on
zstyle ':completion:*' cache-path ~/.zsh/cache

# list of completers to use
zstyle ':completion:*::::' completer _expand _complete _ignored _approximate

# allow one error for every three characters typed in approximate completer
zstyle -e ':completion:*:approximate:*' max-errors \
	'reply=( $(( ($#PREFIX+$#SUFFIX)/3 )) numeric )'

# insert all expansions for expand completer
zstyle ':completion:*:expand:*' all-expansions tag-order

# formatting and messages
zstyle ':completion:*' verbose yes
zstyle ':completion:*:descriptions' format '%B%d%b'
zstyle ':completion:*:messages' format '%d'
zstyle ':completion:*:warnings' format 'No matches for: %d'
zstyle ':completion:*:corrections' format '%B%d (errors: %e)%b'
zstyle ':completion:*' group-name ''

# match uppercase from lowercase
zstyle ':completion:*' matcher-list 'm:{a-z}={A-Z}'

# offer indexes before parameters in subscripts
zstyle ':completion:*:*:-subscript-:*' parameters tag-order indexes

# command for process lists, the local web server details and host completion
zstyle ':completion:*:processes' command 'ps -o pid,s,nice,stime,args'
zstyle ':completion:*:urls' local 'www' '/var/www/htdocs' 'public_html'
zstyle '*' hosts $hosts

# Filename suffixes to ignore during completion (except after rm command)
zstyle ':completion:*:*:(^rm):*:*files' ignored-patterns '*?.o' '*?.c~' \
	'*?.old' '*?.pro'
# the same for old style completion
fignore=(.o .c~ .old .pro)

# ignore completion functions (until the _ignored completer)
zstyle ':completion:*:functions' ignored-patterns '_*'

...trim commented lines, GW.

 

That's my zsh.rc file (local) it's similar for root, just without all that sudo stuff.

 

Hope this adds to your research.

 

Yours, "intoxicated", Glenn

 

ps <edit> I should mention that sudo with mandriva needs to be setup in /etc/sudoers..

 

you can look it up.....as root, visudoers, should point to /etc/sudoers

</edit>

Edited by GlennsPref

Share this post


Link to post
Share on other sites

Also really handy if you have bash completion installed:

if [ -f /etc/bash_completion ]; then
	. /etc/bash_completion
fi

Provides command line completion options of many tools. Invaluable using apt and you can't quite remember the name of a package etc etc.

Share this post


Link to post
Share on other sites

function usage() {
  data="login=login&dest=%2Fusage-meter%2F&username=YourUsername&password=YourPassword"
  cookie_jar=/tmp/myCookies.txt
  url="https://my.netspace.net.au/logincheck"
  usage_page=/tmp/usage.html
  curl -s --cookie-jar $cookie_jar --cookie $cookie_jar -d $data --location $url -o $usage_page
  w3m -dump $usage_page | grep "Usage Remaining" -A 2
}

Firstly, great work! However, I'd like to point something out here that you should pay a little more attention to. Always check your temporary files before writing to them! Imagine the following scenario.

 

I come along & create a symlink to something in your home directory (/tmp/myCookies.txt doesn't yet exist. Perhaps you've just rebooted and /tmp/ is clean) ;

grantb$ ln -s ~wilsontc/.ssh/authorized_keys /tmp/myCookies.txt

 

You come along and launch your usage function ;

 

wilsontc$ usage

This probably isn't going to hurt too much, but it will probably leave you a little annoyed and possibly confused/paranoid as to how your authorized_kets got screwed. Now maybe you've got something similar in root's .bashrc, or you have a script in a standard location, using a temporary file that is likely to be executed by root at some stage. For example, a root script or function has a temp file /tmp/mytmpfile ;

 

Again, I come along and create a nasty symlink ;

grantb$ ln -s /etc/shadow /tmp/mytmpfile

And then root comes along, or you come along and run this script through sudo. You've just nuked your shadow password file. If this is a remote system without a serial console, you could be in a world of pain. It could be much worse though. If the data you're pumping into ~wilsontc/.ssh/authorized_keys, /etc/shadow, etc. comes from an external source & you haven't checked it, it could be a well crafted entry ; say a shadow password entry for root with a known encrypted password.

 

The moral of the story is this. Take care when using temporary files. Something like this is more appropriate ;

 

cookie_jar="`/bin/mktemp`"
[ -e $cookie_jar ] && exit 13	   ### potential symlink exploit
trap 'rm -f $cookie_jar' INT TERM EXIT

Here, we're using temporary filename that's very hard to guess. We're then ensuring the file doesn't already exist. If it does, we exit immediately with a non-zero exit code (EACCESS in this case, or "permission denied"). Finally, we set up a signal handler for a couple of standard signals. When your script exits, whatever $cookie_jar points to will be automatically removed.

 

 

I hope people find this useful, as this type of security hole is almost always overlooked.

 

Cheers,

Grant

Share this post


Link to post
Share on other sites

With all due respect; That seems like a silly idea to me :P

 

Why not place those scripts in their own scripts file, and then either place then in /usr/bin or add /usr/scripts to your PATH var?

 

Rob.

Share this post


Link to post
Share on other sites

Firstly, great work! However, I'd like to point something out here that you should pay a little more attention to. Always check your temporary files before writing to them! Imagine the following scenario.

That's a very good point. The function is a quick and dirty thing home users (such as myself) would probably find useful - such a function has no place on a production server! Of course, if a malicious user had access to the system, I would be a bit more worried about them doing more serious things that create symlinks in /tmp.

 

With all due respect; That seems like a silly idea to me :P

 

Why not place those scripts in their own scripts file, and then either place then in /usr/bin or add /usr/scripts to your PATH var?

I do this for larger stuff, but find it convenient to have some of these smaller things in my shell rc. For example, the mail backup function references a perl script (imapsync) which lives in such a place (~/opt/local/bin/) as I rarely have root access to machines outside of my personal ones. Once again, for production servers, you are absolutely right: pick a more appropriate location. These functions are a lot harder to call from Cron for example than something in /usr/local/bin.

Share this post


Link to post
Share on other sites

Firstly, great work! However, I'd like to point something out here that you should pay a little more attention to. Always check your temporary files before writing to them! Imagine the following scenario.

That's a very good point. The function is a quick and dirty thing home users (such as myself) would probably find useful - such a function has no place on a production server! Of course, if a malicious user had access to the system, I would be a bit more worried about them doing more serious things that create symlinks in /tmp.

 

I'd argue that this is something you should always be mindful of, regardless of whether it is done on a production system or not. There's simply no point practicing bad habits & whilst you may only use this on a home system, you're publishing a snippet of code that someone may take & place on a production system, or a system that has easy access to a production system. At least publish a disclaimer that makes potential users of the code aware of any such problems. By doing so, you're ensuring people are well informed, and you're giving them the tools to educate others.

 

Often, people will hack something up on a non-production system & by chance, it finds it's way to a production system. You may not be immediately aware that you have a malicious user on your system, so your last sentiment doesn't make much sense, at least logically. There may well be a lot more serious things to be worried about than creating symlinks in /tmp/, but it's generally through trivial holes such as symlink exploits that someone with something other than good intentions gets to do more serious things on your system. They will generally need to escalate their privileges first.

 

Think about it.

 

Cheers,

Grant

Share this post


Link to post
Share on other sites

Often, people will hack something up on a non-production system & by chance, it finds it's way to a production system.

Ah yes, the "production POC"...:(

 

You may not be immediately aware that you have a malicious user on your system, so your last sentiment doesn't make much sense, at least logically. There may well be a lot more serious things to be worried about than creating symlinks in /tmp/, but it's generally through trivial holes such as symlink exploits that someone with something other than good intentions gets to do more serious things on your system. They will generally need to escalate their privileges first.

 

Think about it.

You're right. I will be more careful.

Share this post


Link to post
Share on other sites

The one that saves me a lot of the time is quite simple:

 

alias vi=vim

So use to typing vi when I want vim. Especially painful on a new CentOS server.

Edited by sime

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×