Skip to content

Instantly share code, notes, and snippets.

@alexandrinos
Last active December 29, 2015 14:57
Show Gist options
  • Save alexandrinos/250cd907644caa43c3ce to your computer and use it in GitHub Desktop.
Save alexandrinos/250cd907644caa43c3ce to your computer and use it in GitHub Desktop.
Netcat
#from
# https://www.youtube.com/watch?v=LQSMJtckzYI
# simple chat coomunication between 2 machines with netcat
#This works with linux, Win
# to install apt-get install netcat-traditional #buntu
#IP 192.168.33.10
nc 192.168.33.10 3137
#IP 192.168.33.11 ; so this machine is listening on the ß0rt 3137 for what 192.168.33.10 is sending, and vicevers
nc -l -p 3137
subshell:
evaluate list of commands in a subshell;environment is distinct from current shell and variable also
form: (list)
ex:
unset z; (z=hello; echo $z;); echo $z #the last variable $z is outside the routine, so is empty,ouside the scope of the subshell
group command:
evaluate list of commands in the current shell,sharing the current shells environment
form: { list; } #spaces is important before and after
ex:
unset z; { z=hello; echo $z; }; echo $z #the last variable $z is part of the routine,
#so is not empty,inside the scope of the group command
usefull grouping:
echo b; echo a | sort; #bad, is not sorted
(echo b; echo a) | sort; #sorted
#show the grouped matriska processes of shells
echo "$(echo "$(echo "$( ps wwf -s $$)")")"
echo "$(echo "$(echo "$(echo "$(ps f $$)")")")"
if list2;then list2;fi
if list1;then list2;else list3;fi #list2 evaluates only if list1 returns status 0
if list1;then list2;elif list3;then list4;else list5; fi
ex:
if [ -f license.txt ];then echo 'license';elif [ -f readme.txt ];then echo 'readme';else echo 'none of these'; fi
case conditionals
case word in
pattern1)
list1;;
pattern2 | pattern3)
list3;;
esac
ex:
case one in
o)
echo 'o' #is not matched because is not fully exhausted or complete
;;
o*)
echo 'o*' #this is matched
;;
*)
echo 'nope'
;;
esac
A.----------Parameters
$# Stores the number of command-line arguments that
were passed to the shell program.
$? Stores the exit value of the last command that was
executed.
$0 Stores the the name of the shell program or the shell script.is not a positional parameter
If started with -c (CMDLINE) option as argument,$0 will be the first argument after CMDLINE
$* Stores all the arguments that were entered on the
command line ($1 $2 ...). as a string
"$@" Stores all the arguments that were entered
on the command line, individually quoted ("$1" "$2" ...)
#####
$? Exit status of most recent pipeline
$- current option flags
$$ PID of the invoked shell #decimal
$! PID of most recent background command executed from current shell #decimal
$- option flags set by shell itself, usually himB
$! The process ID (PID) of the most recently executed background pipeline (like started with command &)
$_ catck all parameters
ex short:
$0 : echo "$0" # -bash or name of the script
ex command line
./command -yes -no /home/username
values of different dollar signs
$# = 3 #number
$* = -yes -no /home/username #string
$@ = array: {"-yes", "-no", "/home/username"} #array
$0 = ./command, $1 = -yes etc.
#Looping and Conditionals in BASH
#
# many infos from
# https://www.youtube.com/watch?v=uqHjc7hlqd0
#!/bin/bash
#Ex0: iterating words with select
#like a menu so we can choose what to select from input keyboard
#so, the $choice is the word that corresponds with our number selection($REPLY index)
#if the selection is out of range, $choice is not set
select var1 in one two "three four"
do
echo "number $REPLY : $choice" #$REPLY is the index variable of var1 and the value is the selected word
done
#Ex 1; iterating
echo 'Ex1: '
for((i=1;i<=3;i++)); #loops until expr (i<=3) returns a non-zero status(fails)
do #each for has a do ended with done !
echo $i; # ";" is optional
done
#Ex 2; just taking a line as a variable
echo 'Ex2: '
for name in Floki John Levi;
do
echo ${name};
done
#Ex 3; we take a data and loop thtough it
echo 'Ex3: '
for l in `cat data1.txt`; #not working with '' or "" , gives just data1.txt
do
echo ${l} | cut -d "," -f 2 #cut is used to filter the data -d = delimiter -f = what columns we want
done
#Ex 4; looping with < -lt; == -eq ; > -gt ; if elif then else fi
echo 'Ex4: '
cutoff=2
for((i=1;i<=3;i++));
do
if [ $i -lt $cutoff ];
then
echo "$i is less than $cutoff";
elif [ $i -eq $cutoff ]; # = is for string, -eq for numeric comparision
then
echo "$i is same as $cutoff";
else
echo "$i is not less than $cutoff"
fi
done
#Ex 5; just taking a line as a variable
echo 'Ex5: '
for z in Alfa Beta Gamma;
do
if [ "${z}" = 'Alfa' ]; #note single equal = for comparision of strings;for int use -eq
then exit0 #continue #or use 'continue' -> for iterating the rest of the code
else
echo $z
fi
done
#interesting reading a list1 and do meanwhile another list2 until list returns non-zero !0 status (fails)
while list1
do
list2
done
#example:
while read var1;do echo $var1;done #reapeats until a non zero exits status
#until is opposite of while; if a zero status come(succed) fails immediately
#executes list1 ; if fails continue to execute list2 and repeat until list1 returns status 0 (succeds)
until list1
do
list2
done
#example:
until read var1;do echo $var1;done #reapeats until a zero exits status
pattern matching
used with [[
* -any
? -single
[character class] -any one of the characters between []
[^...] complement
[x-z] range from x to z
[[:class]] -matches according to POSIX classes:
alnum alpha ascii blank cntrl digit graph lower print punct space
#linux to linux, linux to windows etc
#IP 192.168.33.10 Linux
sudo nc -lp 31337 -c /bin/bash
#IP 192.168.33.1 Windows with netcat installed (http://www.securityfocus.com/tools/139 )
#and from Powershell or Cmd is connecting to Linux and now you are ready to execute cmd to Linux from WIndows
#like ls, useradd, cat, etc
nc.exe 192.168.33.10 31337
#Tests
[expression] or test expression
We can evaluate with the test builtin
[[expression]] new way of declaring expressions with bash 3
right side of a comparision is: 1. pattern if NOT quotted
[[string1==string2]] #so string2 is a pattern
2. a string, if quottted
[[string1=="string2"]] #so string2 is a string
[[-n string]] is non-empty string #if [[ -n string ]];then echo "hello"; fi
[[-z string]] is empty string #if [[ -z string ]];then echo "hello"; fi
[[string1 == string2]] 1 and 2 are same #if [[ 'alfa' == 'alfa' ]];then echo "hello"; fi
[[string1 != string2]] 1 and 2 are not same
[[string1 =~ *]] string1 matches reg express
[[ -e file.txxt ]] if file exists #if [[ -e ilsas.txt ]];then echo "hello"; fi
[[ -f file.txxt ]] if is a file #if [[ -f ilsas.txt ]];then echo "hello"; fi
[[ -d file ]] if is a directory #if [[ -d ilsas ]];then echo "hello"; fi
[[ -t 0 ]] This test option may be used to check whether the stdin [ -t 0 ] or stdout [ -t 1 ] in a given script is a terminal
# 0 is for stin input, but we can use 1 ,stdou
# so if a person is writing something from keyboard the test pass , if is from a script file the input fails
ex1:
#the data is coming from us directly( o is for input),so is a terminal
if [ -t 0 ];then echo 'terminal';else echo 'not terminal';fi
#the data is coming from a text file ( o is for input),so is not a terminal
if [ -t 0 ];then echo 'terminal';else echo 'not terminal';fi
ex2:
while read f ; do echo $f;if [ -t 0 ];then echo 'not terminal';fi;done < data.txt #not terminal
while read f ; do echo $f;if [ -t 0 ];then echo 'terminal';fi;done #from us, so terminal
# vim copy all text
gg"*yG #gg beginn, "*y start a yank command, G end of file
#read line from text
while read myline
do
echo '.......'
echo "$myline"
done < data.txt
#read from input
while read myline
do
echo '.......'
echo "$myline"
done
# read the values of two variables from keyboard until Ctrl^c
while read var1 var2
do echo $var2 $var1
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment