mirror of https://github.com/tLDP/LDP
new
This commit is contained in:
parent
98703ac4a1
commit
1d01a6c06d
|
@ -0,0 +1,146 @@
|
|||
#!/bin/bash
|
||||
# Draw-box.sh: Drawing a box using ASCII characters.
|
||||
|
||||
# Script by Stefano Palmeri, with minor editing by document author.
|
||||
# Used in the "ABS Guide" with permission.
|
||||
|
||||
|
||||
######################################################################
|
||||
### draw_box function doc ###
|
||||
|
||||
# The "draw_box" function lets the user
|
||||
#+ draw a box into a terminal.
|
||||
#
|
||||
# Usage: draw_box ROW COLUMN HEIGHT WIDTH [COLOR]
|
||||
# ROW and COLUMN represent the position
|
||||
#+ of the upper left angle of the box you're going to draw.
|
||||
# ROW and COLUMN must be greater than 0
|
||||
#+ and less than current terminal dimension.
|
||||
# HEIGHT is the number of rows of the box, and must be > 0.
|
||||
# HEIGHT + ROW must be <= than current terminal height.
|
||||
# WIDTH is the number of columns of the box and must be > 0.
|
||||
# WIDTH + COLUMN must be <= than current terminal width.
|
||||
#
|
||||
# E.g.: If your terminal dimension is 20x80,
|
||||
# draw_box 2 3 10 45 is good
|
||||
# draw_box 2 3 19 45 has bad HEIGHT value (19+2 > 20)
|
||||
# draw_box 2 3 18 78 has bad WIDTH value (78+3 > 80)
|
||||
#
|
||||
# COLOR is the color of the box frame.
|
||||
# This is the 5th argument and is optional.
|
||||
# 0=black 1=red 2=green 3=tan 4=blue 5=purple 6=cyan 7=white.
|
||||
# If you pass the function bad arguments,
|
||||
#+ it will just exit with code 65,
|
||||
#+ and no messages will be printed on stderr.
|
||||
#
|
||||
# Clear the terminal before you start to draw a box.
|
||||
# The clear command is not contained within the function.
|
||||
# This allows the user to draw multiple boxes, even overlapping ones.
|
||||
|
||||
### end of draw_box function doc ###
|
||||
######################################################################
|
||||
|
||||
draw_box(){
|
||||
|
||||
#=============#
|
||||
HORZ="-"
|
||||
VERT="|"
|
||||
CORNER_CHAR="+"
|
||||
|
||||
MINARGS=4
|
||||
E_BADARGS=65
|
||||
#=============#
|
||||
|
||||
|
||||
if [ $# -lt "$MINARGS" ]; then # If args are less than 4, exit.
|
||||
exit $E_BADARGS
|
||||
fi
|
||||
|
||||
# Looking for non digit chars in arguments.
|
||||
# Probably it could be done better (exercise for the reader?).
|
||||
if echo $@ | tr -d [:blank:] | tr -d [:digit:] | grep . &> /dev/null; then
|
||||
exit $E_BADARGS
|
||||
fi
|
||||
|
||||
BOX_HEIGHT=`expr $3 - 1` # -1 correction needed because angle char "+" is
|
||||
BOX_WIDTH=`expr $4 - 1` #+ a part of both box height and width.
|
||||
T_ROWS=`tput lines` # Define current terminal dimension
|
||||
T_COLS=`tput cols` #+ in rows and columns.
|
||||
|
||||
if [ $1 -lt 1 ] || [ $1 -gt $T_ROWS ]; then # Start checking if arguments
|
||||
exit $E_BADARGS #+ are correct.
|
||||
fi
|
||||
if [ $2 -lt 1 ] || [ $2 -gt $T_COLS ]; then
|
||||
exit $E_BADARGS
|
||||
fi
|
||||
if [ `expr $1 + $BOX_HEIGHT + 1` -gt $T_ROWS ]; then
|
||||
exit $E_BADARGS
|
||||
fi
|
||||
if [ `expr $2 + $BOX_WIDTH + 1` -gt $T_COLS ]; then
|
||||
exit $E_BADARGS
|
||||
fi
|
||||
if [ $3 -lt 1 ] || [ $4 -lt 1 ]; then
|
||||
exit $E_BADARGS
|
||||
fi # End checking arguments.
|
||||
|
||||
plot_char(){ # Function within a function.
|
||||
echo -e "\E[${1};${2}H"$3
|
||||
}
|
||||
|
||||
echo -ne "\E[3${5}m" # Set box frame color, if defined.
|
||||
|
||||
# start drawing the box
|
||||
|
||||
count=1 # Draw vertical lines using
|
||||
for (( r=$1; count<=$BOX_HEIGHT; r++)); do #+ plot_char function.
|
||||
plot_char $r $2 $VERT
|
||||
let count=count+1
|
||||
done
|
||||
|
||||
count=1
|
||||
c=`expr $2 + $BOX_WIDTH`
|
||||
for (( r=$1; count<=$BOX_HEIGHT; r++)); do
|
||||
plot_char $r $c $VERT
|
||||
let count=count+1
|
||||
done
|
||||
|
||||
count=1 # Draw horizontal lines using
|
||||
for (( c=$2; count<=$BOX_WIDTH; c++)); do #+ plot_char function.
|
||||
plot_char $1 $c $HORZ
|
||||
let count=count+1
|
||||
done
|
||||
|
||||
count=1
|
||||
r=`expr $1 + $BOX_HEIGHT`
|
||||
for (( c=$2; count<=$BOX_WIDTH; c++)); do
|
||||
plot_char $r $c $HORZ
|
||||
let count=count+1
|
||||
done
|
||||
|
||||
plot_char $1 $2 $CORNER_CHAR # Draw box angles.
|
||||
plot_char $1 `expr $2 + $BOX_WIDTH` +
|
||||
plot_char `expr $1 + $BOX_HEIGHT` $2 +
|
||||
plot_char `expr $1 + $BOX_HEIGHT` `expr $2 + $BOX_WIDTH` +
|
||||
|
||||
echo -ne "\E[0m" # Restore old colors.
|
||||
|
||||
P_ROWS=`expr $T_ROWS - 1` # Put the prompt at bottom of the terminal.
|
||||
|
||||
echo -e "\E[${P_ROWS};1H"
|
||||
}
|
||||
|
||||
|
||||
# Now, let's try drawing a box.
|
||||
clear # Clear the terminal.
|
||||
R=2 # Row
|
||||
C=3 # Column
|
||||
H=10 # Height
|
||||
W=45 # Width
|
||||
col=1 # Color (red)
|
||||
draw_box $R $C $H $W $col # Draw the box.
|
||||
|
||||
exit 0
|
||||
|
||||
# Exercise:
|
||||
# --------
|
||||
# Add the option of printing text within the drawn box.
|
|
@ -0,0 +1,52 @@
|
|||
#!/bin/bash
|
||||
# avoid-subshell.sh
|
||||
# Suggested by Matthew Walker.
|
||||
|
||||
Lines=0
|
||||
|
||||
echo
|
||||
|
||||
cat myfile.txt | while read line;
|
||||
do {
|
||||
echo $line
|
||||
(( Lines++ )); # Incremented values of this variable
|
||||
#+ inaccessible outside loop.
|
||||
# Subshell problem.
|
||||
}
|
||||
done
|
||||
|
||||
echo "Number of lines read = $Lines" # 0
|
||||
# Wrong!
|
||||
|
||||
echo "------------------------"
|
||||
|
||||
|
||||
exec 3<> myfile.txt
|
||||
while read line <&3
|
||||
do {
|
||||
echo "$line"
|
||||
(( Lines++ )); # Incremented values of this variable
|
||||
#+ accessible outside loop.
|
||||
# No subshell, no problem.
|
||||
}
|
||||
done
|
||||
exec 3>&-
|
||||
|
||||
echo "Number of lines read = $Lines" # 8
|
||||
|
||||
echo
|
||||
|
||||
exit 0
|
||||
|
||||
# Lines below not seen by script.
|
||||
|
||||
$ cat myfile.txt
|
||||
|
||||
Line 1.
|
||||
Line 2.
|
||||
Line 3.
|
||||
Line 4.
|
||||
Line 5.
|
||||
Line 6.
|
||||
Line 7.
|
||||
Line 8.
|
|
@ -0,0 +1,144 @@
|
|||
#! /bin/bash
|
||||
# is-spammer.sh: Identifying spam domains
|
||||
#
|
||||
# This is a simplified version of the "is_spammer.bash
|
||||
#+ script in the Contributed Scripts appendix.
|
||||
|
||||
# is-spammer <domain.name>
|
||||
# $Id$
|
||||
|
||||
# Uses an external program: 'dig'
|
||||
# Tested with version: 9.2.4rc5
|
||||
|
||||
# Uses functions.
|
||||
# Uses IFS to parse strings by assignment into arrays.
|
||||
# And even does something useful: checks e-mail blacklists.
|
||||
|
||||
# Use the domain.name(s) from the text body:
|
||||
# http://www.good_stuff.spammer.biz/just_ignore_everything_else
|
||||
# ^^^^^^^^^^^
|
||||
# Or the domain.name(s) from any e-mail address:
|
||||
# Really_Good_Offer@spammer.biz
|
||||
# ^^^^^^^^^^^
|
||||
# as the only argument to this script.
|
||||
#(PS: have your Inet connection running)
|
||||
#
|
||||
# So, to invoke this script in the above two instances:
|
||||
# is-spammer.sh spammer.biz
|
||||
|
||||
|
||||
# Whitespace == :Space:Tab:Line Feed:Carriage Return:
|
||||
WSP_IFS=$'\x20'$'\x09'$'\x0A'$'\x0D'
|
||||
|
||||
# No Whitespace == Line Feed:Carriage Return
|
||||
No_WSP=$'\x0A'$'\x0D'
|
||||
|
||||
# Field separator for dotted decimal ip addresses
|
||||
ADR_IFS=${No_WSP}'.'
|
||||
|
||||
# Get the dns text resource record
|
||||
# get_txt <error_code> <list_query>
|
||||
get_txt() {
|
||||
|
||||
# parse $1 by assignment at the dots
|
||||
local -a dns
|
||||
IFS=$ADR_IFS
|
||||
dns=( $1 )
|
||||
IFS=$WSP_IFS
|
||||
if [ "${dns[0]}" == '127' ]
|
||||
then
|
||||
# see if there is a reason
|
||||
echo $(dig +short $2 -t txt)
|
||||
fi
|
||||
}
|
||||
|
||||
# Get the dns address resource record
|
||||
# chk_adr <rev_dns> <list_server>
|
||||
chk_adr() {
|
||||
local reply
|
||||
local server
|
||||
local reason
|
||||
|
||||
server=${1}${2}
|
||||
reply=$( dig +short ${server} )
|
||||
|
||||
# if reply might be an error code . . .
|
||||
if [ ${#reply} -gt 6 ]
|
||||
then
|
||||
reason=$(get_txt ${reply} ${server} )
|
||||
reason=${reason:-${reply}}
|
||||
fi
|
||||
echo ${reason:-' not blacklisted.'}
|
||||
}
|
||||
|
||||
# Need to get the IP address from the name.
|
||||
echo 'Get address of: '$1
|
||||
ip_adr=$(dig +short $1)
|
||||
dns_reply=${ip_adr:-' no answer '}
|
||||
echo ' Found address: '${dns_reply}
|
||||
|
||||
# A valid reply is at least 4 digits plus 3 dots.
|
||||
if [ ${#ip_adr} -gt 6 ]
|
||||
then
|
||||
echo
|
||||
declare query
|
||||
|
||||
# Parse by assignment at the dots.
|
||||
declare -a dns
|
||||
IFS=$ADR_IFS
|
||||
dns=( ${ip_adr} )
|
||||
IFS=$WSP_IFS
|
||||
|
||||
# Reorder octets into dns query order.
|
||||
rev_dns="${dns[3]}"'.'"${dns[2]}"'.'"${dns[1]}"'.'"${dns[0]}"'.'
|
||||
|
||||
# See: http://www.spamhaus.org (Conservative, well maintained)
|
||||
echo -n 'spamhaus.org says: '
|
||||
echo $(chk_adr ${rev_dns} 'sbl-xbl.spamhaus.org')
|
||||
|
||||
# See: http://ordb.org (Open mail relays)
|
||||
echo -n ' ordb.org says: '
|
||||
echo $(chk_adr ${rev_dns} 'relays.ordb.org')
|
||||
|
||||
# See: http://www.spamcop.net/ (You can report spammers here)
|
||||
echo -n ' spamcop.net says: '
|
||||
echo $(chk_adr ${rev_dns} 'bl.spamcop.net')
|
||||
|
||||
# # # other blacklist operations # # #
|
||||
|
||||
# See: http://cbl.abuseat.org.
|
||||
echo -n ' abuseat.org says: '
|
||||
echo $(chk_adr ${rev_dns} 'cbl.abuseat.org')
|
||||
|
||||
# See: http://dsbl.org/usage (Various mail relays)
|
||||
echo
|
||||
echo 'Distributed Server Listings'
|
||||
echo -n ' list.dsbl.org says: '
|
||||
echo $(chk_adr ${rev_dns} 'list.dsbl.org')
|
||||
|
||||
echo -n ' multihop.dsbl.org says: '
|
||||
echo $(chk_adr ${rev_dns} 'multihop.dsbl.org')
|
||||
|
||||
echo -n 'unconfirmed.dsbl.org says: '
|
||||
echo $(chk_adr ${rev_dns} 'unconfirmed.dsbl.org')
|
||||
|
||||
else
|
||||
echo
|
||||
echo 'Could not use that address.'
|
||||
fi
|
||||
|
||||
exit 0
|
||||
|
||||
# Exercises:
|
||||
# --------
|
||||
|
||||
# 1) Check arguments to script,
|
||||
# and exit with appropriate error message if necessary.
|
||||
|
||||
# 2) Check if on-line at invocation of script,
|
||||
# and exit with appropriate error message if necessary.
|
||||
|
||||
# 3) Substitute generic variables for "hard-coded" BHL domains.
|
||||
|
||||
# 4) Set a time-out for the script using the "+time=" option
|
||||
to the 'dig' command.
|
File diff suppressed because it is too large
Load Diff
|
@ -0,0 +1,30 @@
|
|||
#!/bin/bash
|
||||
# prepend.sh: Add text at beginning of file.
|
||||
#
|
||||
# Example contributed by Kenny Stauffer,
|
||||
# and slightly modified by document author.
|
||||
|
||||
|
||||
E_NOSUCHFILE=65
|
||||
|
||||
read -p "File: " file # -p arg to 'read' displays prompt.
|
||||
if [ ! -e "$file" ]
|
||||
then # Bail out if no such file.
|
||||
echo "File $file not found."
|
||||
exit $E_NOSUCHFILE
|
||||
fi
|
||||
|
||||
read -p "Title: " title
|
||||
cat - $file <<<$title > $file.new
|
||||
|
||||
echo "Modified file is $file.new"
|
||||
|
||||
exit 0
|
||||
|
||||
# from "man bash"
|
||||
# Here Strings
|
||||
# A variant of here documents, the format is:
|
||||
#
|
||||
# <<<word
|
||||
#
|
||||
# The word is expanded and supplied to the command on its standard input.
|
|
@ -0,0 +1,69 @@
|
|||
#!/bin/bash
|
||||
# remote.bash: Using ssh.
|
||||
|
||||
# This example by Michael Zick.
|
||||
# Used with permission.
|
||||
|
||||
|
||||
# Presumptions:
|
||||
# ------------
|
||||
# fd-2 isn't being captured ( '2>/dev/null' ).
|
||||
# ssh/sshd presumes stderr ('2') will display to user.
|
||||
#
|
||||
# sshd is running on your machine.
|
||||
# For any 'standard' distribution, it probably is,
|
||||
#+ and without any funky ssh-keygen having been done.
|
||||
|
||||
# Try ssh to your machine from the command line:
|
||||
#
|
||||
# $ ssh $HOSTNAME
|
||||
# Without extra set-up you'll be asked for your password.
|
||||
# enter password
|
||||
# when done, $ exit
|
||||
#
|
||||
# Did that work? If so, you're ready for more fun.
|
||||
|
||||
# Try ssh to your machine as 'root':
|
||||
#
|
||||
# $ ssh -l root $HOSTNAME
|
||||
# When asked for password, enter root's, not yours.
|
||||
# Last login: Tue Aug 10 20:25:49 2004 from localhost.localdomain
|
||||
# Enter 'exit' when done.
|
||||
|
||||
# The above gives you an interactive shell.
|
||||
# It is possible for sshd to be set up in a 'single command' mode,
|
||||
#+ but that is beyond the scope of this example.
|
||||
# The only thing to note is that the following will work in
|
||||
#+ 'single command' mode.
|
||||
|
||||
|
||||
# A basic, write stdout (local) command.
|
||||
|
||||
ls -l
|
||||
|
||||
# Now the same basic command on a remote machine.
|
||||
# Pass a different 'USERNAME' 'HOSTNAME' if desired:
|
||||
USER=${USERNAME:-$(whoami)}
|
||||
HOST=${HOSTNAME:-$(hostname)}
|
||||
|
||||
# Now excute the above command line on the remote host,
|
||||
#+ with all transmissions encrypted.
|
||||
|
||||
ssh -l ${USER} ${HOST} " ls -l "
|
||||
|
||||
# The expected result is a listing of your username's home
|
||||
#+ directory on the remote machine.
|
||||
# To see any difference, run this script from somewhere
|
||||
#+ other than your home directory.
|
||||
|
||||
# In other words, the Bash command is passed as a quoted line
|
||||
#+ to the remote shell, which executes it on the remote machine.
|
||||
# In this case, sshd does ' bash -c "ls -l" ' on your behalf.
|
||||
|
||||
# For information on topics such as not having to enter a
|
||||
#+ password/passphrase for every command line, see
|
||||
#+ man ssh
|
||||
#+ man ssh-keygen
|
||||
#+ man sshd_config.
|
||||
|
||||
exit 0
|
Loading…
Reference in New Issue