How to do a HTTP request from terminal

A quick tip for use in terminal:

# print string auth 
echo -n "dummyuser:MyPassword" | base64

# Unix terminal (basic auth, doing http request with GET method)

curl -X GET \
  -H "Content-Type: text/xml" \
  -H "Authorization: Basic ZHVtbXl1c2VyOk15UGFzc3dvcmQ=" \
  "http://dummyserver:5000/dir/wsdl?p=ic/6e7abd99891231jij123ndb769978566c470"


# Unix terminal (basic auth, doing http request with POST method to send payload from file)

curl -X POST \
  -H "Content-Type: application/soap+xml" \
  -H "Authorization: Basic ZHVtbXl1c2VyOk15UGFzc3dvcmQ=" \
  -d @./request.xml \
  "https://dummyserver:50001/XISOAPAdapter/MessageServlet?senderParty=&senderService=SYS_LEGACY&receiverParty=&receiverService=&interface=BookingFlight_Out&interfaceNamespace=http://LGCY/namespace"


# Unix terminal (basic auth, doing http request with POST method)

curl -X POST \
  -H "Content-Type: text/xml" \
  -H "Authorization: Basic ZHVtbXl1c2VyOk15UGFzc3dvcmQ=" \
  -d "<soapenv:Envelope>....ommited lines </soapenv:Envelope>" \
  "https://dummyserver:50001/XISOAPAdapter/MessageServlet?senderParty=&senderService=SYS_LEGACY&receiverParty=&receiverService=&interface=BookingFlight_Out&interfaceNamespace=http://LGCY/namespace"



# Windows Powershell (basic auth, doing http request with GET method)

$Username = "dummyuser"
$Password = ConvertTo-SecureString "MyPassword" -AsPlainText -Force
$Credential = New-Object System.Management.Automation.PSCredential ($Username, $Password)

Invoke-RestMethod -Uri "http://dummyserver:5000/dir/wsdl?p=ic/6e7abd99891231jij123ndb769978566c470" -Method GET -Credential $Credential


# Windows Powershell (basic auth, doing http request with POST method to send payload from file)
 
$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes("dummyuser:MyPassword"))
$headers = @{
    Authorization = "Basic $base64AuthInfo"
    "Content-Type" = "text/xml"
}

Invoke-RestMethod -Uri "https://dummyserver:50001/XISOAPAdapter/MessageServlet?senderParty=&senderService=SYS_LEGACY&receiverParty=&receiverService=&interface=BookingFlight_Out&interfaceNamespace=http://LGCY/namespace" -Method Post -Headers $headers -Body (Get-Content -Path "request.xml" -Raw)

Bye!

Jenkins, Execute Script over ssh with Pipeline

In the previous post I’ve explained how to configure the login over ssh to execute remote commands with Jenkins with a Build Step, now it’s the same but using Pipeline Step.

Avoid login prompt

To avoid logging prompt on each execution with the pipeline script is necessary copy the ssh key to remote server.

  1. Create a ssh key pair, login in the Jenkins server with the user that are executing the Jenkins as a service and execute the next commands:

    sudo su -s /bin/bash jenkins
    ssh-keygen -t -rsa
    

    Leave the options by default, a file $HOME/.ssh/id_rsa is generated, take care if you have a previous file, you will not be able to authenticate using the previous key anymore if you choose overwrite

  2. Copy the public key to remote server use only one of this commands:

    # Using pub key by default ~/.ssh/id_rsa.pub
    ssh-copy-id user@host 
     
    # Using another key 
    ssh-copy-id -i ~/.ssh/otherKey.pub user@host
    
    # if ssh-copy-id is not available, do it manually
    cat ~/.ssh/id_rsa.pub | ssh user@host"mkdir -p ~/.ssh && touch ~/.ssh/authorized_keys && chmod -R go= ~/.ssh && cat >> ~/.ssh/authorized_keys"
    
  3. Test the connection

    # Test the login with the private key 
    ssh -i ~/.ssh/id_rsa user@host 
    
    # Then connect without prompt password
    ssh user@host
    

For more detail check this post

If the authentication fails

If the above steps were followed and ssh’ing to the appropriate user is still prompting for passwords, inspect the permissions on both the local and remote user’s files, per the following command:

[user@ssh-server ~]$ ls -ld ~/{,.ssh,.ssh/authorized_keys*}
drwx------. 25 user user 4096 Aug 21 11:01 /home/user/
drwx------.  2 user user 4096 Aug 17 13:13 /home/user/.ssh
-rw-------.  1 user user  420 Aug 17 13:13 /home/user/.ssh/authorized_keys

And set the permissions as follow:

chmod 600 ~/.ssh/authorized_keys
chmod 700 ~/.ssh/

Don’t forget it, the PubkeyAuthentication yes must be enabled in the server into /etc/ssh/sshd_config

Configure the Pipeline

Create a new Pipeline project and set the next script, notice the end of the pseudo script is intentionally aligned at left, if I indent the text does not work.

pipeline {
    agent any
    stages {
        stage('execute') {
            steps {
		script {
			echo 'Using remote command over ssh'
			sh 'echo "Today is:" date'
			echo '*** Executing remote commands ***'
	 		sh '''#!/bin/bash
				date
				ssh devops@hostname << ENDSSH
				java -version
			    	date
			    	cd /tmp
			    	pwd
ENDSSH
'''

			sh '''
				date
				ssh devops@hostname << ENDSSH
				javac -version
				date
				cd /tmp
				pwd
ENDSSH
'''
                }
            }
        }
    }
}

Then save it and execute the job.

Checking the job log

The script have different ways to call a sh command, let’s check each one.

sh 'echo "Today is:" date'

The first is a simple sh command to get date, the interesting part is how Jenkins execute this, if see the job log each command is executed by separate, the echo first and date after.

[Pipeline] echo
Using remote command over ssh
[Pipeline] sh
[PL_REMOTE_COMMAND] Running shell script
+ echo Today is:
Today is:
+ date
mié dic 18 05:05:06 CST 2019

The second command is executed line by line, one per command:

echo '*** Executing local commands ***'
sh '''
	ls -l
	date
	cd /tmp
	pwd
'''

output:

[Pipeline] echo
*** Executing local commands ***
[Pipeline] sh
[PL_REMOTE_COMMAND] Running shell script
+ ls -l
total 0
+ date
mié dic 18 05:05:07 CST 2019
+ cd /tmp
+ pwd
/tmp

The next command use a pseudo script style to hide the commands and connect to remote server, note my servers have a different date time, that means the commands in the script are hidden in the log and only shows the result of each one:

echo '*** Executing remote commands ***'
    sh '''#!/bin/bash
	date
    	ssh devops@hostname << ENDSSH
	    	java -version
	    	date
	    	cd /tmp
	    	pwd
ENDSSH
'''

output:

*** Executing remote commands ***
[Pipeline] sh
[PL_REMOTE_COMMAND] Running shell script
mié dic 18 05:31:05 CST 2019
Pseudo-terminal will not be allocated because stdin is not a terminal.
java version "1.6.0_45"
Java(TM) SE Runtime Environment (build 1.6.0_45-b06)
Java HotSpot(TM) Server VM (build 20.45-b01, mixed mode)
vie dic 20 18:20:22 CST 2019
/tmp

This is a good option if you don’t want to show sensible commands in other way, remove the script header to show them.

echo '*** Executing remote commands ***'
sh '''
	date
	ssh devops@hostname << ENDSSH
    	java -version
    	date
    	cd /tmp
    	pwd
ENDSSH
'''

output:

[Pipeline] echo
[Pipeline] sh
[PL_REMOTE_COMMAND] Running shell script
+ date
mié dic 18 05:31:05 CST 2019
+ ssh devops@hostname
Pseudo-terminal will not be allocated because stdin is not a terminal.
javac 1.6.0_45
vie dic 20 18:20:22 CST 2019
/tmp

Seguir leyendo

Create ssh tunnel with Putty

This is a simple trick to use a unix server as bridge to achieve other host or services like as local service, for example, I’ve a instance of jboss running in a server but I’cant access to the server directly, if I open the url: http://remotehost:8080 the error is not found page in browser.

Then go to putty configuration ( Connection > SSH > Tunnels) and add a simple tunnel:

  • Source port: the port to access the server from your localhost
  • Destination: the hostname:port of the remote service

2019-06-17 17_10_48-PuTTY Configuration Seguir leyendo

JBoss EAP 6.2 Shell script as a service for SUSE


Just like that, explained by self  🙂

#! /bin/sh
### BEGIN INIT INFO
# Provides: JBOSS
# Required-Start:
# Required-Stop:
# Default-Start: 5
# Default-Stop:
# Description: Start Jboss Aplication Server 6.2
### END INIT INFO
export JBOSS_HOME=/usr/share/jboss/jboss-eap-6.2
start(){
echo "Starting JBoss EAP..."
# If using an SELinux system such as RHEL 4, use the command below
# instead of the "su":
# eval "runuser - jboss -c '/opt/jboss/current/bin/run.sh > /dev/null 2> /dev/null &'
# if the 'su -l ...' command fails (the -l flag is not recognized by my su cmd) try:
#Jboss EAP 6.2
su -l jboss -c '$JBOSS_HOME/bin/standalone.sh > /dev/null 2> /dev/null &'
}
stop(){
echo "Stopping JBoss..."
# If using an SELinux system such as RHEL 4, use the command below
# instead of the "su":
# eval "runuser - jboss -c '/opt/jboss/current/bin/shutdown.sh -S &'
# if the 'su -l ...' command fails try
#Jboss EAP 6.2
su -l jboss -c '$JBOSS_HOME/bin/jboss-cli.sh --connect command=:shutdown'
}
restart(){
su -l jboss -c '$JBOSS_HOME/bin/jboss-cli.sh --connect --command=:reload'
}
case "$1" in
start)
start
;;
stop)
stop
;;
restart)
restart
;;
*)
echo "Usage: jboss {start|stop|restart}"
exit 1
esac
exit 0
view raw jboss7.sh hosted with ❤ by GitHub

References

Regads

Install Java JDK on Server (Suse)

La instalación del a jdk en servidores Unix puede generar a veces algunos problemas ya que generalmente no hay una guía exacta para realizar la instalación, pero desde mi punto de vista creo que también se debe a que en internet hay muchas formas de hacerlo y también es verdad que Unix lo permite, por lo tanto no hay una forma «correcta» de hacerlo.

Sin embargo hay una forma de mantener un orden dentro de todo este caos, por ejemplo hay servidores que a veces necesitan tener instaladas dos o más versiones de la jdk en el mismo server… y ¿como controlamos esto? este tipo de cuestiones son las que causan esos problemas  a los que me refiero porque muchos ajustamos variables de entorno y terminamos creando un dessatre, y bueno antes de mostrar la instalación quería transmitir esta pequeña perspectiva.

El siguiente comando es el que nos a ayudar en Suse a controlar esto:

update-alternatives

Nota: en Ubuntu el equivalente es el comando alternatives
Seguir leyendo

Suse add Jboss 5 Startup Script

Este es uno de esos días que me arrepiento por no guardar esos buenos tips que voy descubriendo, sé que en algún momento me pueden servir y hoy es uno de esos días ¬_¬…

Para agregar un script para ejecutar el jboss al iniciar el sistema en SUSE tenemos que seguir unos sencillos pasos:

  1. Crear archivo, generas un archivo de texto llamado jboss en la carpeta /etc/init.d con el siguiente contenido:
    #! /bin/sh
    ### BEGIN INIT INFO
    # Provides: JBOSS
    # Required-Start:
    # Required-Stop:
    # Default-Start: 5
    # Default-Stop:
    # Description: Start Jboss Aplication Server 5.1 to allow and provide QA Environment the "Publicador Promociones"
    ### END INIT INFO
    export JBOSS_HOME=/opt/jboss-5.1.0.GA
    start(){
    echo "Starting jboss..."
    # If using an SELinux system such as RHEL 4, use the command below
    # instead of the "su":
    # eval "runuser - jboss -c '/opt/jboss/current/bin/run.sh > /dev/null 2> /dev/null &'
    # if the 'su -l ...' command fails (the -l flag is not recognized by my su cmd) try:
    $JBOSS_HOME/bin/run.sh -Djboss.as.deployment.ondemand=false -b 0.0.0.0 > /dev/null 2> /dev/null &
    #Su -l jboss -c '$JBOSS_HOME/bin/run.sh > /dev/null 2> /dev/null &'
    }
    stop(){
    echo "Stopping jboss..."
    # If using an SELinux system such as RHEL 4, use the command below
    # instead of the "su":
    # eval "runuser - jboss -c '/opt/jboss/current/bin/shutdown.sh -S &'
    # if the 'su -l ...' command fails try:
    $JBOSS_HOME/bin/shutdown.sh -S &
    #su -l jboss -c '$JBOSS_HOME/bin/shutdown.sh -S &'
    }
    restart(){
    stop
    # give stuff some time to stop before we restart
    sleep 60
    # protect against any services that can't stop before we restart (warning this kills all Java instances running as 'jboss' user)
    #su -l jboss -c 'killall java'
    # if the 'su -l ...' command fails try:
    # sudo -u jboss killall java
    start
    }
    case "$1" in
    start)
    start
    ;;
    stop)
    stop
    ;;
    restart)
    restart
    ;;
    *)
    echo "Usage: jboss {start|stop|restart}"
    exit 1
    esac
    exit 0
    view raw jboss hosted with ❤ by GitHub
  2. Le otorgas permisos de ejecución:
    chmod +x jboss
  3. Agregas el script al sistema:
    insserv /etc/init.d/jboss
    ln -s /etc/init.d/jboss /sbin/rcjboss

Para configurar el inicio y el fin debes editar el contenido del script en la sección BEGIN INIT INFO.

Referencias

Saludos