Networks/Servers
[Linux/Shell/Bash] Count pattern matches/instances in string
by Chuck Kozler on Jan.20, 2010, under Computers, Linux/Unix, Networks/Servers, Programming, Web Dev
This handy little Bash scripting function will count the number of instances it finds in a string by the given delimeter. As of right now, its pretty basic…I didnt go too far into expanding it and working it (like going through and escaping all character escapes) and only escape for | and \.
function countPatterns { string_in="$1" delim="$2" if [ $delim == "/" ] || [ $delim == "|" ]; then delim="\\$delim"; fi retval=`echo $string_in | perl -ne 'while(/'${delim}'/g){++$count}; print "$count\n"'` return $retval }
Remember, this isnt really completed but you get the idea. The use is something like:
countPatterns "this|is|a|string|" "|" instances="$?" #would output 4 echo $instances
[Ubuntu/Linux] Set networking script
by Chuck Kozler on Dec.09, 2009, under Computers, Home, Linux/Unix, Networks/Servers, Ubuntu
Hi Everyone,
I have to manually change my network configuration settings a lot on my laptop when I’m at work and jumping between routers and other networks.
I got tired of manually entering all the commands so I wrote this. I expanded it a bit so other people can use it.
Its a quick and dirty script so it doesnt catch errors. Just checks if ifconfig and route commands were executed successfully.
Its pretty self explanitory. It takes in arguments of
device = the device you want to modify (eth0, eth1, etc…)
ip = the IP you want to set for yourself
gateway = The gateway you’re connecting to
dns = This is optional but you can set your own single DNS server address. If you do not enter one, the script will ask you if you want it to set you default ones with OpenDNS’ DNS server (http://opendns.org).
#!/bin/bash script_name=`basename $0` echo "" if [ "$1" == "--help" ] || [ "$1" == "-h" ]; then echo "Usage is: " echo "" echo " $script_name [device] [ip_addr] [gateway] [Optional: one_dns_server]" echo "" echo "Note: Only one DNS server can be set. Manually update resolv.conf if you want more" echo "Note: If no DNS is specified, the script will ask you if you want it to set you one." echo "" echo "" echo "Example: " echo "" echo " $script_name eth0 192.168.1.135 192.168.1.1 4.2.2.2" echo "" exit 0 fi device="$1" ip="$2" gw="$3" primary_dns="$4" user=`echo $USER` ans='y' if [ $user != "root" ]; then echo "You must be root to run this" exit 108 fi if [ -z $device ]; then echo "You must specify a device to modify" exit 5 fi if [ -z $ip ]; then echo "You must specify an IP address for your machine" exit 5 fi if [ -z $gw ]; then echo "You must specify a default gateway address" exit 5 fi if [ -z $primary_dns ]; then echo "* No DNS server set." echo "* You do not need to specify a DNS server but it is suggested" echo "* Would you like the script to write default DNS for you (using OpenDNS)? [y/n] (default: y)" read ans echo "Write DNS = " $ans fi # Do the networking configuration # ifconfig eth0 $ip if [ $? -eq 0 ]; then success=1 fi route add default gw $gw ret="$?" if [ $ret -eq 0 ] || [ $ret -eq 7 ]; then success=2 fi if [ ! -z $primary_dns ]; then su -c "echo nameserver $primary_dns > /etc/resolv.conf" else if [ $ans == 'y' ] || [ $ans == 'Y' ]; then su -c "echo nameserver 208.67.222.222 > /etc/resolv.conf" su -c "echo nameserver 208.67.222.220 >> /etc/resolv.conf" fi fi device_ip=`ifconfig eth0 | grep -i $ip | cut -d ':' -f2 | cut -d ' ' -f1` route_gw=`route -n | grep -i 192.168.155.1 | awk '{ print $2 }'` route_flag=`route -n | grep -i 192.168.155.1 | awk '{ print $4 }'` if [ $success -eq 2 ]; then echo "" echo "* Success!" echo "" echo "* IP: $device_ip" echo "* Gateway: $route_gw ( $route_flag )" echo "" fi exit
[Gentoo] VirtualBox/VMWare Networking pcnet32
by Chuck Kozler on Oct.07, 2009, under Computers, Home, Linux/Unix, Networks/Servers
So, personally, I have never really had to install Gentoo in a Virtualized environment so normally my networking and everything has worked right out of the box from the basic kernel configurations selected when you run “make menuconfig”.
Unbeknown to me, that would not at all be the case when you virtualize Gentoo with VMWare or VirtualBox. As some of you may know, VMware and VirtualBox both use a pcnet32 driver/module to enable networking. Now, after a ton of googling and back and forth I finally figured out what you have to do.
When you execute “make menuconfig”
make menuconfigThen, when you’re prompted with the options, step through the menu as such
Device Drivers
|----------> Network Device Support
|---------------> [ * ] Ethernet (10 or 100mbit)
|--------->[ * ] AMD PCNet32 PCI Support
Make sure you press “Y” to include it rather than modularize it (unless you have an initrd.img setup and want/need to modularize it) so this way you dont have to worry about editing
/etc/modules.autoload.d/
or having to do anything else for that matter to get networking working with the virtualized network/ethernet adapters
Then
# make -j2 # cp /usr/src/linux/arch/x86/boot/bzImage /boot/kernel # reboot
[PHP]Deny by Host/Hostname
by Chuck Kozler on Mar.16, 2009, under Computers, Networks/Servers, Programming
Well, it seems my Wordpress blog here is getting slammed with Russian spambots lately. My hoster has not gotten back to me yet if they can block a host for me (Henrik!
), so, I decided to write this up.
The comments in the code explain it pretty well
<?php //Get remote host $_get_addr = $_SERVER['REMOTE_ADDR']; //Get the host from the address $_get_host = gethostbyaddr( $_get_addr ); //Ban list array. The text before => is a key name and //after the => is the host you want to ban. This allows //for aliasing your problematic hostnames $_config_hostbanlist = array( russia1 => "nn-88-237.nm-s.ru" ); /* Ban by full hostname. The list is populated by the hostbanlist array. Simply it just compares their host with an entry in the hostbanlist array */ function isHostBanned( $host, $_ban_list ) { //Iterate each entry in the banned list array foreach( $_ban_list as $entry ) { //If we get a match, return true (the host is banned) if( strtolower( $host ) == strtolower( $entry ) ) return true; } return false; } /* This will ban by IP. This list is populated dynamically by the hostbanlist variable. It then resolves IP's for each hostname in the list and then checks it. */ function isIPBanned( $_remote_addr, $_ban_list ) { foreach( $_ban_list as $entry ) { $entry_ip = gethostbyname( strtolower( $entry ) ); if( $_remote_addr == $entry_ip ) return true; } return false; } function doAlert( $alert ) { echo "<script language='JavaScript'>window.alert( \"$alert\" )</script>"; } /* We will be checking for true/false of isIPBanned first since the IP banned list is populated by the $_config_hostbanlist array and isIPBanned resolves the hostname to an IP. Then, if that fails, it also then checks the hostname and compares to the list again via hostname. Flow: isIPBanned? -> loop hostbanlist -> resolve IP for given loop iteration -> check IP resolved to REMOTE_ADDR -> Match? => Banned -> No Match? => Not in that list, assume they are either not in the list or their IP could not be resolved. Go to isHostBanned function to ensure they are not in the list isHostBanned -> loop hostbanlist -> check if their host is equal to an entry in the array -> Match? => Banned -> No match? => Not banned */ if( isIPBanned( $_get_addr, $_config_hostbanlist ) || isHostBanned( $_get_host, $_config_hostbanlist ) ) { doAlert( "You are banned" ); echo "<br><br><br><p align='center'>Your host [$_get_host] and/or IP [$_get_addr] has been banned. <br> Please contact <a href='mailto:webmaster@ckozler.net'>webmaster@ckozler.net</a> if you feel you have reached this page in error</p>" . "<br>"; exit; } ?>
When you use this, all you need to do is copy and paste this into a file and then in each file you want to ban the host from, you do:
require('hostblock.php');
Of course, the example above assumes you name the file hostblock.php
Sphere: Related Content[Linux/Bash] Update DNS Script
by Chuck Kozler on Mar.04, 2009, under Computers, Linux/Unix, Networks/Servers, Ubuntu
I am currently doing a project that involves me creating my own DNS servers in virtual environments. To utilize those DNS servers I will need to, of course, edit the host computer’s /etc/resolv.conf to point to my new DNS servers IP addresses. For some odd reason I could not set a static IP (my Universities network is complete crap) and I required the dhclient/dhcp3-client package to be able to utilize my network. Since dhcp3-client likes to overwrite the /etc/resolv.conf file (regardless of what is in the /etc/dhcp3/dhcp.conf file) I had to hand edit the /etc/resolv.conf file ALL the time. Well, I got sick of that and this is what came of it.
I hope you enjoy
#!/bin/bash user=`whoami` if [ $user != "root" ]; then echo "You must be root to run this" exit 0 fi basename=`basename $0` location=`dirname "$0"` ans='n' if [ $location != "/usr/local/bin" ]; then echo "" echo "If this were in /usr/local/bin then it would be easier to use (or set PATH variables)" echo "" echo "Would you like to copy it there? [Y/n]" read ans fi function usage { echo " " echo "Usage is: " echo " " echo " makedns <domain> <domain_to_search> <ip_of_dns_1>" echo " " echo " " echo "Example:" echo " " echo " makedns test.local test.local 192.168.1.101" echo " " } if [ -z "$1" ] || [ -z "$2" ] || [ -z "$3" ] ; then usage if [ -z "$1" ];then echo "You left field 1 blank. Please fill in a DNS domain name" elif [ -z "$2" ]; then echo "You left field 2 blank. Please fill in a DNS search to search" elif [ -z "$3" ]; then echo "You left field 3 blank. Please fill in a primary DNS IP" fi echo " " exit 0 fi # # TODO: Possibly do this in a loop and store them in an array? # lines=`wc -l /etc/resolv.conf | cut -d " " -f1` dnsdomain=`sed -n "1 p" /etc/resolv.conf | cut -d " " -f2` dnssearch=`sed -n "2 p" /etc/resolv.conf | cut -d " " -f2` dnsip1=`sed -n "3 p" /etc/resolv.conf | cut -d " " -f2` dnsip2=`sed -n "4 p" /etc/resolv.conf | cut -d " " -f2` _in_dnsdomain="$1" _in_dnssearch="$2" _in_dnsip1="$3" _in_dnsip2="$4" # # # #echo text_to_pipe | sed s/<old>/<new> complete=0 if [ $lines -gt 0 ]; then echo "domain $_in_dnsdomain" > /etc/resolv.conf if [ $? -eq 0 ]; then complete=1 fi echo "search $_in_dnssearch" >> /etc/resolv.conf if [ $? -eq 0 ]; then complete=2 fi echo "nameserver $_in_dnsip1" >> /etc/resolv.conf if [ $? -eq 0 ]; then complete=3 fi if [ $_in_dnsip2 != "" ]; then echo "nameserver $_in_dnsip2" >> /etc/resolv.conf fi if [ $complete -ge 3 ]; then echo "DNS update/reconfiguration complete and successful." echo " " echo "New Configuration: " echo " " cat /etc/resolv.conf echo " " echo "Exiting." fi fi #If they want to move it or not to /usr/local/bin if [ $ans == "y" ] || [ $ans == "Y" ]; then echo "Moving..." cp $basename /usr/local/bin/ chmod 755 /usr/local/bin/$basename fi
Intro to UNIX Handbook – by Charles Kozler
by Chuck Kozler on Jan.28, 2009, under Computers, Linux/Unix, Networks/Servers
I wrote this 50 page UNIX handbook from scratch as apart of a project in my sophomore year of college. There is a title page to it so you can browse the summary of it. I have pasted that below. You can find the entire 50 page handbook attached to this post.
I would like some feedback if you all could be so kind. Positive and/or negative feedback is welcome. Negative feedback had better be constructive criticism and not posted in a manner of a flame or bash on my work. I was a sophomore in college when I did this. This in no way equivocates to my current knowledge of all of these topics. As you could imagine, my knowledge in these topics has only increased. Any errors I may have had then in this I have not corrected. Corrections would be nice and much appreciated.
Index Page:
UNIX
Administration Handbook
By Charles Kozler
Below you will find a chapter list and a list of what is covered in each chapter
Chapter 1 – Introduction to UNIX server and basic server administration principles
- What UNIX is
- Using UNIX
- Startup and shut down
- Root, logging off, sudo, and su
- Single-User Mode
Chapter 2 – Installing UNIX
- Minimum requirements (for operational server)
- Using FDISK and installing UNIX
- Configuring UNIX
- Ports and using them
Chapter 3 – Apache
- What Apache and it does
- Guide to installing Apache
- Configuring Apache
- Testing Apache
- Setting up Virtual Hosts
- Common problems installing, configuring, and running Apache
Chapter 4 – DNS
- What DNS is
- What DNS does
- Guide to installing BIND
- Configuring DNS
- Testing DNS configuration and BIND installation
- Common problems installing, configuring, and running DNS
Chapter 5 – Postfix
- What Postfix is and what it does
- Guide to installing and configuring Postfix
- Testing Postfix
- Common problems installing, configuring, and running Postfix
Chapter 6 – Useful commands and files
- Useful commands and options
- Files to be familiar with
Download
intro_to_unix_handbook-charles_kozler
[Bash/Linux] Web server start/stop scripts
by Chuck Kozler on Jan.15, 2009, under Linux/Unix, Networks/Servers
The scripts are pretty self explanatory.
The start script checks if apache is running. Then, it checks if MySQL is running. If they are both running then the script exits. Apache is checked first, then MySQL. If Apache is running but not MySQL then the script continues and starts up MySQL.
The stop script attempts to gracefully stop Apache. If it fails then it tries a normal stop. If that (for whatever reason) fails too, then it kills it forcefully by running “killall”. This command may not be globally available so you may have to edit it based on your servers configuration. I could probably also change the script to get the PID of Apache and then force kill it that way. That will probably come later as for now, this works for me and you can get the general idea.
startweb:
#!/bin/bash user=`whoami` if [ $user != "root" ]; then echo "You must be root to run this." exit 0 fi apache=`ps -A | grep -ic apache2` mysql=`ps -A | grep -ic mysql` aprunning=0 #Check apache2 first if [ $apache -gt 0 ]; then echo "*** WARNING *** Apache is already running. Will not start Apache." echo "*** NOTIFICATION *** Trying MySQL...." #apache2 is running, attempt mysql anyway if [ $mysql -gt 0 ]; then echo "*** WARNING *** MySQL is already running. Will not start MySQL." echo "*** EXITING ***" exit 0 else echo "*** NOTIFICATION *** Apache is running but MySQL is not. Starting MySQL..." fi aprunning=1 fi if [ $aprunning -ne 1 ]; then apache2ctl start if [ $? -eq 0 ]; then echo " * Apache2 started successfully" elif [ $? -ne 0 ]; then echo " * ERROR: Apache2 could not start. Please check logs." fi fi /etc/init.d/mysql start if [ $? -gt 0 ]; then echo "ERROR: MySQL could not start. Please check logs." exit 0 fi exit 0
stopweb:
#!/bin/bash user=`whoami` if [ $user != "root" ]; then echo "You must be root to run this." exit 0 fi #stop apache apache2ctl graceful-stop if [ $? -gt 0 ]; then echo "Graceful stop seemed to fail." echo "Trying normal stop." apache2ctl stop if [ $? -ne 0 ]; then echo "Normal stop didnt work." echo "Force killing." #CHANGE HERE IF YOUR SERVER DOES NOT SUPPORT 'killall' COMMAND killall apache2 if [ $? -eq 0 ]; then echo " * Apache2 force killed " else echo " * ERROR: Apache2 not stopped or killed" fi else echo " * Apache2 stopped (not gracefully though)" fi else echo " * Apache2 gracefully stopped" fi #stop mysql /etc/init.d/mysql stop if [ $? -ne 0 ]; then echo "Stop didnt seem to work..." echo "Killing MySQL" killall mysql fi exit
[PHP] Kill MySQL Connections
by Chuck Kozler on Jan.08, 2009, under Computers, Networks/Servers, Web Dev
Here is some code I made in PHP to kill all concurrent MySQL connections. I made this one night because a MySQL command I needed to kill commands could not handle wild cards. This iterates through all connections/processes and kills them one by one. If you have some knowledge of PHP then this is self-explanatory:
<?php $con = mysql_connect( "localhost", "root", "<password>" ) or die( "can not connect" ); if( $con ) echo "Connected<br>"; $result = mysql_query( "SHOW FULL PROCESSLIST", $con ); while( $row = mysql_fetch_array( $result, $con ) ) { $process_id = $row["id"]; if( $row["Info"] == "NULL" ) { $sql = "KILL $process_id"; $res = mysql_query( "$sql", $con ); if( $res ) { echo "Mysql Process ID $process_id has been killed<br>"; } } else echo "Row not found?<br>"; } ?>