Search This Blog

Monday, November 02, 2020

Python substitute for netcat

When working across multiple platforms and flavours of IX, a system administrator  might not always have tools like telnet or netcat available to test network connections. One tool that is on all flavours of IX and is universal is python.


A quick python script for testing network connection.

I used this python script to send a message via UDP to a logstash server.


import socket
message = 'Flah blah bleh'
byteToSend = str.encode(message)
serverAddressPort = ('IP Address', port)
bufferSize = 1024
soc = socket.socket(family=socket.AF_INET, type=socket.SOCK_DGRAM)
soc.sendto(byteToSend, serverAddressPort)



Tuesday, August 11, 2020

Password validator

 Validating password with python.


Regex

import re

passRX = re.compile(r'[A-Za-z0-9@#$%^&+=]{8,}')

pp = passRX.search('dy_5mupV')

pp == None

True


pp = passRX.search('DEVGukvk')

pp == None

False


Reference:

Thursday, June 18, 2020

awk multiple field separators.

Using multiple field separators with awk and perl to convert "," to a newline.

Example:

echo "100(txt),528(smbw),529(smbt),530(smbn),10115(smbs)"|\
 perl -pe 's/,/\n/g'
100(txt)
528(smbw)
529(smbt)
530(smbn)
10115(smbs)

Using awk to just print the text between ().
echo "100(txt),528(smbw),529(smbt),530(smbn),10115(smbs)"|\
 perl -pe 's/,/\n/g'|\
 awk -F"[()]" '/smb/{print $2}'
smbw
smbt
smbn
smbs

Using awk to just print the text between [].

awk -F'[][]' and awk -F'[[]]' will not work.

Wednesday, October 23, 2019

Update remote /etc/hosts file using sudo

Sudo example

printf 'IP\tHost\n' | ssh -q host "sudo su - root -c 'cat - >> /etc/hosts'"

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];}'



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 -"


 

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

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.

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]])}

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"




Thursday, July 11, 2013

Scripting SMTP
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)