Sunday, June 25, 2017

meeting timer

meeting timer
mtg_timer

you can also see remain time on the tab.
TimeKeeper

current time is

end time is

remain time is
min sec



<!DOCTYPE html>
<html lang="ja">
    <head>
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">

        <!--multi device--!>
        <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
        <title>TimeKeeper</title>
    </head>
    <body>
    <!-- get current time --!>
    <p>
    current time is<label id="RealtimeClock"></label>
    </p>
    <!-- get end time --!>
    <p>
    end time is<input type="text"id="EndTime"  value="18:00" size="10">
    </p>
    <!-- get remain time and set buttons --!>
    <p>
    <form name="timer">
    remain time is</br>
    <input type="text" value="10" size="10">min
    <input type="text" value="0" size="10">sec<br>
    <input type="button" value="start" onclick="cntStart()">
    <input type="button" value="stop" onclick="cntStop()">
    <input type="button" value="reset" onclick="reSet()">
    </form>
    <!-- get aleart message --!>
    <font color="red"><label id="AlertMessage"></label></font>
    </p>
    <!-- get textbox --!>
    <p>
    <textarea name="freeans" rows="20" cols="30">Today's Agenda</textarea>
    </p>
        <!-- javascript --!>
        <script type="text/javascript">
        // display clock
        function showClock() {
           var nowTime = new Date();
           var nowHour = setfig( nowTime.getHours() );
           var nowMin = setfig( nowTime.getMinutes() );
           var nowSec = setfig( nowTime.getSeconds() );
           var msg = nowHour + ":" + nowMin + ":" + nowSec;
           document.getElementById("RealtimeClock").innerHTML = msg;
        }
        setInterval('showClock()',1000);
        // fix number of digits to two if its one
        function setfig(num) {
           var ret;
           if( num < 10 ) { ret = "0" + num; }
           else { ret = num; }
           return ret;
        }
        // set timer id
        var timer1;
        
        // get countdown timer for 1000msec
        function cntStart() {
          document.timer.elements[2].disabled=true;
          timer1=setInterval("countDown()",1000);
        }
        
        // stop timer
        function cntStop() {
          document.timer.elements[2].disabled=false;
          clearInterval(timer1);
        }
        
        // countdowm timer
        function countDown() {
          var min=document.timer.elements[0].value;
          var sec=document.timer.elements[1].value;
          
          if( (min=="") && (sec=="") ) {
            var msg="set time!";
            document.getElementById("AlertMessage").innerHTML = msg;
            reSet();
          } else {
            if (min=="") min=0;
            min=parseInt(min);
            
            if (sec=="") sec=0;
            sec=parseInt(sec);
            
            tmWrite(min*60+sec-1);
          }
        }
        
        // get remain time
        function tmWrite(int) {
          int=parseInt(int);
          
          if (int<=0)
          {
            reSet();
            var msg="it's time!";
            //alert(msg)
            document.getElementById("AlertMessage").innerHTML = msg;
            // aleart tab if remain time is zero
            document.title  = msg
          } else {
            // get remain min
            rem_min=Math.floor(int/60);
            // get remain sec
            rem_sec=int % 60;
            // get remain time
            document.timer.elements[0].value=rem_min;
            document.timer.elements[1].value=rem_sec;
            // display remain time
            document.title  = 'remain time is ' + rem_min + ':' + rem_sec;
          }
        }
        
        // reset form
        function reSet() {
          var msg="";
          document.getElementById("AlertMessage").innerHTML = msg;
          document.timer.elements[0].value="10";
          document.timer.elements[1].value="0";
          document.timer.elements[2].disabled=false;
          clearInterval(timer1);
        }
        </script>
    </body>
</html>

Sunday, May 28, 2017

play with python turtle

drawing by python is fun! You can start drawing with python script by importing "turtle module".

1. sample code

# coding: utf-8

import turtle
t = turtle.Pen()
while True:
    color_list = 'pink', 'green', 'purple'
    import random
    scale = random.randint(50, 100)
    print("scale %s" % scale)
    t.left(60)
    for c in color_list:
        i = 1
        print("color %s" % c)
        while i <= 360 / 60:
            print("count %s" % i)
            t.begin_fill()
            t.forward(scale)
            t.fillcolor(c)
            t.left(30)
            t.circle(scale/4, 180)
            t.right(180)
            t.circle(scale/4, 180)
            t.left(30)
            t.forward(scale)
            t.left(180)
            i = i + 1
            t.end_fill()
        scale = scale * 0.8
    t.up()
    location = random.randint(100, 300)
    print("location %s" % location)
    t.left(scale)
    t.forward(location)
    t.left(scale)
    t.forward(location)
    t.down()
turtle.done()

2. execution

% python turtle_work.py

check /etc/hosts by shell script

To practice shell script coding, I coded host check script which ping to hosts wrote in /etc/hosts file and generate check result logs.

1. script

#!/bin/bash

# make directory if unexist
DIR=./pinglog

if [ ! -d $DIR ]; then
mkdir $DIR
echo -e "directory created"
fi

# variables
LOGDIR=$DIR
NOW=$(date +"%Y%m%d-%H%M%S")

OKLOG=$LOGDIR/hostcheck_$NOW.log
NGLOG=$LOGDIR/hostcheck_error_$NOW.log

# check ping reply
while read line; do
hosts=$(echo $line | grep -v ^# | grep -v ^$ | grep -v localhost | cut -d " " -f 2)

# ping to hosts
if [ -n "$hosts" ]; then
ping -c 1 $hosts > /dev/null 2>&1

# it got reply "hostname : OK" else "hostname : NG"
# output OK-log and NG-log file

if [ $? -eq 0 ]; then
echo -e "$hosts : OK" >> $OKLOG
else
echo -e "$hosts : NG" >> $NGLOG
fi
fi
done < /etc/hosts

echo -e "done"
github link is below https://github.com/yoshi-island/shell-script_work/blob/master/check_hosts.sh

2. prepare

In this case, I made active hosts by vagrant on virtualbox. vagrantfile is wrote like this.
VAGRANTFILE_API_VERSION = "2"

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|

  config.vm.define :server do |server|
    server.vm.box = "centos7.0_x64"
    server.vm.hostname = "server"
    server.vm.network :private_network, ip: "192.168.111.223"
    #server.ssh.private_key_path = "~/.ssh/id_rsa"
  end

  config.vm.define :client do |client|
    client.vm.box = "centos7.0_x64"
    client.vm.hostname = "client"
    client.vm.network :private_network, ip: "192.168.111.224"
    #client.ssh.private_key_path = "~/.ssh/id_rsa"
  end

end
start machines on.
% vagrant up

/etc/hosts file is wrote like this.
% cat /etc/hosts | egrep -v "^#"
127.0.0.1    localhost
::1             localhost
192.168.111.223 server  # active on virtualbox
192.168.111.224 client  # active on virtualbox
111.111.111.111 dummy  # dummyhost

3. execute

execute
% bash check_hosts.sh
directory created
done
results
% cat pinglog/hostcheck_20170527-232436.log
server : OK
client : OK
% cat pinglog/hostcheck_error_20170527-232436.log
dummy : NG