SSH via a Jumphost to a router

Hi

I’m new to Robot framework and hope you can help me with a a few questions.

I’m running router tests with robot framework. Normally I’ll connect directly to the router and run the tests, which works fine. But now I have to connect to the router via a jumpserver, which I can’t get to work. I can’t find the right example, perhaps some of you can help?

I need to connect to a jumpserver with a user and a keyfile.
From there I need to ssh to a router and run the tests.

Second,
Would it be possible to extend the test to loop through a list of routers and perform the tests on all of them in one test run? Perforable from a hostfile, where all the routers are listed? It dosen’t matter what format the file is, txt, .py or yaml. Just need the robot code example.

Br,
Anders

Hi Anders,

If you ssh to the jump box then once there running the ssh command on the jump box should be just like running any other command, so rather than trying to connect the SSHLibrary to the router SSHLibrary controls the jump box and the ssh session to the router is just something that happens within that jump box ssh session.

As for the second part,

I’d suggest looking at DataDriven Tests | ROBOT FRAMEWORK, that way each host would be treated as a separate test case.

I’d structure it something like this:

  • Suite Startup connects to jump box
  • Test Startup runs ssh command on jump box to connect to the router (unless you want this as a test step)
  • Using DataDriver Library and a test template the number of lines/rows in your datafile determines how many test cases there are
  • Test Teardown exits/disconnects from router’s ssh session (this way you should always be back at the jump box even if the test fails)
  • Suite Teardown logs out of the jump box

Hope that helps,

Dave.

Hi Dave

Thank you for replying.

How do I enter the password when logging into the router?

This is my test so for. I can connect to the jumphost and then ssh to the router. But are missing the password part.

*** Settings ***
Library           SSHLibrary
Suite Setup       Open Connection To JumpHost
Suite Teardown    Close All Connections

*** Variables ***
${JUMP_HOST}      jumphost-1
${ROUTER_HOST}    router-1
${USERNAME}       user-1
${PASSWORD}       pass-1

*** Keywords ***
Open Connection To JumpHost
    Open Connection    ${JUMP_HOST}
    Login With Public Key     ${USERNAME}    /path/to/keyfile/

*** Test Cases ***
Execute Router Test
    Execute Command  ssh ${ROUTER_HOST}
    ${output}=  Execute Command  show version
    Log to console    ${\n}${output}

Br,
Anders

Hi,

I got the password issue fixe. But when I’m executing the next command “show version” it is not run on the router, but on the jumphost itself.

I’m not sure if I understand your approach correctly.

Just to clarify my setup.

[Local machine running Robot Framework] —> [SSH to jumphost] —> [SSH to router] —> Executing commands and returning output / running the tests.

Isn’t there a more direct approach to connect to the remote router? SSHLibrary specifies “jumphost_index_or_alias” as a metode, but can’t find any examples using it.

Br,
Anders

I would expect it would be like the examples in Read Until and Read Until Prompt

I wasn’t aware of that, but now I see it in the doc, so if I understood correctly it looks like you need to do something like this:

*** Keywords ***
Open Connection To JumpHost
    Open Connection    ${JUMP_HOST}    alias=jumphost 	
    Login With Public Key     ${USERNAME}    /path/to/keyfile/

*** Test Cases ***
Execute Router Test
    # Execute Command  ssh ${ROUTER_HOST}
    Open Connection    ${ROUTER_HOST}    alias=router 	
    Login 	${ROUTER_USER} 	${ROUTER_PASS}    jumphost_index_or_alias=jumphost
    ${output}=  Execute Command  show version
    Log to console    ${\n}${output}

I don’t really understand how Open Connection can connect to the router without knowing about the jumphost or it’s alias, but perhaps it doesn’t even attempt the connection until login is called?

In Login there is also an example that uses proxy_cmd which looks like it might be useful to you as well.

Dave.

1 Like

Hi Dave

It works with the “jumphost_index_or_alias” - so I think I’ll go on with that approach.

Thank you very much for the help! :slight_smile:

Br,
Anders Rask

1 Like