-2
import paramiko
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy()
ssh.connect('hostname', username='test', password='123')
sftp = ssh.open_sftp()
with open('path.txt') as fp:
    for line in fp:
        print line
        for filename in sftp.listdir(line):
            if filename.endswith('.txt') or filename.endswith('.file')  or filename.endswith('.xml'):
                check_1 = filename
                stdin, stdout, stderr = ssh.exec_command('hostname')
                result1 = stdout.read().decode().splitlines()           
                std1in, std1out, std1err = ssh.exec_command('python -V')
                result2 = std1out.read().decode().splitlines()              
                output_check = '{0:>25} {1:>45} {2:>30}'.format(filename,result1,result2)
                print output_check
                file = open("check.txt","a")
                file.write(output_check)
                file.close()    

while executing the script my code is giving output which i expected, but when i tried to print the output in text file. Alignment is not providing in correct format in check.txt file

Script output

        /home/test1/check.txt        server1       python3.0       
        /home/test1/check1.txt       server1       python3.0                      

check.txt file is writing as

      /home/test1/check.txt        server1       python3.0  /home/test1/check1.txt        server1       python3.0
Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
Vijay
  • 133
  • 1
  • 1
  • 10

1 Answers1

-1

Try this,

import paramiko
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy()
ssh.connect('hostname', username='test', password='123')
sftp = ssh.open_sftp()
with open('path.txt') as fp:
    for line in fp:
        print line
        for filename in sftp.listdir(line):
            if filename.endswith('.txt') or filename.endswith('.file')  or filename.endswith('.xml'):
                check_1 = filename
                stdin, stdout, stderr = ssh.exec_command('hostname')
                result1 = stdout.read().decode().splitlines()           
                std1in, std1out, std1err = ssh.exec_command('python -V')
                result2 = std1out.read().decode().splitlines()              
                output_check = '{0:>25} {1:>45} {2:>30}'.format(filename,result1,result2)
                print output_check
                file = open("check.txt","a")
                file.write(output_check,'\n')
                file.close()   
Hayat
  • 1,539
  • 4
  • 18
  • 32