Evil_TTL> show | s

Execute SINGLE command with Python

Category:Automation -> Python

Before reading further, make yourself familiar with How to Install Python

Below is updated script from article SSH with Python to automatically execute ‘sh ver’ and print the results:

import paramiko 

host 
'192.168.1.66'
user 'cisco'
secret 'cisco'
port 22

ssh 
paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) #Set policy to use when connecting to servers without a known host key
ssh.connect(hostname=hostusername=userpassword=secretport=port)
stdinstdoutstderr ssh.exec_command('sh ver')
output stdout.readlines()
print (
''.join(output)) 

Use IDLE shell to execute the script for convenience.

Result:

Cisco IOS Software2800 Software (C2800NM-ADVIPSERVICESK9-M), Version 12.4(15)T14RELEASE SOFTWARE (fc2)
Technical Supporthttp://www.cisco.com/techsupport
Copyright (c1986-2010 by Cisco SystemsInc.
Compiled Tue 17-Aug-10 09:00 by prod_rel_team

ROM
System BootstrapVersion 12.4(13r)TRELEASE SOFTWARE (fc1)

R1 uptime is 1 hour13 minutes
System returned to ROM by power
-on
System image file is 
"flash:c2800nm-advipservicesk9-mz.124-15.T14.bin"


This product contains cryptographic features and is subject to United
States 
and local country laws governing importexporttransfer and
use. 
Delivery of Cisco cryptographic products does not imply
third
-party authority to importexportdistribute or use encryption.
Importersexportersdistributors and users are responsible for
compliance with U.S. and local country lawsBy using this product you
agree to comply with applicable laws 
and regulations. If you are unable
to comply with U
.S. and local laws, return this product immediately.

A summary of U.Slaws governing Cisco cryptographic products may be found at:
http://www.cisco.com/wwl/export/crypto/tool/stqrg.html

If you require further assistance please contact us by sending email to
export
@cisco.com.

Cisco 2811 (revision 53.51with 249856K/12288K bytes of memory.
Processor board ID FHK1223F50J
2 FastEthernet interfaces
1 Virtual 
Private Network (VPNModule
DRAM configuration is 64 bits wide with parity enabled
.
239K bytes of non-volatile configuration memory.
62720K bytes of ATA CompactFlash (Read/Write)

Configuration register is 0x2102 

.exec_command() terminates SSH session right after execution, you cannot use it to execute multiple commands.

stdin, stdout, stderr are variables for INPUT, OUTPUT and ERRORS.

If we use output = stdout.readline() instead of output = stdout.readlines(), the result would be just the first line:

Cisco IOS Software2800 Software (C2800NM-ADVIPSERVICESK9-M), Version 12.4(15)T14RELEASE SOFTWARE (fc2

If we use print (output) instead of print (’‘.join(output)), the result would be:

['Cisco IOS Software, 2800 Software (C2800NM-ADVIPSERVICESK9-M), Version 12.4(15)T14, RELEASE SOFTWARE (fc2)\r\n''Technical Support: http://www.cisco.com/techsupport\r\n''Copyright (c) 1986-2010 by Cisco Systems, Inc.\r\n''Compiled Tue 17-Aug-10 09:00 by prod_rel_team\r\n''\r\n''ROM: System Bootstrap, Version 12.4(13r)T, RELEASE SOFTWARE (fc1)\r\n''\r\n''R1 uptime is 1 hour, 44 minutes\r\n''System returned to ROM by power-on\r\n''System image file is "flash:c2800nm-advipservicesk9-mz.124-15.T14.bin"\r\n''\r\n''\r\n''This product contains cryptographic features and is subject to United\r\n''States and local country laws governing import, export, transfer and\r\n''use. Delivery of Cisco cryptographic products does not imply\r\n''third-party authority to import, export, distribute or use encryption.\r\n''Importers, exporters, distributors and users are responsible for\r\n''compliance with U.S. and local country laws. By using this product you\r\n''agree to comply with applicable laws and regulations. If you are unable\r\n''to comply with U.S. and local laws, return this product immediately.\r\n''\r\n''A summary of U.S. laws governing Cisco cryptographic products may be found at:\r\n''http://www.cisco.com/wwl/export/crypto/tool/stqrg.html\r\n''\r\n''If you require further assistance please contact us by sending email to\r\n''export@cisco.com.\r\n''\r\n''Cisco 2811 (revision 53.51) with 249856K/12288K bytes of memory.\r\n''Processor board ID FHK1223F50J\r\n''2 FastEthernet interfaces\r\n''1 Virtual Private Network (VPN) Module\r\n''DRAM configuration is 64 bits wide with parity enabled.\r\n''239K bytes of non-volatile configuration memory.\r\n''62720K bytes of ATA CompactFlash (Read/Write)\r\n''\r\n''Configuration register is 0x2102\r\n'

If we delete output = stdout.readlines() and change print (’‘.join(output)) to print (stdout), the result would be channel information:

<paramiko.ChannelFile from <paramiko.Channel 0 (openwindow=1024 -> <paramiko.Transport at 0x5dddc278 (cipher aes128-cbc128 bits) (active1 open channel(s))>>> 
By privilege15