Friday, October 29, 2010

crond errors

While going through the cron log with my scanlogs script, I noticed that the only errors present are those that say 'unable to exectute /usr/sbin/sendmail/'. Sendmail is a script of some sort that sends a message (to the root user, I believe) with cronjob info. However, /usr/bin/sendmail doesn't exist. You can also sepcify this messaging script in the config file, /etc/conf.d/crond, with the -M switch. So, I made a blank script, cronmailer, and had cron call it instead of sendmail.

Thursday, October 28, 2010

[update]seressid

I think I've posted this script before. Simple script to search for a regexp within a near by broadcasting ESSID. Reports found networks or that nothing matched 'regexp'.

#!/bin/bash

chkroot && [[ $? = 0 ]] || exit 1

count=0
while [[ $count < 10 ]]; do
  iwlist wlan0 scan | grep $1
  [[ $? = 0 ]] && exit 0
    let count=$count+1
  sleep 2
done

echo "No ESSID's matching term '$1' found."

exit 1

Tuesday, October 26, 2010

[update]wircon

Fixes:
If the find_network function failed, the script would still try to connect to a network, which would cause an error because there was no network set to connect to.
An instance where main was called from a function which was called by main. Now it just returns to main rather than calling it again.
An edit to the restart function in the init script was also necessary. The delay between stopping and starting the script was changed from 1 second to 2.

Still to fix:
If the script is stopped before it enters the 'main' function, the signal wont be caught and the do_unset_network function wont be run.

#!/bin/sh

dhcpcdPID="/var/run/dhcpcd.pid"
networks=`cat /home/jason/devel/scripts/wircon/pref_networks.lst`
statfile="/home/jason/devel/scripts/wircon/status"
silent=1

# make sure the script is being run as root
# chkroot && [[ $? = 0 ]] || exit 1

echo "starting" > $statfile

##################################################################
# scan for essids located in pref_networks.lst
find_network() {
  for i in $networks; do
    iwlist wlan0 scan | grep $i &> /dev/null
    if [[ "$?" = "0" ]]; then
      local essid=$i
      break
    fi
  done

  case $essid in
    "SouthPacific")
            network="sp"
            ;;
    "SouthPacific_Extender")
            network="spx"
            ;;
    "Belkin_G_Wireless_C973D1")
            network="seans"
            ;;
        "NETGEAR-Go")
            network="seans2"s
            ;;
        "MadisonOaks")
            network="daniels"
            ;;
        *)
            network=
  esac
}

##################################################################
# if dhcpcd is running, stop it
do_dhcpcd_check() {
  if [[ -f $dhcpcdPID ]]; then
    if [[ $silent = "1" ]]; then
      dhcpcd -x
    else
      dhcpcd -x -q
    fi
  fi

}

##################################################################
# load wireless firmware
do_load_firmware() {
  [[ $silent = "1" ]] && echo "loading wireless firmware"
  ifconfig wlan0 up
}

##################################################################
# set essid and, if applicable, network key
do_set_network() {
  [[ $silent = "1" ]] && echo "setting network parameters"
  iwconfig wlan0 essid $ESSID
  if [[ $KEY != "" ]]; then
      iwconfig wlan0 key $KEY
  fi
}

##################################################################
# unset the network for graceful exit
do_unset_network() {
  do_dhcpcd_check
  ifconfig wlan0 down
}

##################################################################
# try for dhcp lease
do_dhcpcd() {
  if [[ $silent = "1" ]]; then
    dhcpcd
  else
    dhcpcd -q
  fi
}

##################################################################
# something that helps find a carrier
do_channel_auto() {
  [[ $silent = "1" ]] && echo "connecting to carrier"
  iwconfig wlan0 channel auto
}

##################################################################
# make sure it worked
check_connection() {
  ping -c 1 www.google.com &> /dev/null

  if [[ $? = "0" ]]; then
    return 0
  else
    return 1
  fi
}

##################################################################
# set the desired network
set_network_params() {
  case $network in
    "sp")
            ESSID="SouthPacific"
            KEY="6bcebdfbea3caf9f3cd36b31b8"
            ;;
    "spx")
            ESSID="SouthPacific_Extender"
            KEY="6bcebdfbea3caf9f3cd36b31b8"
            ;;
    "seans")
            ESSID="Belkin_G_Wireless_C973D1"
            KEY=
            ;;
        "seans2")
            ESSID="NETGEAR-Go"
            KEY=
            ;;
        "daniels")
            ESSID="MadisonOaks"
            KEY=
            ;;
    *)
            return 1
  esac

    return 0
}

##################################################################
# make sure we're still connected to the network
connection_monitor() {
  while [[ 1 = 1 ]]; do
    sleep 10
    check_connection
    if [[ $? = 1 ]]; then
      echo "disconnected" > $statfile
      return
    fi
    echo "connected" > $statfile
  done
}

##################################################################
# call all functions in proper order to establish connection
main() {
  trap 'echo "down" > $statfile; do_unset_network; exit;' INT
  trap 'echo "down" > $statfile; do_unset_network; exit;' TERM

    while [[ 1 = 1 ]]; do
        set_network_params
        [[ $? = 0 ]] && break
        echo "searching" > $statfile
        find_network
    done

  while [[ 1 = 1 ]]; do
    do_dhcpcd_check
    do_set_network
    sleep 2
    do_dhcpcd
    do_channel_auto
    check_connection
    if [[ $? = 0 ]]; then
      [[ $silent = "1" ]] && echo "connection established"
      connection_monitor
        else
            echo "disconnected" > $statfile
    fi
  done
}


##################################################################
# check scripts usage
if [[ $# > 0 ]]; then
  if [[ $1 = "-s" ]]; then
    silent=0
    shift
  fi
    do_load_firmware
    sleep 2
  if [[ ! -z $1 ]]; then
    network=$1
  else
    find_network
  fi
else
    do_load_firmware
    sleep 2
  find_network
fi

main

exit 2

Saturday, October 16, 2010

scanlogs-0.0.1-1

First draft of the log scanning script. Only searches through '.log' files, purposefully ommitting '.log.n' files created by logrotate. Special case search terms can be added in a case statement, as seen with the Xorg.0.log example. It then goes through each file, and if any instances of the search-term are found, it displays the name of the file in red and asks if it should be displayed, which is then piped through less (later it will only be piped through less if it's too long to fit on the screen). If no instances are found in the file, it prints the name of the file in blue and "clean" under it, then moves to the next file.

Note: right now the auth.log example doesn't work properly. I also plan to code in multiple search terms.

#!/bin/bash

chkroot && [[ $? = 0 ]] || exit 1

logpath='/var/log/'

alllogs=`ls $logpath | grep 'log$'`

clear

for i in $alllogs; do


    #special log cases
    case $i in
        "Xorg.0.log")
            search_term="(EE)"
        ;;
        "auth.log")
            search_term='authentication failure'
        ;;
        *)
            search_term="error"
    esac

    num_errors=`cat $logpath$i | grep -c "$search_term"`
    if [[ "$num_errors" -gt "0" ]]; then
        echo -e "\033[1;31m$i\033[0m"
        echo " :: found $num_errors instances of '$search_term', expand? [y/N]"
        read choice
        if [[ $choice = ['y','Y'] ]]; then
            cat $logpath$i | grep $search_term | less
        fi
    else
        echo -e "\033[1;34m$i\033[0m"
        echo " :: clean"
        echo
    fi
done

exit 0

Monday, October 11, 2010

[bug]Wircon

Turns out I messed something up in the coding. For some reason when re/started from the rc.d init script, some error messages are thrown. I'm guessing these messages are always there, it's just usually not visible because it's run as a startup script. I also might just try to write it in C, or maybe Python (the orriginal plan).

Thursday, October 7, 2010

[update]chkroot

Don't know why I didn't think to do this before...

#!/bin/bash

[[ $(id -u) = 0 ]] && exit 0
[[ $1 = "-q" ]] || echo 'error: this script must be run with root privileges'
exit 1

Wednesday, October 6, 2010

scanlogs

Working on a script to search through all of my current log files (those that haven't been pushed to a .log.n file by logrotate) for any anomolies. I'm mostly going to use this to search for error messages in log files, however, I might add some auth.log functionality to it. Suchas, any time a person tries to gain root access unsuccessfully.