What is the best method to execute script on remote via Jenkins - linux

Am looking for the best & proper method to execute script on remote via Jenkins job.
What is better:
Write the commands directly on the Jenkins "Exec command" label on the "Send files or execute commands over SSH" tab.
Just copy a bash file to the remote and execute it through Jenkins "Exec Com.." label command.
The reason I'm asking this question is because I am struggling with the second approach (i.e. execute local file which located on the remote...) and thinking maybe that not the best practice.
In addition, should I execute the script using sudo (because running as sudo causes me gitlab issues when try to clone..).
And when I am execute the the script he ask for password.
So far added permissions to /etc/sudoers not do the job but I'm thinking maybe that's a tab/lines issue...possible ?
Any suggestions ?

I personally would go for SSH protocol approach (Simple, secure and reliable). Moreover, there are some plugins already made for those use cases you've mentioned.
1. SSH plugin
You can use the SSH Plugin to run shell commands on a remote machine
via ssh.
So this plugin would fit into your first option of executing script or commands on remote via SSH.
2. Publish Over SSH Plugin
The goal of the Publish Over plugins is to provide a consistent set of
features and behaviors when sending build artifacts ... somewhere.
The Publish over SSH plugin will allow you copy files to a remote server and execute arbitrary commands on the remote server. So this fits into your second approach where you want to first copy script and then execute.
Really you can use any of those plugins (Both use SSH so it's more secure). The question is what suits you the most. Choose wisely :)

Related

How to remotely control Trace32 via Terminal

I have an automated Linux agent (CentOS) with Bamboo to aid in building and deployment of new software onto a board. I want to be able to run .cmm files via Trace32 and a Lauterbach onto my locally located board using the remote Bamboo agent to trigger daily builds and deployment.
I am aware of the terminal command option: ./trace32 -s file.cmm to avoid using the GUI.
Is there anyway I can use this kind of thing for remote deployment, perhaps utilising ssh?
There is no SSH server in TRACE32. However you can control TRACE32 remotely via the TRACE32 remote API. See the document "api_remote.pdf" in the PDF folder of your TRACE32 installation.
To launch some scripts or execute some simple commands from a bash shell, you can use the command line tool t32rem from the <t32>/bin/linux folder.
This tool uses the TRACE32 remote API to send TRACE32 commands to open TRACE32 GUIs.
Do the following:
Enable the TRACE32 remote API to add the following two lines to your TRACE32 configuration file (usually "config.t32")RCL=NETASSIST
PORT=20000 You have to ensure that there is an empty line before and after these lines in your configuration file.
Launch TRACE32 and send a command to it with t32rem like that: t32rem localhost port=20000 <command> For <command> you can choose any TRACE32 command like DO file.cmm to execute your mentioned PRACTICE script.
For more complex remote control of TRACE32 (e.g. from a Python script) you should have a look at the previously mentioned "api_remote.pdf" and the examples at <t32>/demo/api.
When you already have a command to run, how about using a Script task in your plan, so that you can execute the command you wanted?

Pass password through jenkins build step

I'm trying to add a build step in jenkins to copy files from my build server to my web application server. I've got the following command working in the command prompt
sudo scp -r /var/lib/jenkins/workspace/demoproj/publish root#0.0.0.0:/usr/temp
but when I run this command, it prompts me for a password every time. I found out about sshpass, but when I run this command...
sudo sshpass -p "passwordhere" scp -r /var/lib/jenkins/workspace/demoproj/pub root#0.0.0.0:/usr/temp
the terminal gets stuck. And never makes it through.
My main problem is if I add the first command to a build step in jenkins, it won't be able to pass the password over. How can I either supply the password in jenkins, or modify the command to pass over my credentials?
Helpful information: I'm using Putty on Windows 10 to connect to my
Ubuntu 16.04.3 LTS x64 servers from another Ubuntu 16.04.3 server.
First, sshpass needs to be installed on both the systems that is, the one running your jenkins instance as well as the one you are trying to access that is: root#0.0.0.0. You can verify it by doing 'which sshpass' or 'whereis sshpass'. If its not installed even in one of them then you need to install it first.
Also, Have you ever tried doing a ssh to the said machine: root#0.0.0.0 from the system where you have your jenkins instance? If not then there might not be an entry in the 'known-hosts' of either system. for that you can do ssh with '-o StrictHostKeyChecking=no' option to make an automatic entry in known-hosts.
Alternatively, if you dont want to enter password again and again you should work with 'keys'. Generate a unique key for both the systems and do an scp or ssh with -i option.
You should use jenkins credentials instead of using sensitive passwords directly into the scripts. Put the whole scp or ssh part inside a block which looks like: withCredentials(){}.
What's the point of having CI if you are required to be nearby to enter password every time? Install "publish over ssh" plugin, it has a step to send stuff over ssh.
https://wiki.jenkins.io/display/JENKINS/Publish+Over+SSH+Plugin
Look at "Use SSH during a build" section, you can use "send files or execute commands over SSH" build step. This shall become available after plugin installation.

puppet remote command execution without agent configuration

Which puppet module to used (How to configure), so that it can execute command in the remote machine, without using any puppet agent
for example from HOST-A, a puppet script to be executed and it takes the command and execute the command in HOST-B
what i know is the openssh keys can be used to exchange the login info, then after the command can be executed, what i'm missing is is there any module available that do this job, so that, just adding the command the same can be achieved
This is coming from the ANsible background
You maybe looking for mcollective/live management feature of puppet which allows you to run ad-hoc commands on the nodes. However you will have to install mcollective agents on all the nodes being managed any ways.
If you compare it with Ansible, puppet does not support running ad-hoc commands inherently otherwise. It does not work over ssh like ansible does. Its not agentless by nature. You need puppet agent/apply to be installed and configured.
There is no built in way of achieving this, but you can use exec to execute ssh with a command to run on another machine. The command to execute would be the same as what you would use from the shell.
you need jump out from the pit what you know about Ansible when thinking of puppet, they are different.
Puppet works for final status called idempotency, ansible is as batch ssh scripts.
The rest, #Gourav has been explained clearly.

Can Jenkins run shell script on agent-less linux host

I am looking for a Jenkins plugin that makes it execute simple shell script into a given machine.
A possible execution of this plugin would be:
Given hostname,username,password, execution folder, and script --> connect to the machine and execute the script.
Can I do it without pre-configuring the machine as Jenkins slave?
I've checked out ssh-plugin and publish-over-ssh but they don't offer doing such thing without preconfiguring the hosts.
So I couldn't find the right built-in solution for Jenkins but Python Fabric is an interesting direction

When SSHing into system, is there a way to launch netbeans from command-line like I do VIM?

VIM seems integrated to the terminal. Can I open a remote file from the command-line with netbeans? Does it have shell integration? Any further explanation on this so I can better conceptually understand it would be very appreciated.
Few options:
Using ssh -X to forward X. Then you
can start netbeans remotely.
Use sshfs so you mount the
remote filesystem locally over ssh.
You can use then local copy of
netbeans to work on remote files.
Using rsync to have a local copy
The best one - use version control
Yes, you can, providing certain conditions are met!
Using ssh, you can tunnel the display of X-Window applications, meaning you can run the application (the X-Client) on one machine, and have it display on another (the X-Server).
You'll need to launch ssh with the -X option (or -Y, but preferably -X) to allow tunnelling, and then run NetBeans from the commandline as usual:
netbeans MyFile.java
Read about X-forwarding. The link points to a very verbose and detailed howto/tutorial.

Resources