Sudo example
printf 'IP\tHost\n' | ssh -q host "sudo su - root -c 'cat - >> /etc/hosts'"
Search This Blog
Wednesday, October 23, 2019
Wednesday, August 28, 2019
Debug bash scripts
Place the below lines near the top of the script.
Call debuggerIt in the script from a point where debugging should take place or at the start of the script execution.
OUTBASE="/tmp/${USER}/${0##*/}.log"
#-------------------------------------------
debuggerIt()
{
test -d ${OUTBASE} || mkdir -p ${OUTBASE}
exec 5> ${OUTBASE}/${0##*/}.debug
BASH_XTRACEFD="5"
PS4='$LINENO: '
set -x
}
#-------------------------------------------
.
more functions
.
.
start of execution.
debuggerIt
more commands
Thursday, April 25, 2019
Remote tar
Copy a files from remote location using a file list.
ssh host "cat fileList | xargs tar cf -" | tar xvf -
Wednesday, May 30, 2018
Using regex in vim
Search and replace using quantifiers.
Example:
This will erase all checkID's with single and/or double digits.
:%s/"checkID":[0-9]\{1,2\} //g
Before
Running command.
Wednesday, August 17, 2016
Split the $PATH variable into seperate lines.
To split the $PATH variable into separate lines can be useful, specially if you have a very long $PATH. Also good for showing double entries.
echo $PATH | awk '{split($0,a,":")}; END { for (p in a) printf "%s\n",a[p];}'
echo $PATH | awk '{split($0,a,":")}; END { for (p in a) printf "%s\n",a[p];}'
Monday, February 08, 2016
Man in the middle remote copy files.
To copy files between 2 remote servers using man in the middle server using tar.
ssh server1 "cd /data;tar cf - ." | ssh server2 "cd /data;tar xvf -"
ssh server1 "cd /data;tar cf - ." | ssh server2 "cd /data;tar xvf -"
Friday, September 18, 2015
Using commandline options in shell scripts
Sample code using getopt
while getopts :f:cuh OP
do
case ${OP} in
f) SERVERS=${OPTARG}
;;
c) if [ "${SERVERS}" != "false" ]
then
check
else
usage
fi
;;
u) if [ "${SERVERS}" != "false" ]
then
cd ${BASE}
fi
;;
*|h) usage
;;
esac
done
while getopts :f:cuh OP
do
case ${OP} in
f) SERVERS=${OPTARG}
;;
c) if [ "${SERVERS}" != "false" ]
then
check
else
usage
fi
;;
u) if [ "${SERVERS}" != "false" ]
then
cd ${BASE}
fi
;;
*|h) usage
;;
esac
done
Tuesday, August 11, 2015
Replace spaces with commas using sed
sed 's/ \{1,\}/,/g'
Friday, May 08, 2015
Use variable in search pattern in awk
Passing a variable to awk and using it in the search pattern.
awk -v string="$STRING" -f script.awk [filename]
Script extract:
$0 ~ string {
print $0;
}
Note:
On Solaris use nawk.
awk -v string="$STRING" -f script.awk [filename]
Script extract:
$0 ~ string {
print $0;
}
Note:
On Solaris use nawk.
Tuesday, April 14, 2015
Match IP numbers with regex.
Print any
IP address talking on port 21 in coming or out going.
Change the port number to any port that needs to be matched.
netstat -na | perl -ne '/((\d{1,3}\.){4}21)/ && print ;'
Monday, January 26, 2015
Variable assigments
Validating positional parameters.
The below example will print a usage message if there is no 2nd position paramaeter.
PACKAGE=${2:?$(usage ${SERVERLIST} [Package name:Version:Package file[:Dependent service]])}
The below example will print a usage message if there is no 2nd position paramaeter.
PACKAGE=${2:?$(usage ${SERVERLIST} [Package name:Version:Package file[:Dependent service]])}
Wednesday, August 06, 2014
awk and ifconfig
Print interface name and network config oneliner.
#ifconfig -a | awk '/^[a-z]/{intf=sprintf("%-10s", $1);getline; if($1=="inet") printf "%-6s %s\n", intf, $0;}'
inet addr:172.19.2.26 Bcast:172.19.2.255 Mask:255.255.255.0
inet addr:127.0.0.1 Mask:255.0.0.0
Wednesday, October 16, 2013
Colourise your shell scripts
#!/usr/bin/ksh
#----------------------------------------------------------------------------------------------
# Define Colours for scripts.
#
# Written : Stan Lovisa
# Date : 26-10-2012
# Mod :
#
# Black 0;30 Dark Gray 1;30
# Blue 0;34 Light Blue 1;34
# Green 0;32 Light Green 1;32
# Cyan 0;36 Light Cyan 1;36
# Red 0;31 Light Red 1;31
# Purple 0;35 Light Purple 1;35
# Brown 0;33 Yellow 1;33
# Light Gray 0;37 White 1;37
# 0 normal, 1 bold, 4 underline, 7 reverse video
#----------------------------------------------------------------------------------------------
#
# Notes. How to use.
#
# printf "${TXTGREEN}" To switch on colour.
# printf "${TXTNC}" To switch off colour.
#
#----------------------------------------------------------------------------------------------
TXTRED="\033[1;31m"
TXTREDRV="\033[7;31m"
TXTLBLUE="\033[1;34m"
TXTBLUERV="\033[7;36m"
TXTGREEN="\033[0;32m"
TXTGREENRV="\033[7;32m"
TXTYELLOW="\033[1;33m"
TXTYELLOWRV="\033[7;33m"
TXTNC="\033[1;0m"
#----------------------------------------------------------------------------------------------
# Define Colours for scripts.
#
# Written : Stan Lovisa
# Date : 26-10-2012
# Mod :
#
# Black 0;30 Dark Gray 1;30
# Blue 0;34 Light Blue 1;34
# Green 0;32 Light Green 1;32
# Cyan 0;36 Light Cyan 1;36
# Red 0;31 Light Red 1;31
# Purple 0;35 Light Purple 1;35
# Brown 0;33 Yellow 1;33
# Light Gray 0;37 White 1;37
# 0 normal, 1 bold, 4 underline, 7 reverse video
#----------------------------------------------------------------------------------------------
#
# Notes. How to use.
#
# printf "${TXTGREEN}" To switch on colour.
# printf "${TXTNC}" To switch off colour.
#
#----------------------------------------------------------------------------------------------
TXTRED="\033[1;31m"
TXTREDRV="\033[7;31m"
TXTLBLUE="\033[1;34m"
TXTBLUERV="\033[7;36m"
TXTGREEN="\033[0;32m"
TXTGREENRV="\033[7;32m"
TXTYELLOW="\033[1;33m"
TXTYELLOWRV="\033[7;33m"
TXTNC="\033[1;0m"
Thursday, July 11, 2013
SMTP: Using smtp commands in a
shell script when sendmail isn't setup.
LOG="/tmp/${0##*/}.log"
SUBJECT="${0##*/}"
MAILSERVER=mailhost
PORT="25"
MAILFROM="user1@domain
user2@domain"
MAILLIST="email@somedomain"
#------------------------------------------------------------------------
mailto()
{
exec
3<>/dev/tcp/$MAILSERVER/$PORT
if [ $? -ne 0
]
then
echo
echo "ERROR: Cannot connect to
the Mail Server"
echo "Please check the
servername and/or the port number"
exit
fi
echo -en "HELO ${MAILSERVER}\r\n"
>&3
echo -en "MAIL FROM:$MAILFROM\r\n"
>&3
for MAILTO in
${MAILLIST}
do
echo -en "RCPT TO:$MAILTO\r\n"
>&3
done
echo -en "DATA\r\n"
>&3
echo -en "Subject: $SUBJECT\r\n\r\n"
>&3
echo -en "$(cat ${LOG})\r\n"
>&3
echo -en ".\r\n"
>&3
echo -en "QUIT\r\n"
>&3
cat <&3
}
#------------------------------------------------------------------------
Some commands
.
.
mailto
Tuesday, September 20, 2011
AD group member listing.
List member of a AD group from XP
net group G_UNIX_SCHED /domain
or list user attributes.
net user <user id> /domain
Tuesday, August 30, 2011
Awk: Using getline to read a file in BEGIN
Example reading in the password file into an array for later use.
BEGIN {
FS=":";
while ((getline < "/etc/passwd") >;0 )
{
Alias[$1]=$0;
}
}
Tuesday, June 28, 2011
Shell Scripting: basename variable
"${0##*/}" is the equivalent of "basename $0".
I.e. From /usr/bin/sh we get just sh.
Also try this to execute commands $(shell command)
I.e. From /usr/bin/sh we get just sh.
Also try this to execute commands $(shell command)
Thursday, December 23, 2010
Perl script options
Using getopt in perl scripts.
Sample code
Output
or
This method won't report an error if an option is emitted.
Sample code
#!/usr/bin/perl
#-------------------------------------------------------------------------
# Using getop
#-------------------------------------------------------------------------
use Getopt::Std;
getopt('cw');
my @op = `./x.sh -c $opt_c -w $opt_w`;
foreach (@op)
{
#printf "-c %s\t-w %s\n", $opt_c,$opt_w;
print $_;
}
Output
./x.pl -c 3 -w 55
in ./x.sh.sh : -c 3 -w 55
-c 3
-w 55
or
my %options=();
#Pre set default values
my $critv=95;
my $warnv=85;
getopts("c:w:", \%options);
$critv = $options{c} if defined $options{c};
$warnv = $options{w} if defined $options{w};
This method won't report an error if an option is emitted.
Wednesday, December 01, 2010
awk convert time string to time format
This will take a time string HHMMSS (121525) and format it as 12:15:25
A sample line from a.txt:
Will produce the following out put:
A sample line from a.txt:
01201 095440 sd10 3393.2 1.1 13572.8 4.4 0.0 1.0 0.3 2 91
awk '{h=substr($2,1,2);\
m=substr($2,3,2);\
s=substr($2,5,2);\
$2=sprintf("%s:%s:%s",h,m,s);\
print $0;}' a.txt | head Will produce the following out put:
101201 09:54:40 sd10 3393.2 1.1 13572.8 4.4 0.0 1.0 0.3 2 91
Thursday, September 02, 2010
Midnight commander
Startup Midnight commander with each panel in a pre-detirmined location.
mc /tmp /#ftp:guest@remote-host.com/pub/dir
mc /tmp /#ftp:guest@remote-host.com/pub/dir
Subscribe to:
Posts (Atom)