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
  Reading data from excel file –> process it >>then write to another excel output file Jennifer_Jone 0 1,047 Mar-14-2023, 07:59 PM
Last Post: Jennifer_Jone
  Python VS Code: using print command twice but not getting output from terminal kdx264 4 1,034 Jan-16-2023, 07:38 PM
Last Post: Skaperen
  How to output one value per request of the CSV and print it in another func? Student44 3 1,278 Nov-11-2022, 10:45 PM
Last Post: snippsat
  python Multithreading on single file mg24 3 1,672 Nov-05-2022, 01:33 PM
Last Post: snippsat
  Create multiple/single csv file for each sql records mg24 6 1,323 Sep-29-2022, 08:06 AM
Last Post: buran
  Saving the print result in a text file Calli 8 1,700 Sep-25-2022, 06:38 PM
Last Post: snippsat
  How to print the output of a defined function bshoushtarian 4 1,237 Sep-08-2022, 01:44 PM
Last Post: deanhystad
  failing to print not matched lines from second file tester_V 14 5,946 Apr-05-2022, 11:56 AM
Last Post: codinglearner
  Print to a New Line when Appending File DaveG 0 1,190 Mar-30-2022, 04:14 AM
Last Post: DaveG
Sad Want to Save Print output in csv file Rasedul 5 10,688 Jan-11-2022, 07:04 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

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