Tuesday 8 March 2016

Linux networking commands



PUTTy PSCP (Putty Secure Copy Program)

      These commands really saved me lot of time when i have to copy huge files (GBs!!) from different server machines, as opposed to drag-drop the files.

·         Install PSCP and copy to preferably in C:\Windows\System32
·         Open cmd->in promt: set PATH= C:\Windows\System32 
(This will only work for the lifetime of that particular console window)
                         OR
                      Cygwin tool can be used.
                     Tool I love:  Babun for windows.



DOWNLOAD OR COPY FILES FASTER between multiple host machines

                                              I    LOCAL machine to REMOTE Machine copy

      1) Copy files from Local machine to Remote machine


       Syntax:
pscp -i "<ppk_file_path>" -P <port_no> <local_file_name> <remote_user_name>@<host_name>:/<path_in_remote>

       EG:   pscp -i "D:\sample_ppk_file.ppk" -P 221 sample.txt  root@10.20.2.72:/var/www


      2)  Copy entire Directory from local to remote (-r   copy source file recursively)

      Syntax: 
pscp -i "<ppk_file_path>" -P <port_no> <local_folder_name>\* <remote_user_name>@<host_name>:/<path_in_remote>

Eg: pscp -r -i "D:\sample_ppk_file.ppk" -P 221 test.csv\* root@10.20.2.72:/var/www


OR using rsync command:

rsync -rv --exclude="*.sql" --exclude="*.jpg" --exclude=".git/" /local/folder/path  user@host:/destination/server/path




3) Copy multiple directories to remote

pscp -r -i "D:\cloud-admin.ppk" -P 22 <localdir1><dir2> --exclude=/cygdrive/d/../* root@10.20.2.72:/var/


                                            II    REMOTE machine to LOCAL machine copy

scp -i  "D:\sachin\Metro_Web.ppk" -P 141 metrouser@metroretail.cloudapp.net:/DBdrive/MetroImagesUpload /Cygdrive/d/<path>


NOTE:  /cygdrive is the command to access the local machine files(in Cygwin or babun tools)

                                                    III    REMOTE machine to REMOTE copy

      1)      Convert ppk to ppm

sudo apt-get install putty-tools
puttygen ppkkey.ppk -O private-openssh -o UATPemkey.pem

             Place the pemkey.pem file in your ~/.ssh directory: as:
cp pemkey.pem ~/.ssh
chmod 400 pemkey.pem


2) scp -i /<path_to_ppm_file> /<source_remote_path>  <dest_user@host_name>:/<dest_remotepath>


If in case the copy got aborted/ your machine got switched off, the same can be resumed using rysync command:
Resume download using rsync
To access local Drive file:   /cygdrive/d/<path>

rsync -avmz -e "ssh -p 141 -i /cygdrive/d/sachin/your_ppk_file.ppk"   username@host_name:/file_path/filename.sql /cygdrive/d/sachin/

Tar Zip and Unzip 

compress a directory:
# tar -cvf     archive_name.tar      directory_to_compress

And to extract the archive:
# tar -xvf   archive_name.tar.gz

It gives very good compression while not utilizing too much of the CPU while it is compressing the data.
# tar -zcvf   archive_name.tar.gz    directory_to_compress

 tar -zcvf <custom_file_name>.tar.gz <file_to_compress>

To decompress an archive use the following syntax:
# tar -zxvf    archive_name.tar.gz


Creating a symlink from another systems network folder to windows drive

> Open the windows cmd prompt with administrator privileges

> mklink /d D:/Windows_source_folder \\HMEC__\Network_Folder

Note:
Windows_source_folder: Should not be present, the folder will be created by mklink command.

                                                              MYSQL

To allow remote access to mysql from command line inside your virtual machine

> Assuming you don't have any gui tools to connect to mysql like phpmyadmin and mysql-workbench

> As root, open your /etc/mysql/my.cnf .

> Look for the [mysqld] section, and in there for the bind-address keyword.
   This usually is set to 127.0.0.1 -- change that to match your "normal" IP-address
        ie) the IP which you want to access to MSQL.

> service mysql restart

> Remember you must enable your remote users to access their database(s) from remote, by setting the appropriate GRANTs
e.g.
     GRANT ALL PRIVILEGES ON <databasename> TO 'root'@'%' IDENTIFIED BY 'mysqlpassword' WITH GRANT OPTION;

    FLUSH PRIVILEGES;

     (Note the @'%', which means "from any host".)


DB BACKUP command

mysqldump  -u root –p databasename  --routines > [path]  eg: /var/import/dbbackups/fuldump.sql 


Dump single table from DB

If you're in local machine then use this command

/usr/local/mysql/bin/mysqldump -h127.0.0.1 --port = 3306 -u [username] -p [password] --databases [db_name] --tables [tablename] > /to/path/tablename.sql;

For remote machine, use below one

/usr/local/mysql/bin/mysqldump -h [remoteip] --port = 3306 -u [username] -p [password] --databases [db_name] --tables [tablename] > /to/path/tablename.sql;

Eg:
mysqldump -u root -h <VM_IP> -p DBNAME Table_name > /Destination_Path/filename.sql


DB Restore command     

Mysql –u root –p databasename < [path]

Format mysql output format in SSH terminal 

mysql>  pager less -SFX

Leave this view by hitting the q key, which will quit the less too

Count number of tables in DB (phpmyadmin)
              
SELECT count(*) AS totalTables
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_SCHEMA = 'yourDBName'





Navigation Commands

Command
Description
Ctrl+v
Block Selection
gg
Go to first line
Shift+g
Go to last line
o
Go to next line and start editing
Shift+o
Go to previous line and start editing
Shift+v
Highlight current line
Shift+v Shift+g
Highlight current line until end of file
Shift+^
Go to to the beginning of the line
Shift+$
Go to the end of the line

Working with multiple files

Command
Description
:split filename
Split window and open file in new split
:vsplit filename
Split window vertically and open file in new split
:vs filename
Split window vertically and open file in new split
ctrl-w ctrl-w
Go to next window

Recipes
Command
Description
gg Shift+v Shift+g
Highlight the whole file
Ctrl+v
Block Selection










Configuring AngularJS and MongoDB on windows 7

Required tools :
1)      Babun/Cygwin 
2)      Node.js (can be downloaded here: http://nodejs.org/)
3)      Mongo DB (can be downloaded here:  http://www.mongodb.org/downloads)
4)      Express-generator
a.       $ pact install npm (if using babun)
b.      npm install express-generator –g   (To install express)
c.       express –h  (To display command options)
d.      express showtrackr  (creates an express app)
e.       Then install dependencies
                                                              i.      cd showtrackr
                                                            ii.      npm install
                                                          iii.      Run the app: npm start and

                                                          iv.      Hit http://localhost:3000/


Configuring MongoDB on windows 7

Ø  Cd to node_modules

Ø  node_modules> npm install mongodb
(we should have the node_modules/mongodb folder available)


Hotfix for mongodb is available here:


To start Mongo DB service:

Ø  Net start mdb27017    (27017- default mongoDB port)

Can be checked in windows services(Windows key-> search 'services'->Search for MongoDB Services as shown below )


NOTE: 

Check list/ solutions for errors during installation:

       i)    In the node_modules folder of our project folder, there should be mongodb folder, which confirms mongodb is installed.




    ii ) If we encounter below error:


    Then, 

      npm install -g node-gyp
     git clone https://github.com/mongodb/js-bson.git
     cd js-bson
     npm install
     node-gyp rebuild 
                         
                                                        or
    In the MongoDB/node_modules, search

 bson = require('../build/Release/bson');

 and in catch block change to

 bson = require('../browser_build/bson');


Note:  To start MongoDB

In windows cmd promt:
 cd to bin of MongoDB

Ø  Mongo.exe

Ø  Use <DBname>

Ø  Show < DBname >
   














Install and Configure Hybris 5.7 suite in Ubuntu or windows

HYBRIS 5.7 BUILD & INSTALLATION STEPS (LINUX)

 Step 1)      Unzip the downloaded hybris 5.x suite to the destination folder.
       (To unzip:  Unzip hybris-commerce-suite-5.5.0.0.zip –d  /var/www/html/Hybris E-Commerce)

Note: Scroll below for troubleshooting issues- For solving errors regarding ANT, JAVA Path etc.

Step 2) Install the Recipe.
         
             Syntax (in linux): install.sh –r recipeName
              
             Recipes are used to install B2C accelerators,B2B accelerators ,Telco accelerator...
         
             Before installing the recipes, we will find only the bin folder inside the hybris folder.

             To install the recipe, navigate to the installer path in command,

             for Linux:        hybris_test/installer$   install.sh –r b2c_acc_plus
             for windows:   hybris_test/installer$   install.bat –r b2c_acc_plus

                    
             This will add all the required extensions to localextensions.xml as per the recipeName                   we have give in the command.

             We have to get Build Successful message in the cmd prompt.

              Now check the hybris folder, we get
             
 

   
             Here is my localextensions.xml after installing the recipe:  

             <hybrisconfig xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xsi:noNamespaceSchemaLocation='../bin/platform/resources/schemas/extensions.xsd'>
  <extensions>
    <path dir='${HYBRIS_BIN_DIR}' autoload='false' />
    <extension name='mcc' />
    <extension name='backoffice' />
    <extension name='commercesearchbackoffice' />
    <extension name='commerceservicesbackoffice' />
    <extension name='solrfacetsearchbackoffice' />
    <extension name='solrserver' />
    <extension dir="${HYBRIS_BIN_DIR}/custom/training/trainingcore"/>
    <extension dir="${HYBRIS_BIN_DIR}/custom/training/trainingfacades"/>
    <extension dir="${HYBRIS_BIN_DIR}/custom/training/trainingtest"/>
    <extension dir="${HYBRIS_BIN_DIR}/custom/training/trainingstorefront"/>
    <extension dir="${HYBRIS_BIN_DIR}/custom/training/trainingfulfilmentprocess"/>
    <extension dir="${HYBRIS_BIN_DIR}/custom/training/traininginitialdata"/>
    <extension dir="${HYBRIS_BIN_DIR}/custom/training/trainingcockpits"/>
    <extension name='yaddon' />
    <extension name='ycommercewebservices' />
    <extension name='electronicsstore' />
    <extension name='apparelstore' />
    <extension name='captchaaddon' />
    <extension name='liveeditaddon' />
    <extension name='acceleratorwebservicesaddon' />
  </extensions>
</hybrisconfig>

Step 3) Installing the modulegen
  
             Navigate to bin/platform folder in cmd.  (/var/www/html/hybris_test/hybris/bin/platform)
            
             In linux:         . ./setantenv.sh    (. <space> ./setantenv.sh)  
             In windows:  . ./setantenv.bat
      
             NOTE : If windows you can refer the link : http://hybrisdiary.com/2015/09/26/installing-hybris-in-local-system/

            Run the modulegen command as follows: 
           
            ant modulegen -Dinput.module=accelerator -Dinput.name=training -Dinput.package=com.hybris.training -Dinput.template=develop

            Explanation of the command :
    
            Dinput.module=accelerator    => we are installing B2C module 
            Dinput.name= training (can be given any name)   => name of the module
            Dinput.package= com.hybris.training   (can be given any name) 
            Dinput.template= specifies whether configuration is for development or production.

           We have to get Build Successful message in the cmd prompt. 

      (Follow the 1st 2 steps specified in the command prompt). Refer above for my localextensions.xml file after following the steps from the command prompt. 

   Step 4)  Run ant clean all in the same folder path (bin/platform) . Build should be successful.

                

   Step 5) Start the Hybris server: 
       
                in Linux:  ./hybrisserver.sh run
                in Win:     hybrisserver.bat debug

  Step 6) Initialize the Hybris server.
               Can be done in 2 ways: 

                     1) Through HAC(Hybris Admininstration Console) ---  Make sure Toggle all checkbox   is selected.                                     or
                     2) Through command :  ant initialize

              Wait until Hybris starts and until the command shows:
              Dec 12, 2014 3:39:08 PM org.apache.catalina.startup.Catalina start

              INFO: Server startup in 163949 ms

     Hybris is now ready to use: 

     Default port :  http://localhost:9001
            or           http://<IP>:9001
                        

 Step 7) Enter below URL mappings in Host file in the below path:
              in linux:    /etc/hosts
              in win:     C:\Windows\System32\drivers\etc 

             
      127.0.0.1   apparel.local
      127.0.0.1   electronics.local
      <IP>        ... 

            If you encounter DNS lookup failed when accessing the URL, refer the below link:

             https://hybrisdiary.com/2015/08/07/the-server-at-electronics-local-cant-be-found-because-the-dns-look-up-failed/
 
 
 Step 8)  Access sample site shipped with Hybris:

              Apparel UK site
             http://apparel.local:9001/trainingstorefront/?site=apparel-uk
             
            

      Similarly 
  
            Apparel-DE site
           http://localhost:9001/ trainingstorefront/?site=apparel-de


           Electronics site
           http://localhost:9001/ trainingstorefront/?site=electronics


           TroubleShoot during configuration:
                 

         1)  If you encounter the following ERROR: ant build : unable to locate tools.jar. Expected find it in C:\Program Files\Java\jre7\lib\tools.jar)

you can just copy the tools.jar from "C:\ProgramFiles\Java\jdk1.7.0\lib" directly into "C:\Program Files\Java\jre7\lib\".You will successfully recover the problems.


        NOTE: For above issue In Ubuntu: Install JDK :  sudo apt-get install default-jdk


2)  To install Java on Ubuntu using  apt-get

       sudo apt-get update

 Then, check if Java is not already installed:

         java -version
 
       If it returns "The program java can be found in the following packages", Java     hasn't been installed yet, so execute the following command:

         sudo apt-get install default-jre 
      This will install the Java Runtime Environment (JRE). If you instead need the Java Development Kit (JDK), which is usually needed to compile Java applications (for example Apache Ant, Apache Maven, Eclipse and IntelliJ IDEA execute the following command:

       sudo apt-get install default-jdk
  
 3)  Setting the "JAVA_HOME" environment variable

i) First find out the path of your Java installation:

  sudo update-alternatives --config java
 
ii) The path of the installation is for each:
  1. /usr/lib/jvm/java-7-oracle
  2. /usr/lib/jvm/java-6-openjdk-amd64
  3. /usr/lib/jvm/java-7-oracle
 iii) Copy the path from your preferred installation and then edit the file 
      /etc/environment:
 
           sudo nano /etc/environment 
 iv) In this file, add the following line (replacing YOUR_PATH by the just copied path): 
 
    JAVA_HOME="YOUR_PATH"
 
    Don't forget to remove bin/java from the end of the path while putting it into JAVA_HOME 
 
 v) Now reload this file:
 
    source /etc/environment
 
 vi) Test it by executing:
 
         echo $JAVA_HOME 




  





CONFIGURE HYBRIS AS A SERVICE – To auto start hybris on boot

1)      Stop the server:                ./hybrisserver stop       (or ctrl –c   exit)

2)      Create a hybris service startup file:    vi /etc/init.d/hybris  
a.       Setting the base path in the file to your hybris installation:

                        HYBRIS_HOME="/var/www/html/Hybris E-Commerce/hybris" and save the file.

b.    If error is thrown in this line:
accepted
# Source function library
. /etc/init.d/functions

Replace with:                            # Source function library
                        . /lib/lsb/init-functions

c.       Set permission for the file to be executed:
  chmod 755 /etc/init.d/hybris

3)      Add the file to the server startup list:

chkconfig --add hybris
chkconfig hybris on


4)      To rebuild hybris :
cd /home/hybris/hybris/bin/platform
                                                         . ./setantenv.sh
ant clean all          # command to rebuild

NOTE: To upgrade higher ANT version:
i)                    Download “apache-ant-1.9.6.zip” and extract to /usr/local
ii)                   Under hybris/bin/platform: 


iii)                 To check version of ant:  ant -version

5)      Start Hybris server:             ./hybrisserver.sh run

Hurray HYBRIS is now ready to RUN!!!

    Default localhost:              http://<localhost>:9001

    Default Factory password:
                                                User:         admin   
Password: nimda




http://<localhost>:9001/mcc




http://<localhost>:9001/admincockpit






To install/save Custom extensions in a separate directory.


If the above cockpits are not loaded, we need to add the extensions in the 
hybris\config\localextensions.xml




In localextensions.xml

<extension>
    <!--
    There should be a path dir defined within the config like below
  -->
    <path dir="${HYBRIS_BIN_DIR}" />
    <!--
    Navigate to where your extensions are defined, you can create your 
    own path variable above if you want too, this is just an example for you  -->
  
    <!-- ext-template -->
    <extension name="yempty" />
 
    <!--Begin Custom extensions -->
    <extension name="admincockpit" /> 
    <extension name="backoffice" />
    <extension name="cockpit" />
    <extension name="hmc" />    <!--Hybris management console -->
    <extension name="mcc" />
    <extension name="platformhmc" /> 
    <!--End Custom extensions -->
 
 
  <extension name="${HYBRIS_BIN_DIR}/../../myExtensionsFolder/extension1" />
  <extension name="${HYBRIS_BIN_DIR}/../../myExtensionsFolder/extension2" />  
</extension>
 

To configure Hybris to use MYSQL

    As we know that hybris suite by default provides HSQL database,
    so if we want to connect to MySQL database we need to do the following modifications:

   1) Under  \hybris\bin\platform\project.properties
  
       Under DATABASE SETTINGS (apprx line 191)
    
       Comment-  "Database settings for HSQLDB"
       and Uncomment the "Database settings for MySQL 5.1"

  2) This is my configuration for MYSQL:
   
     db.url=jdbc:mysql://localhost/hsqldb?useConfigs=maxPerformance&characterEncoding=utf8&useSSL=false
     db.driver=com.mysql.jdbc.Driver
     db.username=root
     db.password=<ur DB password>
     db.tableprefix=
     db.customsessionsql=SET SESSION TRANSACTION ISOLATION LEVEL READ COMMITTED;
     mysql.optional.tabledefs=CHARSET=utf8 COLLATE=utf8_bin
     mysql.tabletype=InnoDB
     mysql.allow.fractional.seconds=true     

     Create a DB (say as in above config: 'hsqldb') in MYSQL.

3) Since hybris Commerce Suite version 5.0.2, MySQL connector is not shipped with the hybris   Commerce Suite,
Hence download the below mysql java connector and place it in hybris/bin/platform/lib/dbdriver
MYSQL java connector link :
NOTE:  With MySQL 5.6.4 (or later) the mysql.allow.fractional.seconds property must be defined.
Before 5.6.4 you must either omit the mysql.allow.fractional.seconds property or
set it for false! In project.properties file.

4) reload/build ant:  ant clean all

5) Initialize the system in HAC or use :  ant initialize
    (ant clean all initialize can also be used)

6) Say:    
    mysql -u root -p 
    show databases;
    use hsqldb;
    show tables;
    We can now check that our DB ('hsqldb') has loaded with Hybris default tables.