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