OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

only last element from list is successful ssh and get saved with .txt file

  • Thread starter Thread starter MrTor
  • Start date Start date
M

MrTor

Guest
I try to write a python script to collect data of my network device but it always success only last element from list that was created. At first I think it because \n from readlines but after found some solution to remove, it still not work. I'm so stuck at this, please help.

Input from source file

Code:
10.190.98.31 
10.190.98.32 
10.191.98.31 
10.191.98.32

Code:
import paramiko as pk
import netmiko as nk
import re
import time
from datetime import datetime
file_path = r'xx\Source_IP.txt'
output = open(file_path).readlines()
output = [x.rstrip('\n') for x in output]
username_for_ssh = input('Enter username: ')
password_for_ssh = input('Enter password: ')
for i in range(len(output)):
    time.sleep(5)
    ssh = pk.SSHClient()
    ssh.set_missing_host_key_policy(pk.AutoAddPolicy())
    timestamp = datetime.now()
    try:
        username = username_for_ssh
        password = password_for_ssh
        ssh.connect(hostname=output[i],username=username,password=password,look_for_keys=False)
        shell = ssh.invoke_shell()
        time.sleep(2)
        shell.send('ter le 0'+'\n'+'show run'+'\n')
        time.sleep(2)
        result = shell.recv(65535).decode('utf-8')
        print('Connection success.',timestamp)
        try:
            local_output_file = r'D:\2201890\Scripts\Test\output_'+output[i]+'.txt'
            with open(local_output_file, 'w') as output_file:
                output_file.write(result)
                time.sleep(2)
                print('Create output success.',timestamp)
        except:
            print('Create output failed.',timestamp)
    except:
        print('Connection failed.',timestamp)

Output Output

Code:
Connection failed. 2024-06-28 16:55:47.847675
Connection failed. 2024-06-28 16:55:52.870029
Connection failed. 2024-06-28 16:55:57.871382
Connection success. 2024-06-28 16:56:02.872735
Create output success. 2024-06-28 16:56:02.872735
<p>I try to write a python script to collect data of my network device but it always success only last element from list that was created. At first I think it because \n from readlines but after found some solution to remove, it still not work. I'm so stuck at this, please help.</p>
<p>Input from source file</p>
<pre><code>10.190.98.31
10.190.98.32
10.191.98.31
10.191.98.32
</code></pre>
<pre><code>import paramiko as pk
import netmiko as nk
import re
import time
from datetime import datetime
file_path = r'xx\Source_IP.txt'
output = open(file_path).readlines()
output = [x.rstrip('\n') for x in output]
username_for_ssh = input('Enter username: ')
password_for_ssh = input('Enter password: ')
for i in range(len(output)):
time.sleep(5)
ssh = pk.SSHClient()
ssh.set_missing_host_key_policy(pk.AutoAddPolicy())
timestamp = datetime.now()
try:
username = username_for_ssh
password = password_for_ssh
ssh.connect(hostname=output,username=username,password=password,look_for_keys=False)
shell = ssh.invoke_shell()
time.sleep(2)
shell.send('ter le 0'+'\n'+'show run'+'\n')
time.sleep(2)
result = shell.recv(65535).decode('utf-8')
print('Connection success.',timestamp)
try:
local_output_file = r'D:\2201890\Scripts\Test\output_'+output+'.txt'
with open(local_output_file, 'w') as output_file:
output_file.write(result)
time.sleep(2)
print('Create output success.',timestamp)
except:
print('Create output failed.',timestamp)
except:
print('Connection failed.',timestamp)
</code></pre>
<p>Output
Output</p>
<pre><code>Connection failed. 2024-06-28 16:55:47.847675
Connection failed. 2024-06-28 16:55:52.870029
Connection failed. 2024-06-28 16:55:57.871382
Connection success. 2024-06-28 16:56:02.872735
Create output success. 2024-06-28 16:56:02.872735
</code></pre>
 

Latest posts

J
Replies
0
Views
1
jbowerbir
J
V
Replies
0
Views
1
Vinicius Martin
V
Top