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

Saturday, 18 July 2015

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