Welcome to the Scripts page. This is a good place for me to share my scripts with you guys. Hope you all enjoy, any questions about any of these scripts leave a comment or email me and I'll get right back to you.
Click the below links to see the code.
#!/bin/bash
NUM=$1
URL=`(cd /home/path/to/workspace ; svn info |grep '^URL:' | sed -e 's/^URL: //')`
txtylw=$(tput setaf 3)
txtgrn=$(tput setaf 2)
txtblu=$(tput setaf 4)
txtbld=$(tput bold)
txtrst=$(tput sgr0)
if [ -n "$1" ]; then
echo "${txtbld}${txtylw} Showing you the last $1 commits to $URL ${txtrst}"
echo "________________________________________________________________________"
svn log --verbose --limit $1 $URL | while read line; do
if [[ $line == "M /"* ]]; then
echo "${txtbld}${txtblu} $line ${txtrst}"
else if [[ $line == "A /"* ]]; then
echo "${txtbld}${txtgrn} $line ${txtrst}"
else if [[ $line == "---"* ]]; then
echo "${txtbld} $line ${txtrst}"
else if [[ $line == "r"[0-9]* ]]; then
echo "${txtgrn}${txtbld} $line ${txtrst}"
else
echo "$line"
fi
fi
fi
fi
done
else
echo "Please provide a limit parameter! (e.g 10)"
fi
This script is designed to show a number of recent commits to the users current SVN branch in an easy readable format. I'm up for criticism on this as I'm not a huge fan of the amount of if statements...
#!/bin/sh
_HOSTNAME=`hostname`
_HOSTTYPE=`echo $HOSTTYPE`
_MACHINETYPE=`echo $MACHTYPE`
_OSTYPE=`echo $OSTYPE`
_VENDOR=`echo $VENDOR`
_KERNEL=`uname -r | awk -F- '{print $1}'`
_MEM=`cat /proc/meminfo | awk '/MemTotal/ {print $2 $3}'`
_CPU=`cat /proc/cpuinfo | grep 'cpu MHz' | awk '{print $4}'`
echo '=============================='
echo 'HOSTNAME ' $_HOSTNAME
echo 'HOSTTYPE ' $_HOSTTYPE
echo 'MACHINETYPE ' $_MACHINETYPE
echo 'OSTYPE ' $_OSTYPE
echo 'VENDOR ' $_VENDOR
echo 'KERNEL ' $_KERNEL
echo 'MEM INFO ' $_MEM
echo 'CPU INFO ' $_CPU
echo '=============================='
This script is designed to retrieve host information from the kernel to the memory. It assigns stander commands to variables and then echo's everything out in a nice readable form.
#!/bin/bash
while read line
do
# -c 1 means ping only once
# -w means time-out, so if nothing is received within 5 seconds then it's a fail
if ! ping -c 1 -w 5 $line &>/dev/null ;
then
echo "Failed to connect to $line"
else
echo "Connected to $line!"
username=user
passwd=password
port=23
cmd1=""
cmd2=""
cmd3=""
( echo open ${line} ${port}
sleep 2
echo ${username}
sleep 2
echo ${passwd}
sleep 2
echo ${cmd1}
sleep 2
echo ${cmd2}
sleep 3
echo exit ) | telnet
fi
done < ip.txt
This script is your basic telnet script.
in order to use this script you must create a text file called 'ip' and fill it with the ip(s) you wish to connect to
The script loops through the list of IP addresses in the file and ping's them. Once it has a ping it starts Telnet and issues commands between sleeps.
Just fill out the relevant information (username, password and required commands). The script loops through the ip.txt file and ping's the address, if the ping is successful it will go on to do the telnet, if not it will skip that IP and move on to the next.
#!/bin/bash
filename="aFile.txt"
username=user
passwd=password
while read line
do
if ! ping -c 1 -w 5 $line &>/dev/null ;
then
echo "Failed to connect to $line"
else
echo "Connected to $line!"
#-inv
# -i means turns off prompting during multiple file transfers
# -n means stops auto-login
# -v is our old friend verbose
ftp -inv $line<<!
user $username $passwd
bin
cd /a/directory/
#place a file in the directory
put $filename
#take a file from the directory
get aFile.mp3
close
bye
!
fi
done < ip.txt
This script is very similar to the above Telnet one. It uses the same while loop and ip.txt file. FTP is used for transferring files. It's not the only way but its a start. Make sure you run this from the directory which has the file in you want to send.
#!/bin/bash
echo "tv or radio?"
read option
echo "Please paste in you link for the content you wish to download:"
read url
get_iplayer --get --type $option --url=$url
For this script you will need to have get_iplayer installed (usually a "
apt-get install get_iplayer" will do the trick). It's a very basic script I know but feel free to jazz it up a bit, the PVR function is worth looking at. There is also some more help
here if you need it
#!/bin/bash
################################################
# Define colours
################################################
txtund=$(tput sgr 0 1) # Underline
txtbld=$(tput bold) # Bold
txtred=$(tput setaf 1) # Red
txtgrn=$(tput setaf 2) # Green
txtylw=$(tput setaf 3) # Yellow
txtblu=$(tput setaf 4) # Blue
txtpur=$(tput setaf 5) # Purple
txtcyn=$(tput setaf 6) # Cyan
txtwht=$(tput setaf 7) # White
txtrst=$(tput sgr0) # Text reset
echo "${txtylw}${txtbld}---------------------------------------------"
echo "-----------------PORT SCANNER----------------"
echo "---------------------------------------------${txtrst}"
echo "${txtgrn}LAN:"
echo "(e.g. 192.168.0.0)${txtrst}"
read LAN
echo "${txtgrn}Range:"
echo "(as in from 0-10)${txtrst}"
read RANGE
echo "${txtgrn}Port:"
echo "(select a port to search for or press '0' if you wish to scan all ports)${txtrst}"
read PORT
if [ $PORT -gt 0 ]; then
echo "${txtgrn}Scanning for open port $PORT${txtrst}"
sudo nmap -p $PORT --open -sV $LAN-$RANGE
else
echo "${txtgrn}Scanning for all open ports${txtrst}"
sudo nmap --open -sV $LAN-$RANGE
fi
This script is used to scan for open ports on a network. This should be used responsibly.
#!/bin/bash
# if no parameters then show help
# $@ is the parameter
# -z checks is parameter is empty and if so then show help.
if [[ -z "$@" ]]; then
#echo " ${0##*/} <*file/dir2> <*name>.tar.gz - archive file/folders"
# ${0##*/} show name of file
echo " ${0##*/} <*file/dir2> - archive and compress files and dirs"
exit
fi
# choose a file name for your new tar.gz
#read the line, -p means prompt for a responce
while true; do
read -p " Enter archive filename: " filename
# -n means none emtpy string (checking that you wrote something)
if [ -n "$filename" ]; then
break
fi
done
# Check if selection(s) exists
# for loop looping through the two files and checking there ok
for a in "$@"; do
if [ ! -f "$a" ]; then
echo " Selection \""$@"\" does not exist."
exit
fi
done
# Create archive
tar -c --xz -f "$filename".tar.gz "$@" && \
#-c means create file.
#--xz means extract files and zip them up
#-f means file
echo " Created "$filename".tar.gz"
This script is used to pack up files into a "tar.gz", this can easily be change to a "tar.xz" by replacing all instances of "tar.gz". This script just makes life a bit easier. The script uses parameters so the format would be:
./pack.sh afile.txt anotherFile.txt (and so on)
#!/bin/bash
tail -fn0 /var/log/auth.log | \
while read line ; do
echo "$line" | grep "Accepted"
if [ $? = 0 ]
then
xmessage "$line" | grep "Accepted"
fi
done
You just need to set this script to run on startup and there you go, if anyone tries to connected to your pc via ssh, telnet or ftp then you're going to know about it (I'm using xmessage here but you can set this up to send you an email or show a notification)
Hi,
ReplyDeletecat and grep is unless in your Host information script.
it can be done without cat and grep
awk '/cpu MHz/ {print $4}' /proc/cpuinfo