<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Unable to execute second python interact.send command in my script in Call Control</title>
    <link>https://community.cisco.com/t5/call-control/unable-to-execute-second-python-interact-send-command-in-my/m-p/4913623#M3451</link>
    <description>&lt;P&gt;I am trying to write a python script which will do a ssh to Cisco VOS device (similar to CUCM).&lt;/P&gt;
&lt;P&gt;I&amp;nbsp; am using paramiko Library and interact command in my python script. Able to successfully SSH the device, able to execute the first command. However second command is failing.&lt;/P&gt;
&lt;P&gt;Here is sample code of my python script where is is not executing.&lt;/P&gt;
&lt;P&gt;import paramiko&lt;BR /&gt;from paramiko_expect import SSHClientInteraction&lt;/P&gt;
&lt;P&gt;sshsession = paramiko.SSHClient()&lt;BR /&gt;sshsession.set_missing_host_key_policy(paramiko.AutoAddPolicy())&lt;BR /&gt;sshsession.connect("xx.xx.xx.xxx", username="xxx", password="xxx")&lt;BR /&gt;# "display=True" is just to show you what script does in real time. While in production you can set it to False&lt;BR /&gt;interact = SSHClientInteraction(sshsession, timeout=600, display=True)&lt;BR /&gt;# program will wait till session is established and&amp;nbsp; returns admin prompt&lt;BR /&gt;interact.expect('admin:') &lt;BR /&gt;# program runs show status command&lt;BR /&gt;interact.send('show status')&lt;BR /&gt;interact.expect('admin:') &lt;/P&gt;
&lt;P&gt;/* This command is not executing*/&lt;BR /&gt;interact.send('utils service restart Engine')&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Any suggesstions on why the above command is not getting executed ?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Rgds,&lt;/P&gt;
&lt;P&gt;Anantha&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Tue, 29 Aug 2023 08:11:41 GMT</pubDate>
    <dc:creator>ananthap</dc:creator>
    <dc:date>2023-08-29T08:11:41Z</dc:date>
    <item>
      <title>Unable to execute second python interact.send command in my script</title>
      <link>https://community.cisco.com/t5/call-control/unable-to-execute-second-python-interact-send-command-in-my/m-p/4913623#M3451</link>
      <description>&lt;P&gt;I am trying to write a python script which will do a ssh to Cisco VOS device (similar to CUCM).&lt;/P&gt;
&lt;P&gt;I&amp;nbsp; am using paramiko Library and interact command in my python script. Able to successfully SSH the device, able to execute the first command. However second command is failing.&lt;/P&gt;
&lt;P&gt;Here is sample code of my python script where is is not executing.&lt;/P&gt;
&lt;P&gt;import paramiko&lt;BR /&gt;from paramiko_expect import SSHClientInteraction&lt;/P&gt;
&lt;P&gt;sshsession = paramiko.SSHClient()&lt;BR /&gt;sshsession.set_missing_host_key_policy(paramiko.AutoAddPolicy())&lt;BR /&gt;sshsession.connect("xx.xx.xx.xxx", username="xxx", password="xxx")&lt;BR /&gt;# "display=True" is just to show you what script does in real time. While in production you can set it to False&lt;BR /&gt;interact = SSHClientInteraction(sshsession, timeout=600, display=True)&lt;BR /&gt;# program will wait till session is established and&amp;nbsp; returns admin prompt&lt;BR /&gt;interact.expect('admin:') &lt;BR /&gt;# program runs show status command&lt;BR /&gt;interact.send('show status')&lt;BR /&gt;interact.expect('admin:') &lt;/P&gt;
&lt;P&gt;/* This command is not executing*/&lt;BR /&gt;interact.send('utils service restart Engine')&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Any suggesstions on why the above command is not getting executed ?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Rgds,&lt;/P&gt;
&lt;P&gt;Anantha&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 29 Aug 2023 08:11:41 GMT</pubDate>
      <guid>https://community.cisco.com/t5/call-control/unable-to-execute-second-python-interact-send-command-in-my/m-p/4913623#M3451</guid>
      <dc:creator>ananthap</dc:creator>
      <dc:date>2023-08-29T08:11:41Z</dc:date>
    </item>
    <item>
      <title>Re: Unable to execute second python interact.send command in my script</title>
      <link>https://community.cisco.com/t5/call-control/unable-to-execute-second-python-interact-send-command-in-my/m-p/4913899#M3452</link>
      <description>&lt;P&gt;Can you post the output of the CLI if you send these two commands via a plain SSH-client?&lt;/P&gt;</description>
      <pubDate>Tue, 29 Aug 2023 13:40:24 GMT</pubDate>
      <guid>https://community.cisco.com/t5/call-control/unable-to-execute-second-python-interact-send-command-in-my/m-p/4913899#M3452</guid>
      <dc:creator>Marcel Zehnder</dc:creator>
      <dc:date>2023-08-29T13:40:24Z</dc:date>
    </item>
    <item>
      <title>Re: Unable to execute second python interact.send command in my script</title>
      <link>https://community.cisco.com/t5/call-control/unable-to-execute-second-python-interact-send-command-in-my/m-p/4914051#M3453</link>
      <description>&lt;P&gt;Played a little bit with paramiko_expect and had similiar issues - maybe fallback to pure paramiko - see this quick n dirty example as a starting point:&lt;/P&gt;
&lt;LI-CODE lang="python"&gt;import paramiko
import time

sshsession = paramiko.SSHClient()
sshsession.set_missing_host_key_policy(paramiko.AutoAddPolicy())
sshsession.connect("x.x.x.x", username="xxx", password="xxx")

commands = [
    "show status",
    "utils service restart Engine"
]

for cmd in commands:
    time.sleep(2)
    stdin, stdout, stderr = sshsession.exec_command(cmd)
    while not stdout.channel.exit_status_ready():
        if stdout.channel.recv_ready():
            print(stdout.channel.recv(9999).decode())
                
sshsession.close()&lt;/LI-CODE&gt;</description>
      <pubDate>Tue, 29 Aug 2023 16:08:46 GMT</pubDate>
      <guid>https://community.cisco.com/t5/call-control/unable-to-execute-second-python-interact-send-command-in-my/m-p/4914051#M3453</guid>
      <dc:creator>Marcel Zehnder</dc:creator>
      <dc:date>2023-08-29T16:08:46Z</dc:date>
    </item>
    <item>
      <title>Re: Unable to execute second python interact.send command in my script</title>
      <link>https://community.cisco.com/t5/call-control/unable-to-execute-second-python-interact-send-command-in-my/m-p/4914054#M3454</link>
      <description>&lt;P&gt;This sample may be helpful:&amp;nbsp;&lt;A href="https://github.com/CiscoDevNet/axl-ansible-examples/blob/main/paramiko_test.py" target="_blank"&gt;https://github.com/CiscoDevNet/axl-ansible-examples/blob/main/paramiko_test.py&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;Though I'm not immediately spotting the difference, this works for me...&lt;/P&gt;</description>
      <pubDate>Tue, 29 Aug 2023 16:11:36 GMT</pubDate>
      <guid>https://community.cisco.com/t5/call-control/unable-to-execute-second-python-interact-send-command-in-my/m-p/4914054#M3454</guid>
      <dc:creator>dstaudt</dc:creator>
      <dc:date>2023-08-29T16:11:36Z</dc:date>
    </item>
    <item>
      <title>Re: Unable to execute second python interact.send command in my script</title>
      <link>https://community.cisco.com/t5/call-control/unable-to-execute-second-python-interact-send-command-in-my/m-p/4914340#M3455</link>
      <description>&lt;P&gt;Hi Marcel,&lt;/P&gt;
&lt;P&gt;Thanks for your valuable time in helping me with the query. I tried running the script you provided, however it is not fully executing.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 30 Aug 2023 06:16:45 GMT</pubDate>
      <guid>https://community.cisco.com/t5/call-control/unable-to-execute-second-python-interact-send-command-in-my/m-p/4914340#M3455</guid>
      <dc:creator>ananthap</dc:creator>
      <dc:date>2023-08-30T06:16:45Z</dc:date>
    </item>
    <item>
      <title>Re: Unable to execute second python interact.send command in my script</title>
      <link>https://community.cisco.com/t5/call-control/unable-to-execute-second-python-interact-send-command-in-my/m-p/4914342#M3456</link>
      <description>&lt;P&gt;Hi - yeah, it was a quick shot and I don't have access to a VOS instance. Looks like the session takes some time to load. As another quick hack try to wait a couple of seconds before excec the commands and maybe pause a little longer between the commands:&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;LI-CODE lang="python"&gt;import paramiko
import time

sshsession = paramiko.SSHClient()
sshsession.set_missing_host_key_policy(paramiko.AutoAddPolicy())
sshsession.connect("x.x.x.x", username="xxx", password="xxx")

# wait 10 seconds for the session to be ready
time.sleep(10)

commands = [
    "show status",
    "utils service restart Engine"
]

for cmd in commands:
    time.sleep(5)
    stdin, stdout, stderr = sshsession.exec_command(cmd)
    while not stdout.channel.exit_status_ready():
        if stdout.channel.recv_ready():
            print(stdout.channel.recv(9999).decode())
                
sshsession.close()&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 30 Aug 2023 06:24:32 GMT</pubDate>
      <guid>https://community.cisco.com/t5/call-control/unable-to-execute-second-python-interact-send-command-in-my/m-p/4914342#M3456</guid>
      <dc:creator>Marcel Zehnder</dc:creator>
      <dc:date>2023-08-30T06:24:32Z</dc:date>
    </item>
    <item>
      <title>Re: Unable to execute second python interact.send command in my script</title>
      <link>https://community.cisco.com/t5/call-control/unable-to-execute-second-python-interact-send-command-in-my/m-p/4914344#M3457</link>
      <description>&lt;P&gt;Hi Marcel,&lt;/P&gt;
&lt;P&gt;When i run through the command using my code i pasted in my initial query, here is the output i get. it just stops the admin prompt before and executing second command.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 30 Aug 2023 06:25:32 GMT</pubDate>
      <guid>https://community.cisco.com/t5/call-control/unable-to-execute-second-python-interact-send-command-in-my/m-p/4914344#M3457</guid>
      <dc:creator>ananthap</dc:creator>
      <dc:date>2023-08-30T06:25:32Z</dc:date>
    </item>
    <item>
      <title>Re: Unable to execute second python interact.send command in my script</title>
      <link>https://community.cisco.com/t5/call-control/unable-to-execute-second-python-interact-send-command-in-my/m-p/4914349#M3458</link>
      <description>&lt;P&gt;Hi Marcel,&lt;/P&gt;
&lt;P&gt;Please find the output of two commands with plain-ssh client.&lt;/P&gt;
&lt;P&gt;Rgds,&lt;/P&gt;
&lt;P&gt;Anantha&lt;/P&gt;</description>
      <pubDate>Wed, 30 Aug 2023 06:32:18 GMT</pubDate>
      <guid>https://community.cisco.com/t5/call-control/unable-to-execute-second-python-interact-send-command-in-my/m-p/4914349#M3458</guid>
      <dc:creator>ananthap</dc:creator>
      <dc:date>2023-08-30T06:32:18Z</dc:date>
    </item>
    <item>
      <title>Re: Unable to execute second python interact.send command in my script</title>
      <link>https://community.cisco.com/t5/call-control/unable-to-execute-second-python-interact-send-command-in-my/m-p/4914569#M3459</link>
      <description>&lt;P&gt;Here is a simple script I use as a base for when I need to run CLI commands in CUCM using Netmiko. When set to Generic as the device_type it doesn't know when the command is done running, so you may need to play with the delay_factor on each command to capture the output from whatever command/commands you're running. Hope it helps.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;import netmiko
from getpass import getpass
from sys import exit

username = input('Enter username: ')
password = getpass('Enter password: ')

host = 'XXX.XXX.XXX'
    
devices = {
    'device_type': 'generic',
    'host': host,
    'username': username,
    'password': password,
          }
    
conn = netmiko.ConnectHandler(**devices)

command = 'show status'
command2 = 'show network eth0'

print(f'Processing for {host}')

output = conn.send_command(command, expect_string=r'admin:', delay_factor=10)
output2 = conn.send_command(command2, expect_string=r'admin:', delay_factor=10)

conn.disconnect()

with open('Results.txt', 'a') as file:
    file.write(output)
    file.write(output2)
        
input('Complete, press any key to close')

exit(0)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 30 Aug 2023 12:44:07 GMT</pubDate>
      <guid>https://community.cisco.com/t5/call-control/unable-to-execute-second-python-interact-send-command-in-my/m-p/4914569#M3459</guid>
      <dc:creator>mradell</dc:creator>
      <dc:date>2023-08-30T12:44:07Z</dc:date>
    </item>
    <item>
      <title>Re: Unable to execute second python interact.send command in my script</title>
      <link>https://community.cisco.com/t5/call-control/unable-to-execute-second-python-interact-send-command-in-my/m-p/4914576#M3460</link>
      <description>&lt;P&gt;Nice, I think this one solves the issue - I was not aware about the generic device - Played with the linux server device type, but wasn't able to change the initial device prompt. Using the generic device type + send_command with expect_string solves this, thanks&amp;nbsp;&lt;a href="https://community.cisco.com/t5/user/viewprofilepage/user-id/834970"&gt;@mradell&lt;/a&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 30 Aug 2023 12:56:39 GMT</pubDate>
      <guid>https://community.cisco.com/t5/call-control/unable-to-execute-second-python-interact-send-command-in-my/m-p/4914576#M3460</guid>
      <dc:creator>Marcel Zehnder</dc:creator>
      <dc:date>2023-08-30T12:56:39Z</dc:date>
    </item>
    <item>
      <title>Re: Unable to execute second python interact.send command in my script</title>
      <link>https://community.cisco.com/t5/call-control/unable-to-execute-second-python-interact-send-command-in-my/m-p/4914604#M3461</link>
      <description>&lt;P&gt;&lt;a href="https://community.cisco.com/t5/user/viewprofilepage/user-id/365859"&gt;@ananthap&lt;/a&gt;&amp;nbsp;based on the reply from&amp;nbsp;&lt;a href="https://community.cisco.com/t5/user/viewprofilepage/user-id/834970"&gt;@mradell&lt;/a&gt;&amp;nbsp;, try this one:&lt;/P&gt;
&lt;LI-CODE lang="python"&gt;from netmiko import ConnectHandler
import logging
import time

logging.basicConfig(filename='netmiko.log', level=logging.DEBUG)
logger = logging.getLogger("netmiko")

device = {
        "device_type": "generic",
        "ip": "xxx.xxx.xxx.xxx",
        "username": "xxx",
        "password": "xxx"
}

with ConnectHandler(**device) as ssh:
    prompt = r"admin:.*"
    commands = [
        "show status",
        "utils service restart Engine"
    ]

    time.sleep(20)

    for cmd in commands:
        output = ssh.send_command(cmd, expect_string=prompt, read_timeout=20)
        print(f"=== {cmd} ===")
        print(output)&lt;/LI-CODE&gt;</description>
      <pubDate>Wed, 30 Aug 2023 13:19:30 GMT</pubDate>
      <guid>https://community.cisco.com/t5/call-control/unable-to-execute-second-python-interact-send-command-in-my/m-p/4914604#M3461</guid>
      <dc:creator>Marcel Zehnder</dc:creator>
      <dc:date>2023-08-30T13:19:30Z</dc:date>
    </item>
    <item>
      <title>Re: Unable to execute second python interact.send command in my script</title>
      <link>https://community.cisco.com/t5/call-control/unable-to-execute-second-python-interact-send-command-in-my/m-p/4914626#M3462</link>
      <description>&lt;P&gt;Thank you Marcel, mradell.&lt;/P&gt;
&lt;P&gt;Your suggesstions helped and saved my time in solving script issue.&lt;/P&gt;</description>
      <pubDate>Wed, 30 Aug 2023 13:38:27 GMT</pubDate>
      <guid>https://community.cisco.com/t5/call-control/unable-to-execute-second-python-interact-send-command-in-my/m-p/4914626#M3462</guid>
      <dc:creator>ananthap</dc:creator>
      <dc:date>2023-08-30T13:38:27Z</dc:date>
    </item>
  </channel>
</rss>

