ssh & run commands on remote host

ssh <username>@<hostname> ‘<command>’

To disable printing warning messages, use –q option to connect in quiet mode

Run multiple commands with – ssh –q <username>@<hostname> ‘<command>; <command>; <command>’

 

To run local script on remote host, use bash –s option, additionally for supplying argument to the local script use --

ssh –q <username>@<hostname> “bash –s” -- < /path/path/localScriptName arg1 -arg2 arg3

useful Unix command shortcuts

Searching command history

  • ctrl + r and then type the keyword to search.
  • use ctrl + r to scroll recursively.
  • to enable backward scrolling of history, first turn ON intelligent search option by running the command –> stty –ixon
  • Now use ctrl + s to scroll other direction too, cool huh :)
  • !! command will run the previous command, !<number> will run the specific command retrieved from the history command, !:<number> will replace it with specific argument from previous command.
  • first argument from the previous command is stored in !^ and last argument in !:$
  • Argument from previous specific command can be retrieved using !<command>:<number> Example: !cp:2 would get the 2nd argument that was run on cp command previously, used like vi !cp:2

 

Managing history command

  • Remove duplicate commands from history with, this can be added in .bash_profile file
  • Continuous duplicates entry can be removed with, export HISTCONTROL=ignoredups
  • Remove all duplicates from history with, export HISTCONTROL=erasedups
  • Note: These exports would effect only for the commands after they have been set, meaning previous old duplicates would remain in the history.  May be we can clear all history with history –c command, and then export these values, but we will miss the initial existing history this way.  Any other ways to make the export affect previous initial history too ?
  • Running a command without allowing it to exist in history, HISTCONTROL=ignorespace for this command to work, we need to provided a space before entering a command so that, that particular command is not remembered in history.  Example: ‘ pwd’ (notice space at the start)
  • Disable specific commands from being tracked in history export HISTIGNORE="<command>:<command>:<command>" Example: export HISTIGNORE="pwd:ls:ls -ltr:"
  • Disable history with, export HISTSIZE=0
  • Note: Multiple values for HISTCONTROL can be exported by using : (colon)  as delimiter.  Example in my case  export HISTCONTROL=ignorespace:erasedups

install apache benchmarking tool - ‘ab’

On WINDOWS machine :

Download httpd-****-x86 from http://httpd.apache.org/download.cgi

After going through the installation process –> Go to the directory where its installed

similar to C:\httpd-2.4.12-x86\Apache24\bin in my case, add this to system ENV Variable

 

On UNIX machine :

did not work –>> sudo yum install httpd-tools

Below long procedure worked, and it also needed to switch to root user and running it, running command with sudo did not work

You will need to install apr-utils and yum-utils first.

yum -y install apr-util
yum -y install yum-utils

 

extract Apache Benchmark from httpd-tools. And here is how.

mkdir ~/httpd-tools

cd ~/httpd-tools

yumdownloader httpd-tools

 

Now extract ab (Apache Benchmark) and move it to  /usr/bin  folder

rpm2cpio httpd-tools-2.2.24-1.29.amzn1.x86_64.rpm | cpio -idmv

mv usr/bin/ab /usr/bin/ab

cd ~

rm -rf ~/httpd-tools

 

 

sample command to test with -

ab -ki -n 500000 -c 200 http://rusticode.com/2013/04/07/lannn-installing-apache-benchmark-on-centos-ec2-linux-ami-with-nginx/

-n    specifies the number of requests,

-c    number of concurrent connections,

-k    stands for HTTP Keep-Alive,  

-t    maximum time of awaiting for response from connection

Setup up ssh to login without entering password

Considering situation where we need to connect from Server A to Server B

  • On Server A, run the below command to generate the public key

    ssh-keygen -t rsa

    Note: If you don’t have ssh-keygen command recognized in your system, install Git - which will provide required file for running ssh-keygen command.

    After installing Git, add this/similar path to your system environment variable C:\Program Files (x86)\Git\bin\ssh-keygen.exe

    This command should generate id_rsa.pub file in your <homedir>/.ssh/ directory

  • On Server B

    create a folder in the <homedir> with name .ssh

    Copy the id_rsa.pub file from Server A, into this .ssh directory.

    Append the contents of id_rsa.pub file to authorized_keys file inside the .ssh directory on Server B.  If one authorized_keys file does not exist – create it.

That’s it! now we should be able to login without having to enter password every single time.

Loading jndi.properties file from specific path

         Context context = null;
         try {
                File configFile = new File("<provide absolute path to your my.jndi.properties file>");
                Properties prop = new Properties();
                FileInputStream istream = new FileInputStream(configFile);
                prop.load(istream);
                context = new InitialContext(prop);
            } catch (NamingException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

Java Single Jar file with multiple main method classes

/**
* Sample launcher
*/
public class Launcher {

    public static void main(String[] args) throws Exception {
        if (args != null && args.length > 0) {
            String option = args[0];
            String[] args2 = new String[0];

            if (args.length > 1) {
                args2 = new String[args.length - 1];
                System.arraycopy(args, 1, args2, 0, args2.length);
            }

            if (option.equals("JbossJavaTCPSender"))
                new JbossJavaTCPSender().exec(args2);
            else if (option.equals("JbossJavaTCPReceiver"))
                new JbossJavaTCPReceiver().exec(args2);
            else
                System.out.println("Connot find such class to execute having name ["
                        + option+"]");
        } else
            {
            System.out.println("Usage : Provide ClassName as an argument to execute.");
            System.out.println("\t[java -jar JbossJavaTCPExample.jar JbossJavaTCPSender]");
            System.out.println("\t[java -jar JbossJavaTCPExample.jar JbossJavaTCPReceiver]");
            }
    }
}

Pushing local repository to Github

Install Git

Download and run the latest version of GIT from http://msysgit.github.io/ or http://git-scm.com/

Then go to http://github.com/ and create an account and repository.

During the installation you can accept all of the defaults, do not change 'Use Git and optional Unix tools from the Windows Command Prompt' option for Git and SSH operations, shown below:

 


After the installation is completed, to verify that Git is correctly configured run:



git --version



If the version of Git is displayed, means Git was installed successfully and has been add to path env variable.



On your machine, first you will need to navigate to the project folder using git bash. When you get there you do:



git init


which initiates a new git repository in that directory.



When you've done that, you need to register that new repo with a remote (where you'll upload -- push -- your files to), which in this case will be github. You'll get the correct URL from your repo on GitHub.



$ git remote add origin https://github.com/[username]/[reponame].git


You need to add you existing files to your local commit:



git add .   # this adds all the files


git pull https://github.com/[username]/[reponame].git


Then you need to make an initial commit, so you do:



git commit -a -m "Initial commit" # this stages your files locally for commit. 
# they haven't actually been pushed yet


Now you've created a commit in your local repo, but not in the remote one. To put it on the remote, you do the second line you posted:



git push -u origin --all