Monday 14 December 2015

perl mysql connection note

### From mysql side ###

NOTE:
1. Install mysql server.
2. update /etc/mysql/my.cnf to listen outside from the localhost.
3. You should have a user with the access to a database, By default you can connect to remote mysql database as root user.
4. The user should have the access to connect from remote host.

https://www.digitalocean.com/community/tutorials/how-to-create-a-new-user-and-grant-permissions-in-mysql

# creating a test database
CREATE DATABASE test; 

# created a mysql user [ For this example, I have created an mysql user ]
CREATE USER ‘mysql'@'%' IDENTIFIED BY 'password';

# Grant the mysql user to access database [ Not secure example but, just for example ]
GRANT ALL PRIVILEGES ON * . * TO 'mysql@'%';

# Enable the mysql server to listen from outside
/etc/mysql/my.cnf
#bind-address           = 127.0.0.1


##########################################################
##########################################################

### From perl side ###
#!/usr/bin/perl

use DBI;
use strict;

my $host = ‘mysql_db_server';
my $driver = "mysql";
my $database = "test";
my $dsn = "DBI:$driver:$database;host=$host";
my $userid = "mysql";
my $password = "password";

my $dbh = DBI->connect($dsn, $userid, $password ) or die $DBI::errstr;

NOTE: you need to have DBI::mysql module in your host.


####### another example ######

#!/usr/bin/perl

use DBI;
use strict;

my $host = ‘mysql_db_server';
my $driver = "mysql"; 
my $database = "test";
my $dsn = "DBI:$driver:$database;host=$host";
my $userid = "mysql";
my $password = "password";

my $dbh = DBI->connect($dsn, $userid, $password ) or die $DBI::errstr;


#
#

my $query="show databases";
print "\n\UFollowing is the output of query: \"$query\"\n\n";
my $sth=$dbh->prepare($query);
$sth->execute();

while (my @row=$sth->fetchrow_array)
{
  print $row[0]."\n";
}

#
#
$query="show tables";
print "\n\UFollowing is the output of query: \"$query\"\n\n";

$sth=$dbh->prepare($query);
$sth->execute();

while (my @row=$sth->fetchrow_array)
{
  print $row[0]."\n";
}


#
#
$query="describe domains";
print "\n\UFollowing is the output of query: \"$query\"\n\n";

$sth=$dbh->prepare($query);
$sth->execute();
while (my @row=$sth->fetchrow_array)
{
  print $row[0]."\n";
}

$sth->finish;

## Note: close, only at the end. No need to close in middle.

### output of the above example ###


FOLLOWING IS THE OUTPUT OF QUERY: "SHOW DATABASES"

information_schema
mysql
performance_schema
test

FOLLOWING IS THE OUTPUT OF QUERY: "SHOW TABLES"

domains

FOLLOWING IS THE OUTPUT OF QUERY: "DESCRIBE DOMAINS"

id
name
url
descr


Ref links
http://aruljohn.com/code/perl/mysqlselect.html

Friday 11 December 2015

Using ssh-agent


Using ssh-agent

ssh-agent -s > AmundSshAgent
source AmundSshAgent
ssh-add <key>


Sometime,  we need to perform some daily task, that need grant access. In these case, we can use the following few tricks to make use along with cronjobs.

[step1]

$ ssh-agent -s > sshagent_amit

[step2] [you should see something like following output. ]

$ source sshagent_amit

$ cat sshagent_amit

SSH_AUTH_SOCK=/tmp/ssh-iHFFf28257/agent.28257; export SSH_AUTH_SOCK;
SSH_AGENT_PID=28258; export SSH_AGENT_PID;
echo Agent pid 28258;

[step3]
ssh-add <your_keys>

NOTE: After the system got restared, you need to update your agent file, or you can automate this one too.

Now you can just logout, and login again and source the file [ example: sshagent_amund] and then, you should see your keys, when you type "ssh-add -L"

[step4]
The script that we have plans to add into the crontab.

#!/bin/bash
source sshagent_amund
ssh amit@remote_host "command" > output_file.txt


In cron entry:

* * * * * /bin/bash -l -c '/path/to/your/script'


-l        Make bash act as if it had been invoked as a login shell (see INVO-CATION below).
-c        Command


update the cron time as per your need.

Wednesday 11 November 2015

sudo su as

Type 1:

cat ScriptFile
whoami
sudo su - user2 <<EOF
whoami
EOF


$ ssh user1@RemoteHost 'bash -s' < ScriptFile 

Output:
user1
user2
Stderr: Pseudo-terminal will not be allocated because stdin is not a terminal.



using pssh:
[ in this approach: (-l)  no need to copy file to remote host to run the command. ]

$ pssh -i -H RemoteHost -l user1 -o OutPutDir -I 'bash -s'< ScriptFile 

[1] 19:28:14 [SUCCESS] RemoteHost
user1
user2

$ pssh -i -H RemoteHost -l user1 -o OutPutDir -I < ScriptFile 

Output:
[1] 19:27:30 [SUCCESS] RemoteHost
user1
user2
Stderr: Pseudo-terminal will not be allocated because stdin is not a terminal.


NOTE: [ quick pssh help]


-h HOST_FILE, --hosts=HOST_FILE
-H HOST_STRING, --host=HOST_STRING
-l USER, --user=USER  username (OPTIONAL) [ small l ]
-p PAR, --par=PAR     max number of parallel threads (OPTIONAL)
-o OUTDIR, --outdir=OUTDIR
-i, --inline          inline aggregated output and error for each server

-I, --send-input      read from standard input and send as input to ssh [ caps i ]


pssh -i -h hosts.txt -l user1 o OutputDir -I 'bash -s' < ScriptFile

pssh -h hosts.txt -l irb2 -o /tmp/foo uptime


Tuesday 10 November 2015

perl html5 video page gen code

#!/usr/bin/env perl

print ("<!DOCTYPE html>\n");
print ("<html>\n");
print ("<body>\n");

my @files = `ls *.mp4`;

foreach $a (@files) {
    print ("<video width=\"320\" height=\"240\" controls>\n");
    print ("<source src=\"$a\" type=\"video/mp4\">\n");
    print ("</video>\n");
}

print ("</body>\n");
print ("</html>\n");

Wednesday 12 August 2015

php image gallery



Steps:


  1. create your main folder. [example pictures ]  [ mkdir pictures]
  2. create another folder as images under pictures [ mkdir pictures/images ] [ NOTE: In the following code the subfolder name it expect as images ]
  3. copy the following code to pictures folder, as filename index.php
  4. copy all your images to pictures/images




<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Gallery from Folder Demo</title>
<style type="text/css">
<!--
li{
list-style-type:none;
margin-right:10px;
margin-bottom:10px;
float:left;
}


-->
</style></head>


<body>


<ul>
<?php
$dirname = "images/";
$images = scandir($dirname);
shuffle($images);
$ignore = array(".", "..");
foreach($images as $curimg){
if(!in_array($curimg, $ignore)) {
echo "<li><a href=\"$dirname$curimg\"><img src=\"$dirname$curimg\" height=200 width=200 /></a></li>\n ";
}
}
?>
</ul>


</body>
</html>


### compress image ###

Along with the above you also might need to compress the file.

at ubuntu, you can use "convert" command to compress the image.

NOTE: the smaller the percentage the smaller is the file.

convert source.jpg -resize 20% destination.jpg

A 4MB file 20% came around 350KB file.


Tuesday 28 July 2015

tmux

Quick tmux commands:

NOTE: [ consider all following key is after the combination (ctrl-b) [ In standard configuration ]

split tmux vertically     : %
split tmux horizontally : "

Moving between panes:
Move Left                     : left arrow or {
Move Right                   : right arrow or }
Move Up                       : up arrow
Move Down                  : down arrow
next pane                       : o
show number of each panes and move: b q + number


Resizing panes

You may want to resize panes to fit your need. Here’s a list how to do that :

(Ctrl-b) + : then type resize-pane -D (Resizes the current pane down)
(Ctrl-b) + : then type resize-pane -U (Resizes the current pane upward)
(Ctrl-b) + : then type resize-pane -L (Resizes the current pane left)
(Ctrl-b) + : then type resize-pane -R (Resizes the current pane right)
(Ctrl-b) + : then type resize-pane -D 5 (Resizes the current pane down by 5 cells)
(Ctrl-b) + : then type resize-pane -U 5 (Resizes the current pane upward by 5 cells)
(Ctrl-b) + : then type resize-pane -L 5 (Resizes the current pane left by 5 cells)
(Ctrl-b) + : then type resize-pane -R 5 (Resizes the current pane right by 5 cells)
(Ctrl-b) + : then type resize-pane -t 2 5 (Resizes the pane with the id of 2 down by 5 cells)
(Ctrl-b) + : then type resize-pane -t -L 5 (Resizes the pane with the id of 2 left by 5 cells)

press (Ctrl-b) + : resize-pane -D 13 to make it down for 13 cells.


create more window: ctrl-b c

(Ctrl-b) + n : Move to next window
(Ctrl-b) + p : Move to previous window
(Ctrl-b) + w : Interactively choose the window (useful if you have more than 2 window)


Detached: ctrl-b d

Listing the tmux session: tmux ls

attaching the tmux session: tmux attach -t <session-name>

### sync tmux panes ###

Ctrl-B :
setw synchronize-panes on
clear history

Friday 24 April 2015

Interesting motd with figlet or toilet


What if you want to have interesting motd?

You can do these interesting motd using figlet.

Example:




You Just need to install [ toilet ] and try these interesting output.

sudo apt-get install toilet

Friday 10 April 2015

ansible-playbook for docker cassandra cluster.

The following command will create a cassdb cluster using ansible.
#####
---
- hosts: all
  tasks:
  - name: coping the file to all the docker server
    template: src=cassandra.yaml.j2 dest=/tmp/cassandra.yaml
  - name: running some command
    raw: sudo docker run -i -t -d --name cassdb --net host -v /cassdata:/var/lib/cassandra cassdb /bin/bash; containerID=`sudo docker inspect -f '{{.Id}}' cassdb`; sudo cp /tmp/cassandra.yaml /var/lib/docker/aufs/mnt/$containerID/etc/cassandra/cassandra.yaml; sudo docker exec cassdb rm -rf /var/lib/cassandra/*; sudo docker exec cassdb /etc/init.d/cassandra start



#####
command would be:

ansible-playbook  docker-inventory_hosts install_cassandra_playbook.yml



########

NOTE: Following is the variable that I have added in the "cassandra.yaml.j2" file, that need to be interpolated, dynamically.

cluster_name: {{ cluster_name }}
          - seeds: {{ hostvars[groups['docker-akamai'][0]]['ansible_eth0']['ipv4']['address'] }}
listen_address: {{ ansible_default_ipv4.address }}
broadcast_rpc_address: {{ ansible_default_ipv4.address }}


### my cassdb docker image:

docker pull amitmund/cassdb:latest

#######

$ sudo docker exec cassdb nodetool status
Note: Ownership information does not include topology; for complete information, specify a keyspace
Datacenter: BLR
===============
Status=Up/Down
|/ State=Normal/Leaving/Joining/Moving
--  Address        Load       Tokens  Owns    Host ID                               Rack
UN  192.168.1.10  54.93 KB   256     37.4%   e7915e97-babf-49fd-bcdf-4434b4443fba  R1
UN  192.168.1.11  261.03 KB  256     32.7%   a0589928-f5eb-496f-8456-5018fabb75ca  R1
UN  192.168.1.12  169.35 KB  256     29.9%   219d1439-fd0d-4ea9-ae03-4d25fe0334fc  R1


Thursday 12 February 2015

How to do port forwarding in virtual box.

How to do port forwarding in virtual box.


1. select your vm that you want for port forwarding.
NOTE: Make sure the vm is in stop mode.
2. select “settings"
3. select “Network"
4. select any free Adapter. “Lets say Adapter 2"
5. enable Enable Network Adapter 
6. In Attached to dorpdown, select NAT
7. Select Advanced
8: In Adapter Type: Select “Paravirtualized Network (virtio-net)”  [ This is important ]
9. Select “Port Forwarding"
10. In Right hand part of the “port forwarding” dialog box, select “+"
11. Provide the :
  Name: Anything you want. example for ssh, say “ssh"
  Protocol: Type of protocol [ for ssh: TCP ]
  Host IP: provide hostname from which host you want to connect to [ over here: 127.0.0.1 ]
  Host Port: On what port of that Host you want to connect to remote port [ example: 60022 ]
  Guest IP: Leave it Blank

  Guest Port: To what port you want to connect from the above host. [ for ssh, the default 22 ]

Monday 26 January 2015

html_frame

For the example of 8 section.


<!DOCTYPE html>
<html>


<frameset cols="50%,*" rows="25%,25%,25%,25%">
  <frame src="http://url1/">
  <frame src="http://url2">
  <frame src="http://url3">
  <frame src="http://url4">

  <frame src="http://url5">
  <frame src="http://url6">
  <frame src="http://url7">
  <frame src="http://url8">

</frameset>

</html>