# Expect Works but Returns Error Code

**URL:** <https://forum.ansible.com/t/expect-works-but-returns-error-code/24420>\
**Category:** Ansible Project\
**Created:** [May 8, 2017, 3:29pm UTC](https://forum.ansible.com/t/expect-works-but-returns-error-code/24420 "2017-05-08T15:29:06Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Scott\_Brumley](https://avatars.discourse-cdn.com/v4/letter/s/34f0e0/32.png) [@Scott\_Brumley](https://forum.ansible.com/u/Scott_Brumley)\
**Post date:** [May 8, 2017, 3:29pm UTC](https://forum.ansible.com/t/expect-works-but-returns-error-code/24420/1 "2017-05-08T15:29:06Z")

</div>

```auto
---
- name: NSP Playbook
  hosts: nsp
  gather_facts: no
  connection: local

  tasks:
    - expect:
        command: /usr/bin/ssh -tt killeruser@{{ inventory_hostname }}
        responses:
          ^.*[p|P]assword:\s*$: "somepassword"
          ^.*\>\s*$:
            - "show malwareenginestats\r\nexit\r\n"
      register: shell

    - debug: msg="{{ shell.stdout_lines }}"

```

```auto

```

```auto
The stdout_lines shows the results from the show command but it shows as having an error with Return Code of 1. Any clues would be nice. I just need to parse the data once it's not in error.

```

```auto

```

```auto
Sincerely,

```

```auto
Scott

```

```auto

```

```auto

```

```auto
**Output:**

```

```auto
PLAY [NSP Playbook] *******************************************************************************************************************************************************

```

```auto

TASK [expect] *************************************************************************************************************************************************************

fatal: [192.168.193.6]: FAILED! => {"changed": true, "cmd": "/usr/bin/ssh -tt killeruser@192.168.193.6", "delta": "0:00:00.431237", "end": "2017-05-08 15:16:13.300577", "failed": true, "rc": 1, "start": "2017-05-08 15:16:12.869340", "stdout": "* * *\r\n\r\nAuthorized users only. Unauthorized users will be prosecuted\r\nto the full extent of the law.\r\n\r\n* * *\r\nkilleruser@192.168.193.6's password: \r\nLast login: Mon May 8 15:07:05 2017 from 192.168.195.205\r\r\nTrying 127.0.0.1...\r\nConnected to 127.0.0.1.\r\nEscape character is 'off'.\r\n\r\nHello, this is zebra (version 0.92a).\r\nCopyright 1996-2001 Kunihiro Ishiguro.\r\n\r\nintruShell@VIPS01> \r\n\r\nMALWARE STATISTICS FOR NSP Analysis(pdf) Engine:\r\n--------------------------------------------------\r\n Files Submitted: 0 \r\n Files Processed: 0 \r\n\r\n Clean Files: 0 \r\n Very High Malware Confidence Matches: 0 \r\n High Malware Confidence Matches: 0 \r\n Medium Malware Confidence Matches: 0 \r\n Low Malware Confidence Matches: 0 \r\n Very Low Malware Confidence Matches: 0 \r\n Unknown Malware Confidence Matches: 0 \r\n\r\n Alert Generated: 0 \r\n Files Blocked: 0 \r\n Connection Reset: 0 \r\n \r\nMALWARE STATISTICS FOR NSP Analysis(flash) Engine:\r\n--------------------------------------------------\r\n Files Submitted: 0 \r\n Files Processed: 0 \r\n\r\n Clean Files: 0 \r\n Very High Malware Confidence Matches: 0 \r\n High Malware Confidence Matches: 0 \r\n Medium Malware Confidence Matches: 0 \r\n Low Malware Confidence Matches: 0 \r\n Very Low Malware Confidence Matches: 0 \r\n Unknown Malware Confidence Matches: 0 \r\n\r\n Alert Generated: 0 \r\n Files Blocked: 0 \r\n Connection Reset: 0 \r\n \r\nMALWARE STATISTICS FOR NSP Analysis(office) Engine:\r\n--------------------------------------------------\r\n Files Submitted: 0 \r\n Files Processed: 0 \r\n\r\n Clean Files: 0 \r\n Very High Malware Confidence Matches: 0 \r\n High Malware Confidence Matches: 0 \r\n Medium Malware Confidence Matches: 0 \r\n Low Malware Confidence Matches: 0 \r\n Very Low Malware Confidence Matches: 0 \r\n Unknown Malware Confidence Matches: 0 \r\n\r\n Alert Generated: 0 \r\n Files Blocked: 0 \r\n Connection Reset: 0 \r\n \r\nMALWARE STATISTICS FOR Mobile Cloud ENGINE:\r\n--------------------------------------------------\r\n Files Submitted: 0 \r\n Files Processed: 0 \r\n\r\n Clean Files: 0 \r\n Very High Malware Confidence Matches: 0 \r\n High Malware Confidence Matches: 0 \r\n Medium Malware Confidence Matches: 0 \r\n Low Malware Confidence Matches: 0 \r\n Very Low Malware Confidence Matches: 0 \r\n Unknown Malware Confidence Matches: 0 \r\n\r\n Alert Generated: 0 \r\n Files Blocked: 0 \r\n Connection Reset: 0 \r\n \r\nMALWARE STATISTICS FOR Gateway Anti-Malware ENGINE:\r\n--------------------------------------------------\r\n Files Submitted: 0 \r\n Files Processed: 0 \r\n\r\n Clean Files: 0 \r\n Very High Malware Confidence Matches: 0 \r\n High Malware Confidence Matches: 0 \r\n Medium Malware Confidence Matches: 0 \r\n Low Malware Confidence Matches: 0 \r\n Very Low Malware Confidence Matches: 0 \r\n Unknown Malware Confidence Matches: 0 \r\n\r\n Alert Generated: 0 \r\n Files Blocked: 0 \r\n Connection Reset: 0 \r\n \r\nMALWARE STATISTICS FOR GTI FILE Reputation ENGINE:\r\n--------------------------------------------------\r\n Files Submitted: 0 \r\n Files Processed: 0 \r\n\r\n Clean Files: 0 \r\n Very High Malware Confidence Matches: 0 \r\n High Malware Confidence Matches: 0 \r\n Medium Malware Confidence Matches: 0 \r\n Low Malware Confidence Matches: 0 \r\n Very Low Malware Confidence Matches: 0 \r\n Unknown Malware Confidence Matches: 0 \r\n\r\n Alert Generated: 0 \r\n Files Blocked: 0 \r\n Connection Reset: 0 \r\n \r\nMALWARE STATISTICS FOR FILE SAVE ENGINE:\r\n--------------------------------------------------\r\n Files Submitted: 0 \r\n Files Processed: 0 \r\n \r\nMALWARE STATISTICS FOR BLACKLIST ENGINE:\r\n--------------------------------------------------\r\n Files Submitted: 0 \r\n Files Processed: 0 \r\n\r\n Very High Malware Confidence Matches: 0 \r\n\r\n Alert Generated: 0 \r\n Files Blocked: 0 \r\n Connection Reset: 0 \r\n \r\n\r\nMALWARE STATISTICS FOR Advanced Threat Detection ENGINE:\r\n--------------------------------------------------\r\n Files Submitted: 0 \r\n Files Processed: 0 \r\n\r\n Number of files dropped by MATD under load/error: 0 \r\n Clean Files: 0 \r\n Very High Malware Confidence Matches: 0 \r\n High Malware Confidence Matches: 0 \r\n Medium Malware Confidence Matches: 0 \r\n Low Malware Confidence Matches: 0 \r\n Very Low Malware Confidence Matches: 0 \r\n Unknown Malware Confidence Matches: 0 \r\n\r\n Alert Generated: 0 \r\n Files Blocked: 0 \r\n Connection Reset: 0 \r\n \r\nintruShell@VIPS01> \r\nshow malwareenginestatsexitConnection closed by foreign host.\r\nConnection to 192.168.193.6 closed.", "stdout_lines": ["* * *", "", "Authorized users only. Unauthorized users will be prosecuted", "to the full extent of the law.", "", "* * *", "killeruser@192.168.193.6's password: ", "Last login: Mon May 8 15:07:05 2017 from 192.168.195.205", "", "Trying 127.0.0.1...", "Connected to 127.0.0.1.", "Escape character is 'off'.", "", "Hello, this is zebra (version 0.92a).", "Copyright 1996-2001 Kunihiro Ishiguro.", "", "intruShell@VIPS01> ", "", "MALWARE STATISTICS FOR NSP Analysis(pdf) Engine:", "--------------------------------------------------", " Files Submitted: 0 ", " Files Processed: 0 ", "", " Clean Files: 0 ", " Very High Malware Confidence Matches: 0 ", " High Malware Confidence Matches: 0 ", " Medium Malware Confidence Matches: 0 ", " Low Malware Confidence Matches: 0 ", " Very Low Malware Confidence Matches: 0 ", " Unknown Malware Confidence Matches: 0 ", "", " Alert Generated: 0 ", " Files Blocked: 0 ", " Connection Reset: 0 ", " ", "MALWARE STATISTICS FOR NSP Analysis(flash) Engine:", "--------------------------------------------------", " Files Submitted: 0 ", " Files Processed: 0 ", "", " Clean Files: 0 ", " Very High Malware Confidence Matches: 0 ", " High Malware Confidence Matches: 0 ", " Medium Malware Confidence Matches: 0 ", " Low Malware Confidence Matches: 0 ", " Very Low Malware Confidence Matches: 0 ", " Unknown Malware Confidence Matches: 0 ", "", " Alert Generated: 0 ", " Files Blocked: 0 ", " Connection Reset: 0 ", " ", "MALWARE STATISTICS FOR NSP Analysis(office) Engine:", "--------------------------------------------------", " Files Submitted: 0 ", " Files Processed: 0 ", "", " Clean Files: 0 ", " Very High Malware Confidence Matches: 0 ", " High Malware Confidence Matches: 0 ", " Medium Malware Confidence Matches: 0 ", " Low Malware Confidence Matches: 0 ", " Very Low Malware Confidence Matches: 0 ", " Unknown Malware Confidence Matches: 0 ", "", " Alert Generated: 0 ", " Files Blocked: 0 ", " Connection Reset: 0 ", " ", "MALWARE STATISTICS FOR Mobile Cloud ENGINE:", "--------------------------------------------------", " Files Submitted: 0 ", " Files Processed: 0 ", "", " Clean Files: 0 ", " Very High Malware Confidence Matches: 0 ", " High Malware Confidence Matches: 0 ", " Medium Malware Confidence Matches: 0 ", " Low Malware Confidence Matches: 0 ", " Very Low Malware Confidence Matches: 0 ", " Unknown Malware Confidence Matches: 0 ", "", " Alert Generated: 0 ", " Files Blocked: 0 ", " Connection Reset: 0 ", " ", "MALWARE STATISTICS FOR Gateway Anti-Malware ENGINE:", "--------------------------------------------------", " Files Submitted: 0 ", " Files Processed: 0 ", "", " Clean Files: 0 ", " Very High Malware Confidence Matches: 0 ", " High Malware Confidence Matches: 0 ", " Medium Malware Confidence Matches: 0 ", " Low Malware Confidence Matches: 0 ", " Very Low Malware Confidence Matches: 0 ", " Unknown Malware Confidence Matches: 0 ", "", " Alert Generated: 0 ", " Files Blocked: 0 ", " Connection Reset: 0 ", " ", "MALWARE STATISTICS FOR GTI FILE Reputation ENGINE:", "--------------------------------------------------", " Files Submitted: 0 ", " Files Processed: 0 ", "", " Clean Files: 0 ", " Very High Malware Confidence Matches: 0 ", " High Malware Confidence Matches: 0 ", " Medium Malware Confidence Matches: 0 ", " Low Malware Confidence Matches: 0 ", " Very Low Malware Confidence Matches: 0 ", " Unknown Malware Confidence Matches: 0 ", "", " Alert Generated: 0 ", " Files Blocked: 0 ", " Connection Reset: 0 ", " ", "MALWARE STATISTICS FOR FILE SAVE ENGINE:", "--------------------------------------------------", " Files Submitted: 0 ", " Files Processed: 0 ", " ", "MALWARE STATISTICS FOR BLACKLIST ENGINE:", "--------------------------------------------------", " Files Submitted: 0 ", " Files Processed: 0 ", "", " Very High Malware Confidence Matches: 0 ", "", " Alert Generated: 0 ", " Files Blocked: 0 ", " Connection Reset: 0 ", " ", "", "MALWARE STATISTICS FOR Advanced Threat Detection ENGINE:", "--------------------------------------------------", " Files Submitted: 0 ", " Files Processed: 0 ", "", " Number of files dropped by MATD under load/error: 0 ", " Clean Files: 0 ", " Very High Malware Confidence Matches: 0 ", " High Malware Confidence Matches: 0 ", " Medium Malware Confidence Matches: 0 ", " Low Malware Confidence Matches: 0 ", " Very Low Malware Confidence Matches: 0 ", " Unknown Malware Confidence Matches: 0 ", "", " Alert Generated: 0 ", " Files Blocked: 0 ", " Connection Reset: 0 ", " ", "intruShell@VIPS01> ", "show malwareenginestatsexitConnection closed by foreign host.", "Connection to 192.168.193.6 closed."]}

       to retry, use: --limit @/vagrant/ansible/nsp.retry

PLAY RECAP ****************************************************************************************************************************************************************

192.168.193.6 : ok=0 changed=0 unreachable=0 failed=1 

```

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [May 9, 2017, 4:57pm UTC](https://forum.ansible.com/t/expect-works-but-returns-error-code/24420/2 "2017-05-09T16:57:51Z")

</div>

This might be because you are sending two commands at once.

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;^.\*\\>\s\*$:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- "show malwareenginestats\r\nexit\r\n"  
should be

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;^.\*\\>\s\*$:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- show malwareenginestats  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- exit

With this code expect will execute "show malwareenginestats" the first time it sees "^.\*\\>\s\*$" and execute "exit" the second time it sees "^.\*\\>\s\*$".
