# Use telnet connection to remote host

**URL:** <https://forum.ansible.com/t/use-telnet-connection-to-remote-host/4681>\
**Category:** Get Help\
**Tags:** playbook, collections, network\
**Created:** [April 2, 2024, 3:33pm UTC](https://forum.ansible.com/t/use-telnet-connection-to-remote-host/4681 "2024-04-02T15:33:12Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Santos](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/santos/32/2759_2.png) [@Santos](https://forum.ansible.com/u/Santos)\
**Post date:** [April 2, 2024, 3:33pm UTC](https://forum.ansible.com/t/use-telnet-connection-to-remote-host/4681/1 "2024-04-02T15:33:12Z")

</div>

I am new to ansible and I am trying to estrablish a telnet connection from a macine to a GNS3 switch (does not support ssh) that is running in a different machine on port 5003 using a ansible playbook. When i type the corresponding telnet command in the terminal it works as expected but when I try to use a playbook using the  
[ansible.netcommon.telnet module](https://docs.ansible.com/ansible/latest/collections/ansible/netcommon/telnet_module.html) it doesn’t work. I wanted to run commands and save the output from each one.

My hosts file is very simple and looks like this (in the future would contain more devices):

```auto
[switch]
sw1 ansible_hosts=192.168.56.176 ansible_port=5003

```

The project configuration is:

```auto
[defaults]
inventory=/home/ar/Diss_Network/ansible/tests/hosts
host_key_checking=False

[inventory]
enable_plugins=host_list, script, auto, yaml, ini, toml

```

and the ansible playbook (switch doesn’t have neither username nor password ):

```auto
---
- hosts: sw1
  tasks:
  - name: run show command
    ansible.netcommon.telnet:
      prompts:
      - '[>#]'
      command:
      - show version

```

This playbook returns:

```auto
fatal: [sw1]: UNREACHABLE! => {"changed": false, "msg": "Failed to connect to the host via ssh: kex_exchange_identification: banner line contains invalid characters\r\nbanner exchange: Connection to 192.168.56.176 port 5003: invalid format", "unreachable": true}

```

I’m having trouble understanding the error because telnet typically doesn’t involve key management. Can telnet effectively establish communication with remote hosts? Is there another ways to establish a telnet connection?

---

<div class="post-metadata">

**Author:** ![Denney-tech](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/denney-tech/32/1556_2.png) [@Denney-tech](https://forum.ansible.com/u/Denney-tech)\
**Post date:** [April 2, 2024, 5:40pm UTC](https://forum.ansible.com/t/use-telnet-connection-to-remote-host/4681/2 "2024-04-02T17:40:29Z")

</div>

Add `gather_facts: false`, so ansible doesn’t try to ssh and gather facts before running the tasks. The telnet module isn’t a connection plugin either, so it isn’t going to respect your `ansible_port` variable if that’s the intended telnet port. It _does_ appear to use the remote\_user/addr though. You might have to try setting user to null or an empty string `""`.

```yaml
- hosts: sw1
  gather_facts: false
  tasks:
  - name: run show command
    ansible.netcommon.telnet:
      port: "{{ ansible_port | default(omit) }}"
      prompts:
      - '[>#]'
      command:
      - show version
      #user: null 

```

---

<div class="post-metadata">

**Author:** ![mariolenz](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/mariolenz/32/108_2.png) [@mariolenz](https://forum.ansible.com/u/mariolenz)\
**Post date:** [April 2, 2024, 5:46pm UTC](https://forum.ansible.com/t/use-telnet-connection-to-remote-host/4681/3 "2024-04-02T17:46:41Z")

</div>

Maybe this should be documented, I don’t think it already is 😉

---

<div class="post-metadata">

**Author:** ![Denney-tech](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/denney-tech/32/1556_2.png) [@Denney-tech](https://forum.ansible.com/u/Denney-tech)\
**Post date:** [April 2, 2024, 6:25pm UTC](https://forum.ansible.com/t/use-telnet-connection-to-remote-host/4681/4 "2024-04-02T18:25:10Z")

</div>

I mean, it’s listed as a module, not a connection type, so that part is sort of documented.

Also, in the synopsis:

> - Executes a low-down and dirty telnet command, not going through the module subsystem.
> - This is mostly to be used for enabling ssh on devices that only have telnet enabled by default.

---

<div class="post-metadata">

**Author:** ![Santos](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/santos/32/2759_2.png) [@Santos](https://forum.ansible.com/u/Santos)\
**Post date:** [April 2, 2024, 8:53pm UTC](https://forum.ansible.com/t/use-telnet-connection-to-remote-host/4681/5 "2024-04-02T20:53:38Z")

</div>

Your modifications solved the error, but the playbook execution hangs. I tried to create a netcat server in the destination host with

```auto
nc -l 5050

```

and I successfully tested connecting to it using a telnet command, establishing bidirectional communication. However, when I ran the Ansible playbook, no action occurred. I attempted to utilize the “send\_newline: true” parameter, which sent a “\n” to the netcat server but nothing happened after that.

---

<div class="post-metadata">

**Author:** ![Denney-tech](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/denney-tech/32/1556_2.png) [@Denney-tech](https://forum.ansible.com/u/Denney-tech)\
**Post date:** [April 2, 2024, 9:13pm UTC](https://forum.ansible.com/t/use-telnet-connection-to-remote-host/4681/6 "2024-04-02T21:13:15Z")

</div>

Unfortunately, I have no experience with this module, and barely any experience with network automation via Ansible. I can only help with the Ansible-centric issues here.

If I were in your shoes, I would try increasing verbosity to max to see if anything stands out in what Ansible is doing. I don’t know anything about your netcat server, so Idk what “prompt” should be expected, but I don’t think Ansible will send any commands until it sees the prompt and that might be why it’s hanging.
