title: Execute shell script in remote machine using Jenkins
body: <p>My requirement is like this:
  The jenkins is installed in one server(say server A).
  I have 3 script in this server for deployment of war into different server say <strong>B and C</strong>.</p>
  
  <p>There is a main script say <strong>JenkinsMainScript.sh</strong> . Inside this script I have copied the deployment script(say <strong>serverB.sh/serverC.sh</strong>)  from server A to the Corresponding server  serverB or serverC based on parameter passed when execute the shell like :</p>
  
  <p>/root/Pictures/JenkinsTesting/JenkinsBuildMain.sh B</p>
  
  <p>in the jenkins's "<strong>Execute shell</strong>" under "<strong>Build</strong>" section of <strong>jenkins</strong> of a job
  where B is the server Name to deploy.
  i.e if I again pass C as parameter in the above statement then the main script will copy serverC.sh from server A to server C and deploy the war into that server
  Note:-All the deployment steps are written inside serverC.sh for server C  and serverB.sh for Server B.</p>
  
  <p>Again My deployment is of two steps in jenkins, <strong>Job1 and Job2.</strong></p>
  
  <p>1.Create war file by taking source as git repository which is access using SSH(say server D)
  2.After Successful execution of Job1 it will trigger Job2.</p>
  
  <p>Inside Job2 I am executing the JenkinsBuildMain.sh file under <strong>Build</strong> section.</p>
  
  <p>The Problem is If I add id_rsa and id_rsa.pub file inside .ssh directory of jenkins to access git server(server D) at the same time I can't access other server like server B for deployment via SSH connection.
  Again if I add id_rsa and id_rsa.pub file inside .ssh for deployment server(server B) then Git server is unable to access at the same time.
  That means only one server is accessible using SSH connection at a time.</p>
  
  <p>Is there any way to access both server using same key or different key?</p>
  
  <p>Or can we add multiple key for ssh connection so that it can access to both server, <strong>server D</strong> for git clone and server B for copying the script file from jenkins server to <strong>server B</strong>.</p>
  
  <p>When I do this I got Bellow Message:
  <strong>"Host key verification failed.
  lost connection"</strong></p>
  
  <p>Current I am able connect one server at a time. If It connect to Git server the shell script file is not able to copy ,saying above error, and if I able set up for copying the file the git Server is unable to access saying   </p>
  
  <p><strong>Return status code 128.
  Permission denied (PublicKey,gssapi-keyex,gssapi-mic,password)....</strong></p>
  
  <p><strong>Is There any way to add multiple keys for ssh connection for different server using Jenkins?</strong></p>
  
  <p>Bellow is my script Code to copy the deploy script to remote server.</p>
  
  <p><strong><em>JenkinsMainScript.sh:</em></strong></p>
  
  <pre><code>if [ $serverId = 'B' ]
      then        
          scp $scriptSourcePath/serverB.sh $serverBPathToCopyTheFile/
          ssh -l $serverBUserName $IpAddr $serverBPathToExecuteTheFile/serverB.sh
  
  else
          echo " No Matching condition found..."
  fi
  </code></pre>
  
  <p>Here is the screen shot of Configuration in the Jenkins for job2:</p>
  
  <p><a href="https://i.stack.imgur.com/qzf9I.png" rel="nofollow noreferrer">Jenkins Config for job2</a></p>
  
  <p>Can anyone help me on this.
  Thanks in advance</p>