Показват се публикациите с етикет bash. Показване на всички публикации
Показват се публикациите с етикет bash. Показване на всички публикации

22.04.2019 г.

bash multiple rename file's

# bash multiple rename file's
for file in `find . -type f -name "*.txt"`;do mv $file ${file%.*}.csv;done

26.08.2016 г.

Ping mashine with nbtscan

#!/bin/bash
#WORK ON DEBIAN BASE SYSTEM!
#CHECK USER IS ROOT OR NOT
if [ $UID -ne 0 ];then
        echo "Must run with root privileg!!!"
        exit
fi

#GET NETWORK INTERFACE
network=$(ifconfig | sed '1!d' | cut -d" " -f1)

#CHECK NETWORK INTERFACE IS ADD TO PARAM IN SCRIPT
if [ "$#" -ne 1 ];then
echo "Usage - ./ping.sh $network"
exit
fi

#CHECK NBTSCANEXISTS OR NOT
test `which nbtscan` || \
echo -e "Error nbtscan not exists\nTry to install!!!" `apt-get install nbtscan` >> /dev/null

interface=$1
echo "The interface is $interface"

prefix=$(ifconfig $interface | grep "inet addr" | cut -d ":" -f2 | cut -d " " -f1 | cut -d"." -f1-3)
echo "------------------------------------------------------------"
for addr in $(seq 1 254);do
nbtscan $prefix.$addr | grep -E "server" | awk -F" " '{printf "%-15s %-20s %s\n", $1,$2,$NF}' &
done


23.03.2016 г.

Make static arp with bash

#!/bin/bash

start=$1
end=$2
ar=/usr/bin/arping
arp=/usr/sbin/arp
echo "Start is $start and end is $end"
echo
start_end=${start##*.}
end_end=${end##*.}

for i in $(seq $start_end $end_end);do   
    $arp -s `$ar -I eth0 ${start%.*}.$i -c 1 |\
    grep "Unicast" | awk -F" " '{print $4," ",$5}' |\
    sed -e 's/\[//' -e 's/\]//'`
done

20.02.2016 г.

Remove empty space's from name of file

#!/bin/bash



DIRECTORY=/home/test


find $DIRECTORY -name "* *"|while read fname;do
new_name=`echo $fname | tr " " "_"`
if [ -e $new_name ];then
echo "Exists"
else
mv "$fname" $new_name
fi
done

31.07.2015 г.

Search for ip and resolv with hostname for enybody file

#!/bin/bash

#Path to file with ip-s
FILE="/home/ips.txt"
#Command to resolv ip- with hostname
DIG="dig +short -x "
#Search for ip in file SEARCH=`cat $FILE | egrep -Eo '((1?[0-9][0-9]?|2[0-4][0-9]|25[0-5])\.){3}(1?[0-9][0-9]?|2[0-4][0-9]|25[0-5])' | sort -t . -k 1,1n -k 2,2n -k3,3n -k4,4n`

#For clasuse
for i in $SEARCH;do
    echo -ne "$i\t-\t" "\e[1;33m `$DIG$i`\e[0m" "\n"
done

2.07.2015 г.

Find Picture in Folder and print only name of them

for i in `find /home/name/Image -type f -name "*.jpg"`;\
do n=`echo $i | awk -F"/" '{print $NF}'`\
| echo ${n//.*}; done




for i in `find /home/name/Image -type f -name "*.jpg"` # Found some picture's
n=`echo $i | awk -F"/" '{print $NF}' #From the top row print only last name of the result
echo ${n//.*} #Remove .avi from file

Change file after time use root access (use for configure network)

su -c bash -c "sleep 1m; mv file_orig.txt file_orig-backup.txt"

su -c #Run root only for command
basj -c #Run bash
"sleep 1m" #Sleep 1 minute
The last is the command you want.

27.04.2015 г.

Find music and play it with shell find use


  • find /home/borko/Mp3 -name "*pls" -type f -print | while read file;do cvlc $file;done 

  • find /home/borko/Mp3 -name "*pls" -type f -print | sed 's/^/cvlc /' | sh -x


14.04.2015 г.

bash alias to unrar some format

Create faile:
touch unrar.sh && chmod +x unrar.sh
And then paste this in terminal:


cat <<EOF >>unrar.sh
#!/bin/bash

if [ -z "$1" ]
then
  echo "Paramather #1 is zero lenght"
else
  echo "Paramether #1 is \"$1\""
case $1 in
*tar) tar -xvf $1
;;
*bz2) bunzip2 $1
;;
*rar) unrar e $1
;;
*zip) unzip $1
esac
fi
EOF


After this open ~./bashrc or this you use and write this:

alias unrar='sh /path/to/unrar.sh/

Same alias start with use sintax:

unrar somefile.tar

9.04.2015 г.

Search for avi file, and play them with vlc - Bash script

#!/bin/bash
#In variable filename save result for find 
filename="result.txt"

#use find command for search for file +1G search for file gt 1G
find /home/borko/ -maxdepth 6 -type f -name *.avi -size +1G > $filename &&

#If not result save data in file
if [ $? -ne 0 ]
then
        date >> $filename
 
fi

#Function read line by line and play with vlc
while read line
do
 name=$line
 echo "Played - $line"
 vlc "$name"
 kill -9 vlc
done < $filename



#If you not use file do this in same line for play mp3 file:
find /home/borko/ -maxdepth 6 -type f -name *.mp3 -size -exec vlc {} \;
#This is a little function
mp3(){
 cd "/home/borko/Музика/"
 o=$IFS
 IFS=$(echo -n "\n\b")
 find -type f -name "*.mp3" -print0 | while read -d $'\0' name
 do
   `vlc $name`
 done
 IFS=o
}

27.03.2015 г.

Check alive ip with bash file

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
#!/bin/bash

#####################################

#This script    for                 #

#alive ip and show name                                #

#uses module arping and work                         #

#for this time only by Debian bases                   #

#####################################

#GLOBAL VARIABLE

LOGFILE=logping.log                #LogFile

ARPING="apt-get install arping"  #Install arping

HPING="apt-get install hping3"   #Install hping3

ETH=$1                                     #Device Name

IP=$(ifconfig $ETH | grep "inet addr" | cut -d":" -f2 | cut \

-d" " -f1 | cut -d"." -f1-3)    #Found Ip



#CREATE FUNCTION FOR READLINE

readlines()

{

while

read line; do

ping=`ping -c 1 -b $line` &&

echo -e "\n**********************************************************************"

echo "IP $line UP" `arp -i $ETH $line | grep ether | awk '{print "Name " $1 " With macaddr "$3}'`

echo -e "\n"

echo `hping3 -c 1 -i $ETH $line --scan 0-6000 -S |awk '{print $1}' 2>&1`



done < $LOGFILE

exit 1

}



if [ -s "$LOGFILE" ]

then

rm $LOGFILE

fi



#DONE WITH FUNCTION

#Check if corect start script

#t.e. enter device name eth0 for example



if [ $# -ne 1 ]; then

echo "Usage - ./ping_and_scan.sh (Enter device like eth0)"

exit

fi



#somtimes finction not work with

#start with user privileg

#next row check if script start

#with user access



if [ "$(id -u)" != "0" ]; then

   echo "This script must be run as root" 1>&2

   exit 1

fi



#For reason script work with

#arping, check if pacckages instaled

#if not it is installed with root access



if [ "$(dpkg -s arping | grep 'Status: install ok' | cut -d" " -f3)" != "ok" ];then

echo "packages arping is not installed - Packages Install "

echo `$ARPING` &&

exit 1

fi



if [ "$(dpkg -s hping3 | grep 'Status: install ok' | cut -d" " -f3)" != "ok" ];then

echo "packages hping3 is not isntalled - Packages Install "

echo `HPING` &&

exit 1

fi

#Variable for Device

#Check for ip address



ping_scanning(){

for addr in $(seq 1 255); do

arp_alive=`arping -c 1 $IP.$addr | grep "bytes from" |\

cut -d " " -f 5 | cut -d "(" -f 2 |\

cut -d ")" -f 1 1>&2 >> $LOGFILE &`

done

}



ping_scanning



echo "Arp is working Waiting "

while true; do

if ! pgrep arping > /dev/null; then

readlines

else

echo -n "."

fi

sleep .3

done