I am currently trying to get ip address from text. But the code I tried is only getting the last line from the file. I am using the following code
import paramiko
import time
import getpass
import sys
import socket
import re
user = raw_input("Enter you username: ")
password = getpass.getpass()
inp = open(r'ipaddressrouter.txt', 'r')
for line in inp:
try:
ssh_client = paramiko.SSHClient()
ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh_client.connect(hostname=line,username=user,password=password)
print "Successful Connection to " + line + '\n'
stdin, stdout, stderr = ssh_client.exec_command('sh ip int b \n')
output = stdout.read()
out = open('out.txt', 'a')
out.write(line + '\n')
out.write(output + '\n')
out.write('\n')
except (socket.error, paramiko.AuthenticationException):
status = 'fail'
ssh_client.close
help would be appreciated
Update:
When I removed except
I got the following error
File "C:\Users\abc\Desktop\Python Test Scripts\newstest2.py", line 20, in
ssh_client.connect(hostname=host,username=user,password=password)
File "C:\Python27\lib\site-packages\paramiko\client.py", line 329, in connect to_try = list(self._families_and_addresses(hostname, port)) File "C:\Python27\lib\site-packages\paramiko\client.py", line 200, in _families_and_addresses hostname, port, socket.AF_UNSPEC, socket.SOCK_STREAM)socket.gaierror: [Errno 11004] getaddrinfo failed
Can some one help me out ?
Successful Connection to...? Do you see a message for each entry inipaddressrouter.txtor only the last one too? 2. You open the fileout.txtmultiple times but never close it. Trywith open('out.txt', 'a') as out:3. You open the fileipaddressrouter.txttwice and use only one (and also never close it).