Jenkins with SSH key - shell

I am trying to run a script on a remote server..I have created ssh key and tested running it from my remote server to another remote server where my script is located, after trying to run it I have succeeded. I have jenkins installed on my server which there I tried the same command in order to run the script on the remote server... it failed.
this is what I tried in my jenkins job:
ssh -i /path/to/my/private/key user#host_name /path/to/my/script/clean_cache_ruby.sh
I am getting in Jenkins the following error: Host key verification failed.
how can i make it run from Jenkins as it run perfectly from my server when I try the same command on the server terminal?

You can use the ssh option StrictHostKeyChecking option.
ssh -o StrictHostKeyChecking=no -i /path/to/my/private/key user#host_name /path/to/my/script/clean_cache_ruby.sh
Or another option is to manually add the finger print to /var/lib/jenkins/.ssh/known_hosts

Related

How to run ssh commands on a host after mounting via sshfs and avoid 2FA?

I have the following situation. I am able to ssh into a server but it requires 2FA each time I ssh into the server. Let's say that I do not have control over the server's configuration to disable/bypass 2FA. After successfully ssh'ing into the server, I am free to run commands on that server through my local terminal.
I can also run sshfs to mount a directory on my local machine with a directory on the server (this will trigger 2FA once):
$ sshfs <server>:/a/b/c /my/local/folder
I would like to be able to run a script locally from my machine which invokes commands on the SSH server. I would not like to run the script inside the server. For example, my script may do something like the following, executed locally:
$ ssh <server> <command 1>
$ ssh <server> <command 2>
...
$ ssh <server> <command X>
However, each time the script runs a ssh command, it will trigger 2FA. I would like to avoid the 2FA completely, if possible. Ideally, I would like to encounter the 2FA once per day. Is there any way that I can leverage the sshfs mount and piggyback the session to send ssh commands to the host? I am open to other extreme alternatives as well (e.g. sending key strokes to the window of my local ssh terminal)

Establish SSH Tunnel as background windows service

I would like to create a background service on Windows 11 for a persistent SSH Tunnel to a remote Linux machine using OpenSSH.
I have setup the SSH config file as such that you can run the command
ssh MyRemoteHost
and the connection will be successfully made.
However, when I try to create a service with
PS New-Service -Name "MyRemoteConnection" -BinaryPathName "C:\Windows\system32\OpenSSH\ssh.exe MyRemoteHost"
the service will be created but I canĀ“t start it. I also tried to create a .BAT file with the SSH command and run this as a service with the same result.
Any help with this would be greatly appreciated.

Jmeter OS Process sampler is not executing in bash mode

My scenario is like, Initially i need to connect to a server through ssh command. once i connected to it enter into bash mode by typing 'bash' command then need to execute few more commands.
I have used Os Process sampler to do this and executed in docker container
Issue 1:) Getting "pseudo-terminal will not be allocated because stdin is not a terminal" error while doing ssh to that particular server.
enter image description here
Issue 2:-) Failed to execute the command in bash mode
enter image description here
Can you please help me on this
If your "ssh" command assumes entering the password - you won't be able to achieve this because the password has to be supplied interactively.
If you need to execute a command (or several commmans) on the remove machine over SSH connection it's better to consider using SSH Command sampler (can be installed as a part of the SSH Protocol Support bundle using JMeter Plugins Manager
Once the plugin is installed you can use it for executing your commands over SSH channel:
More information: How to Run External Commands and Programs Locally and Remotely from JMeter

Using Jenkins to SSH into EC2 Ubuntu instance and run shell scripts

I have installed Jenkins on my local, I have created my own EC2 instance, I can ssh into my instance and run some shell scripts to shut down my Wildfly server installed on my instance.
This is what I do when I do it manually on my Mac.
open my mac terminal, type
ssh -i /Users/xxx/tools/xxxx.pem ubuntu#10.206.xxx.xx
It will login to my Instance, and then I type:
cd /srv/wildfly-10.1.0.Final/bin
sudo -s
source /etc/profile
./jboss-cli.sh --connect command=:shutdown
The screen will output
{"outcome" => "success"}
Now, I want to using Jenkins, when I click build button, it will ssh into that instance and run these shell scripts for me. The output is expected the same as I run it after I ssh into the instance.
My question is: what steps should I follow, after I login to my Jenkins local environment: localhost:8080
Create a New Item, which one? Is there some plugin I can use? Where to put my shell scripts, will it run successfully?
A guide would be helpful, thanks a lot!
Additon:
when I try to login: using my ssh command, I get this error:
Pseudo-terminal will not be allocated because stdin is not a terminal.
Host key verification failed.
Too many questions to answer in one post. but this should get you started.
ssh from jenkins to your ec2 should be password less, should you need to set the keys in jenkins. use the credential manager and create one, by pasting the private key
https://www.cloudbees.com/blog/using-ssh-jenkins
Refer remote command execution over ssh for the rest of the task.
you will find how to do this in tons.. but this should give you an idea. https://www.cyberciti.biz/faq/unix-linux-execute-command-using-ssh/
For the question on job type, at this point just go with the freestyle .. And later, you may plan for fancy stuff.
You need to add the PEM file details in place where it asks for Private Key

Jenkins on Windows get stuck when git plugin execute ssh git-upload-pack

I'm trying to setup Jenkins on Windows server running in Azure.
I've install Jenkins, git and git-plugin on Windows 2012.
I'm trying to clone a very small repository from BitBicket and in Jenkins console I can see this line:
On the server itself there is a process of ssh that is hang with this command line:
c:\CI\Git\bin\ssh.exe -i "c:\Users\jenkins\AppData\Local\Temp\ssh663898192536328409key" -o StrictHostKeyChecking=no git#bitbucket.org "git-upload-pack'mycompany/myrepo.git'"
When I try to run this command in window cmd.exe I get this response, then the process stuck waiting for input and if I type something it exit with protocol error message:
00cbe160a5e558047c6dxy2d00694874365997d14f5 HEAD multi_ack thin-pack side-band
side-band-64k ofs-delta shallow no-progress include-tag multi_ack_detailed symref=HEAD:refs/heads/master agent=git/1.8.5.2
003fe160a5e558047c6dcc37d00694874365997d14f5 refs/heads/master
0000
NOTE: When I git clone the same repository from git bash everything works great.
I didn't find the root cause but I did manage to get around it for now by generating a new key-pair WITHOUT password
For now it is enough and later I'll try to add password to the key-pair file.

Resources