<?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 Re: Python netmiko interactive session on Cisco IOS in Controllers</title>
    <link>https://community.cisco.com/t5/controllers/python-netmiko-interactive-session-on-cisco-ios/m-p/4056484#M2094</link>
    <description>&lt;P&gt;Thanks but this is not what I looking for.&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;In this code, Cisco command needs to be defined.&lt;/P&gt;&lt;PRE&gt;'show run | i hostname'&lt;/PRE&gt;&lt;P&gt;What I wanted to achieve is to run arbitrary command in Cisco devices as shown in&amp;nbsp;msdaniluk's code.&lt;/P&gt;</description>
    <pubDate>Wed, 01 Apr 2020 06:59:47 GMT</pubDate>
    <dc:creator>fc00::/7</dc:creator>
    <dc:date>2020-04-01T06:59:47Z</dc:date>
    <item>
      <title>Python netmiko interactive session on Cisco IOS</title>
      <link>https://community.cisco.com/t5/controllers/python-netmiko-interactive-session-on-cisco-ios/m-p/4054907#M2090</link>
      <description>&lt;P&gt;This is simple Python code to get `show hostname` output in Cisco IOS.&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;script.py&lt;/STRONG&gt;&lt;/P&gt;&lt;PRE&gt;from netmiko import ConnectHandler

cisco_881 = {
'device_type': 'cisco_ios',
'host': 'host',
'username': 'u',
'password': 'p'
}

net_connect = ConnectHandler(**cisco_881)

output = net_connect.send_command('show hostname')
print(output)&lt;/PRE&gt;&lt;P&gt;&lt;STRONG&gt;Output&lt;/STRONG&gt;&lt;/P&gt;&lt;PRE&gt;[user@Linux ~]$ python script.py
Hostname is Cisco_01

[user@Linux ~]$&lt;/PRE&gt;&lt;P&gt;Instead of defining the Cisco command in the code, would it be possible to get interactive mode so that I can type anything on the Cisco IOS Shell?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;Desired Ouput&lt;/STRONG&gt;&lt;/P&gt;&lt;PRE&gt;[user@Linux ~]$ python script.py

Cisco_01 #
Cisco_01 # show hostname
Hostname is Cisco_01
Cisco_01 # exit
Connection to Cisco_01 closed.

[user@Linux ~]$&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 30 Mar 2020 09:14:29 GMT</pubDate>
      <guid>https://community.cisco.com/t5/controllers/python-netmiko-interactive-session-on-cisco-ios/m-p/4054907#M2090</guid>
      <dc:creator>fc00::/7</dc:creator>
      <dc:date>2020-03-30T09:14:29Z</dc:date>
    </item>
    <item>
      <title>Re: Python netmiko interactive session on Cisco IOS</title>
      <link>https://community.cisco.com/t5/controllers/python-netmiko-interactive-session-on-cisco-ios/m-p/4054978#M2091</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;Try this:&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;SPAN&gt;from &lt;/SPAN&gt;netmiko &lt;SPAN&gt;import &lt;/SPAN&gt;ConnectHandler&lt;BR /&gt;&lt;BR /&gt;device = {&lt;BR /&gt;&lt;SPAN&gt;'device_type'&lt;/SPAN&gt;: &lt;SPAN&gt;'cisco_nxos'&lt;/SPAN&gt;&lt;SPAN&gt;,&lt;BR /&gt;&lt;/SPAN&gt;&lt;SPAN&gt;'host'&lt;/SPAN&gt;: &lt;SPAN&gt;'ip'&lt;/SPAN&gt;&lt;SPAN&gt;,&lt;BR /&gt;&lt;/SPAN&gt;&lt;SPAN&gt;'username'&lt;/SPAN&gt;: &lt;SPAN&gt;'u'&lt;/SPAN&gt;&lt;SPAN&gt;,&lt;BR /&gt;&lt;/SPAN&gt;&lt;SPAN&gt;'password'&lt;/SPAN&gt;: &lt;SPAN&gt;'p'&lt;BR /&gt;&lt;/SPAN&gt;}&lt;BR /&gt;&lt;BR /&gt;net_connect = ConnectHandler(**device)&lt;BR /&gt;&lt;SPAN&gt;while True&lt;/SPAN&gt;:&lt;BR /&gt;    command = &lt;SPAN&gt;input&lt;/SPAN&gt;(&lt;SPAN&gt;"Router# "&lt;/SPAN&gt;)&lt;BR /&gt;    &lt;SPAN&gt;if &lt;/SPAN&gt;command == &lt;SPAN&gt;"exit"&lt;/SPAN&gt;:&lt;BR /&gt;        net_connect.disconnect()&lt;BR /&gt;        &lt;SPAN&gt;print&lt;/SPAN&gt;("&lt;SPAN&gt;Disconnected!"&lt;/SPAN&gt;)&lt;BR /&gt;        &lt;SPAN&gt;break&lt;BR /&gt;&lt;/SPAN&gt;    output = net_connect.send_command(command)&lt;BR /&gt;    &lt;SPAN&gt;print&lt;/SPAN&gt;(output)&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;Result:&lt;/P&gt;&lt;PRE&gt;Router# show hostname
N9K-1

Router# show version
Cisco Nexus Operating System (NX-OS) Software
...&lt;BR /&gt;Router# exit&lt;BR /&gt;Disconnected!&lt;/PRE&gt;&lt;P&gt;Regards,&lt;/P&gt;&lt;P&gt;Sergiu&lt;/P&gt;</description>
      <pubDate>Tue, 31 Mar 2020 05:17:46 GMT</pubDate>
      <guid>https://community.cisco.com/t5/controllers/python-netmiko-interactive-session-on-cisco-ios/m-p/4054978#M2091</guid>
      <dc:creator>Sergiu.Daniluk</dc:creator>
      <dc:date>2020-03-31T05:17:46Z</dc:date>
    </item>
    <item>
      <title>Re: Python netmiko interactive session on Cisco IOS</title>
      <link>https://community.cisco.com/t5/controllers/python-netmiko-interactive-session-on-cisco-ios/m-p/4055076#M2092</link>
      <description>&lt;P&gt;Although there are better ways to write this code .. you can try this ..&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;#!/usr/bin/env python
from netmiko import ConnectHandler

cisco_881 = {
'device_type': 'cisco_ios',
'host': '172.16.100.17',
'username': 'cisco',
'password': 'cisco123'
}

net_connect = ConnectHandler(**cisco_881)
output = net_connect.send_command('',expect_string=r'#',strip_command=False, strip_prompt=False)
output += net_connect.send_command('\n',expect_string=r'#',strip_command=False, strip_prompt=False)
output += net_connect.send_command('show run | i hostname',expect_string=r'#',strip_command=False, strip_prompt=False)
net_connect.disconnect()
print(output)&lt;/PRE&gt;&lt;P&gt;Your desired output .. does it have to match? Instead of exit command you can use&amp;nbsp;&lt;STRONG&gt;net_connect.disconnect() &lt;/STRONG&gt;to close the session.&lt;/P&gt;&lt;PRE&gt;omz@mac ios % python script.py

Switch#
Switch#show run | i hostname
hostname Switch
Switch#
omz@mac ios %&lt;/PRE&gt;&lt;P&gt;HTH&lt;/P&gt;</description>
      <pubDate>Mon, 30 Mar 2020 13:27:42 GMT</pubDate>
      <guid>https://community.cisco.com/t5/controllers/python-netmiko-interactive-session-on-cisco-ios/m-p/4055076#M2092</guid>
      <dc:creator>omz</dc:creator>
      <dc:date>2020-03-30T13:27:42Z</dc:date>
    </item>
    <item>
      <title>Re: Python netmiko interactive session on Cisco IOS</title>
      <link>https://community.cisco.com/t5/controllers/python-netmiko-interactive-session-on-cisco-ios/m-p/4056482#M2093</link>
      <description>&lt;P&gt;It's simpler that I thought .. many thanks man. I can't believe simple while loop can do this.&lt;/P&gt;</description>
      <pubDate>Wed, 01 Apr 2020 06:57:22 GMT</pubDate>
      <guid>https://community.cisco.com/t5/controllers/python-netmiko-interactive-session-on-cisco-ios/m-p/4056482#M2093</guid>
      <dc:creator>fc00::/7</dc:creator>
      <dc:date>2020-04-01T06:57:22Z</dc:date>
    </item>
    <item>
      <title>Re: Python netmiko interactive session on Cisco IOS</title>
      <link>https://community.cisco.com/t5/controllers/python-netmiko-interactive-session-on-cisco-ios/m-p/4056484#M2094</link>
      <description>&lt;P&gt;Thanks but this is not what I looking for.&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;In this code, Cisco command needs to be defined.&lt;/P&gt;&lt;PRE&gt;'show run | i hostname'&lt;/PRE&gt;&lt;P&gt;What I wanted to achieve is to run arbitrary command in Cisco devices as shown in&amp;nbsp;msdaniluk's code.&lt;/P&gt;</description>
      <pubDate>Wed, 01 Apr 2020 06:59:47 GMT</pubDate>
      <guid>https://community.cisco.com/t5/controllers/python-netmiko-interactive-session-on-cisco-ios/m-p/4056484#M2094</guid>
      <dc:creator>fc00::/7</dc:creator>
      <dc:date>2020-04-01T06:59:47Z</dc:date>
    </item>
    <item>
      <title>Re: Python netmiko interactive session on Cisco IOS</title>
      <link>https://community.cisco.com/t5/controllers/python-netmiko-interactive-session-on-cisco-ios/m-p/4056540#M2095</link>
      <description>&lt;P&gt;ahh&amp;nbsp; .. I didn't read the requirement properly .. my bad &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;Instead of defining the Cisco command in the code, would it be possible to get interactive mode so that I can type anything on the Cisco IOS Shell?&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;glad you got the answer&lt;/P&gt;</description>
      <pubDate>Wed, 01 Apr 2020 08:06:31 GMT</pubDate>
      <guid>https://community.cisco.com/t5/controllers/python-netmiko-interactive-session-on-cisco-ios/m-p/4056540#M2095</guid>
      <dc:creator>omz</dc:creator>
      <dc:date>2020-04-01T08:06:31Z</dc:date>
    </item>
    <item>
      <title>Re: Python netmiko interactive session on Cisco IOS</title>
      <link>https://community.cisco.com/t5/controllers/python-netmiko-interactive-session-on-cisco-ios/m-p/4130580#M2096</link>
      <description>&lt;P&gt;I don't think that is a genuine way......&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;it is disconnecting session rather than actually sending exit command.&lt;/P&gt;</description>
      <pubDate>Wed, 05 Aug 2020 06:11:04 GMT</pubDate>
      <guid>https://community.cisco.com/t5/controllers/python-netmiko-interactive-session-on-cisco-ios/m-p/4130580#M2096</guid>
      <dc:creator>a4ter</dc:creator>
      <dc:date>2020-08-05T06:11:04Z</dc:date>
    </item>
  </channel>
</rss>

