2

I am a newbie to Python as well as the programming world. After a bit of research for the past 2 days am now able to successfully SSH into the Cisco router and execute set of commands. However my original goal is to print the resultant output to a text file. Checked lots of posts by forum members which helped me in constructing the code, but I couldn't get the result printed on the text file. Please help.

Here is my code:

import paramiko
import sys
import os

dssh = paramiko.SSHClient()
dssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
dssh.connect('10.0.0.1', username='cisco', password='cisco')  
stdin, stdout, stderr = dssh.exec_command('sh ip ssh')
print stdout.read()
f = open('output.txt', 'a')
f.write(stdout.read())
f.close()
dssh.close()
iruvar
  • 22,736
  • 7
  • 53
  • 82
Raj
  • 67
  • 1
  • 4
  • 10
  • 1
    You have already exhausted `stdout` by calling `read` on it and passing to `print`. The second `stdout.read()` will therefore yield nothing to `f.write` – iruvar Dec 24 '13 at 20:30

2 Answers2

2

stdout.read() will read the content and move the file pointer forward. As such, subsequent calls will not be able to read the content again. So if you want to print the content and write it to a file, you should store it in a variable first and then print and write that.


Instead of mentioning the IP address directly on the code, is it possible for me to fetch it from list of IP addresses (mentioned line by line) in a text file?

You can read lines from a file like this:

with open('filename') as f:
    for line in f:
        # Each line will be iterated; so you could call a function here
        # that does the connection via SSH
        print(line)
poke
  • 369,085
  • 72
  • 557
  • 602
  • One more query.. Instead of mentioning the IP address directly on the code, is it possible for me to fetch it from list of IP addresses (mentioned line by line) in a text file? Please help. – Raj Dec 25 '13 at 05:44
  • Thanks for the inputs. Now am able to read the ip address from the file and now I am stuck on how to substitute the ip address read from the file in place of the actual ip address in the code ('10.0.0.1'). Is there a function for that? – Raj Dec 26 '13 at 17:50
  • If the line only contains the IP address, then you can just use that `line` variable there: `dssh.connect(line, …` – poke Dec 26 '13 at 18:07
  • Am getting this error (gaierror: [Errno 11004] getaddrinfo failed). – Raj Dec 26 '13 at 18:22
  • Please open a new question then; it has nothing to do with this question. – poke Dec 26 '13 at 18:43
1

I know this is very late but the code below is what I'm using to do exactly what is being asked.

from __future__ import print_function
from netmiko import ConnectHandler

import sys
import time
import select
import paramiko
import re
fd = open(r'C:\Users\NewdayTest.txt','w') 
old_stdout = sys.stdout   
sys.stdout = fd 
platform = 'cisco_ios'
username = 'Username'
password = 'Password'

ip_add_file = open(r'C:\Users\\IPAddressList.txt','r') 

for host in ip_add_file:
    device = ConnectHandler(device_type=platform, ip=host, username=username, password=password)
    output = device.send_command('terminal length 0')
    output = device.send_command('enable')
    print('##############################################################\n')
    print('...................CISCO COMMAND SHOW RUN OUTPUT......................\n')
    output = device.send_command('sh run')
    print(output)
    print('##############################################################\n')
    print('...................CISCO COMMAND SHOW IP INT BR OUTPUT......................\n')
    output = device.send_command('sh ip int br')
    print(output) 
    print('##############################################################\n')

fd.close()

Or, if you wanted to print from a single host, use this slight edit. That simply removes looking for a list to get the IP address:

from __future__ import print_function
from netmiko import ConnectHandler

import sys
import time
import select
import paramiko
import re
fd = open(r'C:\Users\NewdayTest.txt','w') 
old_stdout = sys.stdout   
sys.stdout = fd
host = '10.10.10.10'
platform = 'cisco_ios'
username = 'Username'
password = 'Password'
device = ConnectHandler(device_type=platform, ip=host, username=username, password=password)
output = device.send_command('terminal length 0')
output = device.send_command('enable')
print('##############################################################\n')
print('...................CISCO COMMAND SHOW RUN OUTPUT......................\n')
output = device.send_command('sh run')
print(output)
print('##############################################################\n')
print('...................CISCO COMMAND SHOW IP INT BR OUTPUT......................\n')
output = device.send_command('sh ip int br')
print(output) 
print('##############################################################\n')

fd.close()
Chris Jones
  • 45
  • 2
  • 9