Python Forum
Print output in single file using pramika loop
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Print output in single file using pramika loop
#1
Hi,
I am using the below script for my automation but currently, it is making different putout file each device but I want to make a only single file for all devices. Can you help me:
import paramiko
import time
import datetime
import re
import sys
import getpass
import time
from datetime import date
from datetime import datetime
import os
import socket


def get_filename_datetime():
    # Use current date & time to get a text file name.
        return str(datetime.now().strftime('%Y_%m_%d %H_%M_%S'))

# Get full path for writing.
fname = get_filename_datetime()

user = input("Enter your M-account: ")
password = getpass.getpass("Enter your M-account Password: ")

ssh = paramiko.SSHClient()

ips = [i.strip() for i in open("switchlist.txt")] # creates a list from input file

for HOST in ips:
    try:
        #date_time = datetime.datetime.now().strftime("%Y-%m-%d")
        ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
        ssh.connect(HOST, port=22, username=user, password=password, look_for_keys=False, timeout=8, banner_timeout=5)
        connection = ssh.invoke_shell()
        connection.send("show run | se vty\n")
        time.sleep(10)
        file_output = connection.recv(99999999).decode(encoding='utf-8')
        hostname = (re.search('(.+)#', file_output)).group().strip('#')
        print(file_output)
        outFile = open(hostname + "_" + HOST + ".txt", "w")
        outFile.writelines(file_output)
        outFile.close()
        ssh.close()
       print("*" * 20 + " " + "%s is done" % hostname + " " + "*" * 20)

    except paramiko.AuthenticationException:
        print("X" * 20 + " " + HOST + ' === Bad credentials ' + "X" * 20)
    except paramiko.SSHException:
        print("X" * 20 + " " + HOST + ' === Issues with ssh service ' + "X" * 20)
    except socket.error:
        print("X" * 20 + " " + HOST + ' === Device unreachable ' + "X" * 20)
Reply
#2
Hello,
the code loops through ips list, and for each ip (in the try block) outFile = open(hostname + "_" + HOST + ".txt", "w") this line creates a new file at each for loop iteration.
There are several possible solutions to this. What I would do is (assuming "switchlist.txt" isn't a humongous list) first loop through ips. Store the data you need in a data structure (list of strings, for example) with each iteration. And after the for loop is finished, write the collected data to the file.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  failing to print not matched lines from second file tester_V 13 522 Apr-13-2021, 03:36 PM
Last Post: DeaD_EyE
  Remove single and double quotes from a csv file in 3 to 4 column shantanu97 0 153 Mar-31-2021, 10:52 AM
Last Post: shantanu97
  print function output wrong with strings. mposwal 5 353 Feb-12-2021, 09:04 AM
Last Post: DPaul
  Print output not working xninhox 7 611 Jan-16-2021, 09:42 AM
Last Post: xninhox
  why print('\n') produced 2 new lines instead of 1 - Located inside a FOR loop JulyFire 2 312 Jan-10-2021, 01:50 AM
Last Post: JulyFire
  Output with none, print(x) in function Vidar567 3 288 Nov-24-2020, 05:40 PM
Last Post: deanhystad
  output correction using print() function afefDXCTN 2 1,559 Oct-13-2020, 12:58 PM
Last Post: cvilao
  Print characters in a single line rather than one at a time hhydration 1 389 Oct-10-2020, 10:00 PM
Last Post: bowlofred
  using 'while loop' output going into infinite loop... amitkb 2 520 Oct-05-2020, 09:18 PM
Last Post: micseydel
  get two characters, count and print from a .txt file Pleiades 9 720 Oct-05-2020, 09:22 AM
Last Post: perfringo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020