Monday, September 11, 2017

How To Create Chart In Linux (unsing gnuplot)

Read this first

    https://stackoverflow.com/questions/327576/how-do-you-plot-bar-charts-in-gnuplot


You need to create 2 files:-


#===================
#   gnuplot.dat
#===================
0 label       100
1 label2      450
2 "bar label" 75
#===================
#   gnuplot.cmd
#===================
set boxwidth 0.5
set style fill solid
set term svg 
set output "mychart.svg"
plot "gnuplot.dat" using 1:3:xtic(2) with boxes


Now, run this command:-
    gnuplot gnuplot.cmd


This will create the output file
    mychart.svg




To view the chart
    display mychart.svg
Tutorial helps a lot 
    http://www.gnuplot.info/docs/tutorial.pdf

Tuesday, October 11, 2016

How To Run A Script As The Owner Of The Script


For the explanation below, just keep in mind that these assumption is true:-
- owner of file == yltan(uid=742)
- user that executes the script == icetools(uid=48102)

How to run a python script as the owner of the script.

1. Create any simple python script, eg a.py:-
#!/usr/bin/env python
import os

print "Before swap::" + str(os.getresuid())
os.system("whoami")

os.setreuid(os.geteuid(),os.getuid())

print "After Swap:" + str(os.getresuid())
os.system("whoami")
2. This is the magic wrapper script, setuid_swap.c. :-
/* This binary is intended to be a setuid script wrapper for dbRsync.pl.
  
   Example compilation on an hp machine:  gcc dbRsync.c -o dbRsync
   This compilation creates an executable called dbRsync. After compiling,
   chmod +s dbRsync (or whatever the executable is) to set the sticky bit or
   it won't work.
*/

int main(int ac, char **av) {
    execv( "/path/to/your/a.py", av );
}
3. Compile the setuid_wrap.c to wrapper_a :-
gcc setuid_swap.c -o wrapper_a
4. Change the setuid bit:-
chmod 4755 wrapper_a
5. Run wrapper_a as someone else other than the owner of the file:-
sudo su - icetools

./wrapper_a
6. ... and this is the output that you get (Running as icetools):-
Before swap::(48102, 742, 742)
icetools
After Swap:(742, 48102, 48102)
yltan






For a detail explanation of how setuid work,

https://drive.google.com/open?id=0B_HHt58thGk_MTVwQkhleWNmZE0



Alternatively, you could swap the users in the compiled code itself by doing this:-
/* This binary is intended to be a setuid script wrapper for dbRsync.pl.
  
   Example compilation on an hp machine:  gcc dbRsync.c -o dbRsync
   This compilation creates an executable called dbRsync. After compiling,
   chmod +s dbRsync (or whatever the executable is) to set the sticky bit or
   it won't work.
*/

int main(int ac, char **av) {
    int uid;
    uid = geteuid();
    setreuid(uid, uid);
    execv( "/path/to/your/a.py", av );
}

Sunday, September 18, 2016

How To Diff A Directory Efficiently

diff -qr -x '*~' -x '*.swp' -x '*.pyc'  -I '$Revision:'  -I '$File:'  -I '$Header:' -I '$Change:' -I '$DateTime:' -I '$Id:' -I '$Date:' -I '$Change:' -I '$Author:' directory1 directory2

Wednesday, August 17, 2016

How To Write Codes In Perl For Accomodating To Simple Testings

In order to write scripts in perl which are testable, it is good to follow the following proposed method (assuming this code is inside file a.pl):-


#!/usr/bin/env perl                                                                                   

sub main
{                                                                                                  
    ### Your main code goes here           
    # ... ... ...                          
    1;                                                         
}


sub is_five
{
    my $num = shift(@_);
    if ($num == 5)
    {
        return 1;
    }
    else:
    {
        return 0;
    }
} # is_five


############################
# This loop will only be entered if this script is called explicitly, ie:-
#   $./a.pl
#
# This loop will not be entered If this file is required, ie:-
#   require 'a.pl'
############################
unless (caller)
{
    main();
}
Now, to write a test that tests the is_five() function, we can create a file call test_a.py, and write the test like this:-
#!/usr/bin/env perl

use Test::Simple tests => 2;
require "a.pl";

sub test_is_five___pass
{
    return is_five(5)
}
sub test_is_five___fail
{
    return is_five(3)
}

unless (caller)
{
    ok(test_is_five___pass());
    ok(! test_is_five___fail());
}

Thursday, August 4, 2016

The Untold Story Behind The Christmas Tree

Christmas Tree are a must during ....well......Christmas. (duh)

Especially when it comes to malls and huge shopping complexes, or theme parks.

People love gathering around a well decorated Christmas tree for photo shots.

Well, there was this mall which decided to make it different. They brought in a 1000 pound 100 feet Christmas tree.

2 groups of people are put in charged of making it happen
- one is responsible for decorating it with bells and whistles
- the other is responsible for making it stable so it doesn't collapse and is safe for people to stick around.

Well, I'm lazy to type. You know most of the drill. Along the process , debates on whether to emphasize more on the decorative or to put more budget and resource on safety is a never ending process.

To cut the story short, here is how it goes.....

If the tree stays put for the rest of Christmas,  no customer or even anyone is going to even notice the existence of the group that puts in effort to make the Christmas tree safe.

But If the tree falls, they are the ones that will get all the heat first.

Well, as for the group of people that focus on adding bells and whistles?

Regardless of whether the tree falls or not, they will be the first to get all the credits during the first day of Christmas when all the people pill over and stick around the Christmas tree.

What if the tree falls on the next day? Well, these folks have already claimed their credits, and are already on their way planning on decorating their next Christmas tree for the next year.

That is the hard truthful fact of life.

Wednesday, April 13, 2016

Ocean's 5th jab

Brought Ocean to vet again as he seems to show signs of painfulness in his mouth and has not been eating well for the past few days.

We decided to seek advice from the vet to see if it is possible to switch to medication instead of jabs, if that is a better option.

Vet gave a jab that will last for 2 days, and gave us a week's medication. Dr. Amelia told us to start giving Ocean the medication starting this Friday, and we should bring Ocean in again for check out a week later.

We plan to crush the pill and mix it with can food, which is what Ocean seems to like lately.
Hopefully with this change, he does not need to endure so much suffering and can have a better quality life moving forward.

How To Bypass Kerberos(kinit) Authentication

Whenever you try to setuid and impersonate as someone else to run something, it is very likely that you will run into kerberos/kinit issues....