Answers: <p>For executing the command in the Jenkins Freestyle Job.</p>
  
  <pre><code>#!/bin/bash
  
  du -sh /bbhome/shared/data/repositories/* |sort -h |tail -20 |
  while IFS= read -r line;do
          DIR=`echo $line | awk '{print$2}'`
          Rep=`cat $DIR/repository-config |grep 'project\|repo' |  tr '\n' ' '`
          Size=`echo $line | awk '{print $1}' `
          echo $Size $Rep
  done
  </code></pre>
  
  <p>For ssh</p>
  
  <pre><code>ssh -o StrictHostKeyChecking=no user@host '
     command_1 to execute
     command_2 to execute
     ./script.sh to execute
     command_3 to execute
  '
  </code></pre>
  
Answers: CC BY-SA 4.0
Answers: 2019-12-18T09:18:33.587Z
Answers: Is there an option with the 'Jenkins Freestyle Job' to add ssh to a remote machine and then run the logic?
Answers: arielma
Answers: CC BY-SA 4.0
Answers: 2019-12-20T04:42:50.643Z
Answers: Let me try to understand the question you are talking about.
  You want to install ssh into some instance and then execute commands?
Answers: Manish_
Answers: CC BY-SA 4.0
Answers: 2019-12-18T08:44:02.890Z
Answers: 2019-12-18T08:44:02.890Z
Answers: Manish_
Answers: <p>You can specify the script in the "execute shell" step of the build. <em>Where</em> it gets executed will depend on what labels the job needs, and which nodes those labels are associated with.</p>
  
  <p>It sounds like you want to execute this script on an agent. As long as the agent is available, you can request it via a label in the job configuration. It's a bit difficult to determine this without having access to your setup, but if <em>e.g.</em> you have a set of agents on Debian boxes, you could attach the label <code>debian</code> to them, and specify that the job should run on <code>debian</code> nodes in the job configuration. </p>
  
  <p>If on the other hand it should be run on <code>master</code> (where Jenkins itself is running), you can specify the label <code>master</code>.</p>
  
  <p>You thus need to put your script into the "Execute Shell" box that Jenkins provides you, perhaps declaring what shell and options you want to use:</p>
  
  <pre><code>/bin/bash -xe
  du -sh /bbhome/shared/data/repositories/* |sort -h |tail -20 |
  while IFS= read -r line;do
          DIR=`echo $line | awk '{print$2}'`
          Rep=`cat $DIR/repository-config |grep 'project\|repo' |  tr '\n' ' '`
          Size=`echo $line | awk '{print $1}' `
          echo $Size $Rep
  done
  </code></pre>
  
  <blockquote>
    <p>I need also to add an ssh command to the environment. This should be executed using passwordless authentication.</p>
  </blockquote>
  
  <p>For this to happen, you need ssh keys to be exchanged with the host onto which you want to ssh into.</p>
  
Answers: CC BY-SA 4.0
Answers: 2019-12-18T05:12:39.417Z
Answers: do you mind put my syntax example on top of my do I'll understand how it should look?
Answers: arielma
Answers: CC BY-SA 4.0
Answers: 2019-12-18T07:45:54.143Z
Answers: I've added your script verbatim. Does this help you answer the question?
Answers: Bruce Becker
Answers: CC BY-SA 4.0
Answers: 2019-12-18T09:07:44.993Z
Answers: and I want to add ssh to some remote machine? for example: ssh -o StrictHostKeyChecking=no -n {user}@{bitbucket_server}
Answers: arielma
Answers: CC BY-SA 4.0
Answers: 2019-12-17T18:15:11.787Z
Answers: 2019-12-18T07:45:13.100Z
Answers: 2019-12-18T07:45:13.100Z
Answers: Bruce Becker
Body: <p>I have the below shell script:</p>
  
  <pre><code>du -sh /bbhome/shared/data/repositories/* |sort -h |tail -20 |
  while IFS= read -r line;do
          DIR=`echo $line | awk '{print$2}'`
          Rep=`cat $DIR/repository-config |grep 'project\|repo' |  tr '\n' ' '`
          Size=`echo $line | awk '{print $1}' `
          echo $Size $Rep
  done
  </code></pre>
  
  <p>How can I run it thought Execute shell step in a Jenkins freestyle job? 
  <a href="https://i.sstatic.net/OcGHZ.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/OcGHZ.png" alt="Execute shell"></a></p>
  
  <p>I need also to add an ssh command to the environment. This should be executed using passwordless authentication.</p>
  
  <p>Note: I don't want to connect to the environmnet and run this shell, but want to execute it directly from the box.</p>
  
Comments: CC BY-SA 4.0
Comments: 2019-12-17T17:36:30.663Z
Comments: The screenshot seems to show that you are using a freestyle job, not a pipeline job. Is this the case?
Comments: Bruce Becker
Comments: CC BY-SA 4.0
Comments: 2019-12-17T17:59:48.577Z
Comments: You are correct
Comments: arielma
Comments: CC BY-SA 4.0
Comments: 2019-12-17T18:08:37.817Z
Comments: I'm a bit confused by your note regarding SSH. Are you saying that you want the script to be executed over SSH on an agent?
Comments: Bruce Becker
Comments: CC BY-SA 4.0
Comments: 2019-12-17T18:37:00.883Z
Comments: Yes. On bitbucket remote server
Comments: arielma
Tags: <jenkins-pipeline><shell-script>
Title: How to add shell script to jenkins freestyle job
UserDisplayName: arielma
ThreadText: # How to add shell script to jenkins freestyle job
  
  (Asked by: username_0 on 2019-12-17)
  
  I have the below shell script:
  
  ```
  du -sh /bbhome/shared/data/repositories/* |sort -h |tail -20 |
  while IFS= read -r line;do
          DIR=`echo $line | awk '{print$2}'`
          Rep=`cat $DIR/repository-config |grep 'project\|repo' |  tr '\n' ' '`
          Size=`echo $line | awk '{print $1}' `
          echo $Size $Rep
  done
  
  ```
  
  How can I run it thought Execute shell step in a Jenkins freestyle job?
  [![Execute shell](https://i.sstatic.net/OcGHZ.png)](https://i.sstatic.net/OcGHZ.png)
  
  I need also to add an ssh command to the environment. This should be executed using passwordless authentication.
  
  Note: I don't want to connect to the environmnet and run this shell, but want to execute it directly from the box.
  
  **Tags:** jenkins-pipeline, shell-script
  
  > **Comments:**
  > **username_1**: The screenshot seems to show that you are using a freestyle job, not a pipeline job. Is this the case?  
  > **username_0**: You are correct  
  > **username_1**: I'm a bit confused by your note regarding SSH. Are you saying that you want the script to be executed over SSH on an agent?  
  > **username_0**: Yes. On bitbucket remote server  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_2 on 2019-12-18. Score: 1 | Accepted answer
  
  For executing the command in the Jenkins Freestyle Job.
  
  ```
  #!/bin/bash
  
  du -sh /bbhome/shared/data/repositories/* |sort -h |tail -20 |
  while IFS= read -r line;do
          DIR=`echo $line | awk '{print$2}'`
          Rep=`cat $DIR/repository-config |grep 'project\|repo' |  tr '\n' ' '`
          Size=`echo $line | awk '{print $1}' `
          echo $Size $Rep
  done
  
  ```
  
  For ssh
  
  ```
  ssh -o StrictHostKeyChecking=no user@host '
     command_1 to execute
     command_2 to execute
     ./script.sh to execute
     command_3 to execute
  '
  
  ```
  
  > **Comments:**
  > **username_0**: Is there an option with the 'Jenkins Freestyle Job' to add ssh to a remote machine and then run the logic?  
  > **username_2**: Let me try to understand the question you are talking about. You want to install ssh into some instance and then execute commands?  
  
  ---
  
  ### Answer by username_1 on 2019-12-17. Score: 1
  
  You can specify the script in the "execute shell" step of the build. *Where* it gets executed will depend on what labels the job needs, and which nodes those labels are associated with.
  
  It sounds like you want to execute this script on an agent. As long as the agent is available, you can request it via a label in the job configuration. It's a bit difficult to determine this without having access to your setup, but if *e.g.* you have a set of agents on Debian boxes, you could attach the label `debian` to them, and specify that the job should run on `debian` nodes in the job configuration.
  
  If on the other hand it should be run on `master` (where Jenkins itself is running), you can specify the label `master`.
  
  You thus need to put your script into the "Execute Shell" box that Jenkins provides you, perhaps declaring what shell and options you want to use:
  
  ```
  /bin/bash -xe
  du -sh /bbhome/shared/data/repositories/* |sort -h |tail -20 |
  while IFS= read -r line;do
          DIR=`echo $line | awk '{print$2}'`
          Rep=`cat $DIR/repository-config |grep 'project\|repo' |  tr '\n' ' '`
          Size=`echo $line | awk '{print $1}' `
          echo $Size $Rep
  done
  
  ```
  
  > I need also to add an ssh command to the environment. This should be executed using passwordless authentication.
  
  For this to happen, you need ssh keys to be exchanged with the host onto which you want to ssh into.
  
  > **Comments:**
  > **username_0**: do you mind put my syntax example on top of my do I'll understand how it should look?  
  > **username_1**: I've added your script verbatim. Does this help you answer the question?  
  > **username_0**: and I want to add ssh to some remote machine? for example: ssh -o StrictHostKeyChecking=no -n {user}@{bitbucket_server}  
  
  ---
  
  