Python - Warning: Unexpected Error: <class 'IndexError'> - python

I am trying to use some code to read username and password from a file called ".logon" and establish a connection to redshift. But I got the error message "Python - Warning: Unexpected Error: ".
Here is my code:
def redshift_connect():
try:
l_user = getpass.getuser()
home = expanduser("~")
f = open(home + '/.logon')
logline = f.readline()
usr = logline.split(',')[0].split('/')[1].strip()
pwd = logline.split(',')[1].rstrip()
conn=psycopg2.connect(host='host-name',
port= 5439, user= usr, password= pwd)
return conn
except IOError as e:
print ('I/O error ({0}): {1}: .tdlogon missing!!'.format(e.errno, e.strerror))
except:
print ('Warning: Unexpected Error:',sys.exc_info()[0])
else:
raise ConnectionError('Cannot establish connectionw with Redshift.')

I guess the expression logline.split(',') works and returns an array with only one entry. The expression logline.split(',')[1] then raises an IndexError which you catch in the unspecific except clause.

Related

How to check if email is valid and working using python

Am trying to validate some emails using python, I keep getting for all emails except for the ones matching my own email server
import re
import smtplib
import dns.resolver
import socket
import smtplib
# Address used for SMTP MAIL FROM command
fromAddress = 'fromMyEmail#mydomain.com'#
input = open("input.txt", "r")
output = open("output.txt", "w+")
# Simple Regex for syntax checking
regex = '^[_a-z0-9-]+(\.[_a-z0-9-]+)*#[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,})$'
_VALID = 'VALID'
_IN_VALID = 'IN_VALID'
output.write('{}\t{}\t{}\t{}\t{}\n'.format('email', 'domain', 'code', 'message', 'is_valid'))
for inputAddress in input.readlines():
print('parsing ', inputAddress)
try:
code = 0
message = None
# SMTP lib setup (use debug level for full output)
server = smtplib.SMTP()
server.set_debuglevel(0)
addressToVerify = str(inputAddress)
# Syntax check
match = re.match(regex, addressToVerify)
if match == None:
print('Bad Syntax')
raise ValueError('Bad Syntax')
# Get domain for DNS lookup
splitAddress = addressToVerify.split('#')
domain = str(splitAddress[1])
print('Domain:', domain)
# MX record lookup
records = dns.resolver.query(domain, 'MX')
print('exchanging')
mxRecord = records[0].exchange
mxRecord = str(mxRecord)
print('mxRecord ', mxRecord)
# SMTP Conversation
server.connect(mxRecord)
server.helo(server.local_hostname) ### server.local_hostname(Get local server hostname)
server.mail(fromAddress)
code, message = server.rcpt(str(addressToVerify))
server.quit()
if code == 250:
output.write(
'{}\t{}\t{}\t{}\t{}\n'.format(addressToVerify.strip(), domain.strip(), code, str(message).strip(),
_VALID))
else:
output.write(
'{}\t{}\t{}\t{}\t{}\n'.format(addressToVerify.strip(), domain.strip(), code, str(message).strip(),
_IN_VALID))
except Exception as e:
output.write('{}\t{}\t{}\t{}\t{}\n'.format(addressToVerify.strip(), domain, -1, str(e).strip(), _IN_VALID))
output.close()
but I keep getting the error [Errno 111] Connection refused

python ssh paramiko save to file

I would like to save to file all my output from ssh connection(s). The ssh connection works fine, the output is ok too to a stdout. I want to create a file for every connection individually in a file. I have changed the output lines to the lines below and also moved it above
output_filename = ip_address + ".txt"
file = open(output_filename, 'w')
file.write(output.decode)
file.close()
What is missing?
I receive this error:
line 100, in fractal
except 10060:
TypeError: catching classes that do not inherit from BaseException is not allowed
And it just don't save the output. the file is created, but in blank.
import socket
import paramiko
import time
import sys
def fractal(ip, username, passwd, enapass, command, command2, command3, command4, devtype):
ip_address = ip
user = username
password = passwd
enapw = enapass
commando = command
commando2 = command2
commando3 = command3
commando4 = command4
devtype = devtype
print("Connecting to: "+ip + " on Port 22")
try:
if ip:
global ssh_client
ssh_client = paramiko.client.SSHClient()
ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh_client.connect(hostname=ip_address, username=user, password=password, compress=True, look_for_keys=False, allow_agent=False, timeout=5)
print("########################## CONNECTED TO: "+ip_address +" ##########################")
remote_connection = ssh_client.invoke_shell()
if devtype == 'CISCO':
results = remote_connection.send("term len 0\n")
print(results)
time.sleep(2)
results = remote_connection.send("show run\n")
print(results)
time.sleep(6)
if devtype == 'F5':
remote_connection.send("term len 0\n")
time.sleep(2)
remote_connection.send("sh ver\n")
time.sleep(6)
if devtype == 'LINUX':
remote_connection.send("pwd\n")
time.sleep(2)
else:
#print("Please set IP Address first!!!")
pass
if enapass:
remote_connection.send("enable\n")
# remote_connection.send("conf t\n")
remote_connection.send(enapw)
remote_connection.send("\n")
else:
pass
if command:
#remote_connection.send("show run\n")
remote_connection.send(commando)
remote_connection.send("\n")
else:
print("Command not found!")
if command2:
remote_connection.send(commando2)
remote_connection.send("\n")
else:
pass
if command3:
remote_connection.send(commando3)
remote_connection.send("\n")
else:
pass
if command4:
remote_connection.send(commando4)
remote_connection.send("\n")
else:
pass
time.sleep(1)
output = remote_connection.recv(65535)
print(output.decode())
print("########################## END OF: " + ip_address + " ##########################")
reader = ssh_client.connect
ssh_client.close
output_filename = ip_address + ".txt"
file = open(output_filename, 'w')
file.write(output)
file.close()
except TypeError:
print('Please check your settings!')
except UnboundLocalError:
print('Please check IP Address!')
except paramiko.AuthenticationException:
print(ip+": Authentication failed, please verify your credentials.")
except paramiko.SSHException as sshException:
print(ip+": Unable to establish SSH connection: %s" % sshException)
except paramiko.BadHostKeyException as badHostKeyException:
print(ip+": Unable to verify server's host key: %s" % badHostKeyException)
except socket.error:
print(ip+": Couldn't connect to server. Check IP Address and Port")
# sys.exit()
except 10060:
print(ip+": The host was not reachable")
except socket.gaierror:
print(ip+': Check IP Address')
except 11004:
print(ip+": The host was not reachable")
except IOError as e:
print("I/O error({0}): {1}".format(e.errno, e.strerror))
except ValueError:
print("Could not convert data to an integer.")
except FileNotFoundError:
print("No File was selected!")
except:
print("Unexpected error:", sys.exc_info()[0])
raise
# countErr = paramiko.AuthenticationException
# countErr = 0
# for countErr in countErr:
# count = count + 1
# print ("Athentication failures: "+countErr)
This happens only in Python3, because except now expects a class that is a subclass of BaseException. And the integer 10060 is not.
>>> try:
... raise ValueError
... except 10080:
... print('dfsdf')
...
Traceback (most recent call last):
File "<stdin>", line 2, in <module>
ValueError
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "<stdin>", line 3, in <module>
TypeError: catching classes that do not inherit from BaseException is not allowed
So Paramiko is raising some other error, but error handling dies when it tries to evaluate the except 10060 statement.

parallel files download from ftp

When i try to download files from ftp sequentially it works perfectly
import ftplib
import os
import logging
class pyFTPDownload(object):
def __init__(self,
remote_host=None,
port=None,
username=None,
passwd=None,
input_folder=None,
output_folder=None,
ftp_conn_id=None,
timeout=10
):
super(pyFTPDownload, self).__init__()
self.remote_host = remote_host
self.port = port
self.username = username
self.passwd = passwd
self.input_folder = input_folder
self.output_folder = output_folder
self.ftp_conn_id = ftp_conn_id
self.timeout = timeout
self.client = None
def get_conn(self):
if not self.client:
logging.info('creating ftp client for conn_id: {0}'.format(self.ftp_conn_id))
if not self.username:
raise Exception("Missing required param: username")
if not self.passwd:
raise Exception("Missing required param: passwd")
if not self.remote_host:
raise Exception("Missing required param: remote_host")
if not self.ftp_conn_id:
self.ftp_conn_id = str(self.username) + '#' + str(self.remote_host) + ":" + (
str(self.port) if self.port else "")
try:
client = ftplib.FTP()
client.connect(self.remote_host, (self.port if not self.port else None))
client.login(self.username, self.passwd)
self.client = client
except ftplib.all_errors as remote_host_error:
logging.error("Auth failed while connecting to host: {0}, error: {1}"
.format(self.remote_host, remote_host_error))
except Exception as error:
logging.error("Error connecting to host: {0}, error: {1}"
.format(self.remote_host, error))
return self.client
def get_file(self, input_folder, output_folder=None,file_ext=None,thread_nbr=1):
#os.chdir(output_folder)
ftp = self.get_conn()
#print ftp.dir(input_folder)
logging.debug(input_folder)
if not os.path.exists(input_folder):
try:
os.makedirs(output_folder, 0755)
logging.debug("{0} is created".format(output_folder))
except OSError, e:
logging.error("ERROR: {0}".format(e))
logging.info(output_folder)
ftp.cwd(input_folder)
for filename in ftp.nlst(file_ext): # Loop - looking for matching files
try:
logging.debug("filename {0}".format(filename))
local_file=os.path.join(output_folder,filename)
logging.debug("local_file {0}".format(local_file))
with open(local_file, 'wb') as fhandle:
logging.debug('Getting ' + filename) # for confort sake, shows the file that's being retrieved
ftp.retrbinary('RETR ' + filename, fhandle.write)
fhandle.close()
except Exception as e:
logging.error("could not download file:{0}, terminated with error {1}".format(filename,e))
But when i tried to do the same thing in parallel i get
[Errno 9] Bad file descriptor
or when i try to decomment the following two lines
xftp.connect(self.remote_host,self.port)
xftp.login(self.username,self.passwd)
i get this error: a float is required. But without a stacktrace so i can debug it
my full code follow:
# coding=utf-8
from itertools import izip, repeat
import ftplib
import os
import multiprocessing
from pathos.multiprocessing import ProcessingPool as Pool
import logging
import traceback
class pyFTPDownload(object):
def __init__(self,
remote_host,
port,
username,
passwd,
ftp_conn_id=None
):
super(pyFTPDownload, self).__init__()
self.remote_host = remote_host
self.port = port
self.username = username
self.passwd = passwd
self.ftp_conn_id = ftp_conn_id
self.client = None
if not self.client:
logging.info('creating ftp client for conn_id: {0}'.format(self.ftp_conn_id))
if not self.username:
raise Exception("Missing required param: username")
if not self.passwd:
raise Exception("Missing required param: passwd")
if not self.remote_host:
raise Exception("Missing required param: remote_host")
if not self.ftp_conn_id:
self.ftp_conn_id = str(self.username) + '#' + str(self.remote_host) + ":" + (
str(self.port) if self.port else "")
try:
client = ftplib.FTP()
client.connect(self.remote_host, (self.port if self.port else None))
client.login(self.username, self.passwd)
self.client = client
except ftplib.all_errors as remote_host_error:
logging.error("Auth failed while connecting to host: {0}, error: {1}"
.format(self.remote_host, remote_host_error))
except Exception as error:
logging.error("Error connecting to host: {0}, error: {1}"
.format(self.remote_host, error))
def get_conn(self):
return self.client
class loadData(pyFTPDownload):
def __init__(self,
remote_host,
port,
username,
passwd,
input_folder,
output_folder,
file_ext=None,
nbr_processes=None,
ftp_conn_id = None):
super(loadData, self).__init__(remote_host,port,username,passwd)
self.input_folder=input_folder
self.output_folder=output_folder
self.file_ext=file_ext
self.nbr_processes=nbr_processes
if not input_folder:
raise Exception("Missing required params: input_folder")
if not output_folder:
raise Exception("Missing required params: output_folder")
if not file_ext:
logging.warn("All the existing files in {0} will be considered".format(input_folder))
if not nbr_processes:
logging.warn("The number of processes to be started will be set to {0}".format(Pool.ncpus))
self.nbr_processes=multiprocessing.cpu_count()
def downloadfunc(self,a):
return self.downloadf(*a)
def downloadf(self, inputf, filename, outputf):
global xftp
global local_file
global fhandle
print filename
try:
xftp = self.get_conn()
xftp.connect(self.remote_host,self.port)
xftp.login(self.username,self.passwd)
print xftp
except ftplib.all_errors as remote_host_error:
logging.error("Auth failed while connecting to host: {0}, error: {1}"
.format(self.remote_host, remote_host_error))
except Exception as error:
logging.error("Error connecting to host: {0}, error: {1}"
.format(self.remote_host, error))
try:
logging.debug("filename {0}".format(filename))
local_file = os.path.join(outputf, filename)
logging.debug("local_file {0}".format(local_file))
except Exception as sd:
logging.error("Unkmown error: {}".format(sd))
xftp.cwd(inputf)
try:
with open(local_file, 'wb') as fhandle:
logging.debug('Getting ' + filename) # for confort sake, shows the file that's being retrieved
xftp.retrbinary('RETR ' + filename, fhandle.write)
fhandle.close()
except Exception as k:
logging.error("Could not download {0} : {1}".format(local_file,k))
finally:
xftp.quit()
print traceback
def get_file(self):
print "PREPARING FILE DOWNLOAD"
print self.output_folder
if not os.path.exists(self.output_folder):
try:
logging.debug("{} does not exists".format(self.output_folder))
os.makedirs(self.output_folder,0755)
logging.debug("{0} is created".format(self.output_folder))
except OSError, e:
logging.error("ERROR:{0} could not be created {1}, {2}".format(self.output_folder,e,OSError))
except Exception as d:
logging.error(d)
ftpObj=self.get_conn()
ftpObj.cwd(self.input_folder)
files_to_dl=ftpObj.nlst(self.file_ext)
p = Pool(self.nbr_processes)
try:
p.map(self.downloadfunc, izip(repeat(self.input_folder),files_to_dl,repeat(self.output_folder)))
p.close()
p.join()
except Exception as f:
logging.error(f)
I do not have a lot of experience with python, so it would be very nice of you to check my code.
I have also some questions what is the best way to implement multiprocessing in this case?
I have found the problem with my code.
In the downloadf function and exactly in this code part
try:
xftp = self.get_conn()
xftp.connect(self.remote_host,self.port)
xftp.login(self.username,self.passwd)
print xftp
except ftplib.all_errors as remote_host_error:
logging.error("Auth failed while connecting to host: {0}, error: {1}"
.format(self.remote_host, remote_host_error))
except Exception as error:
logging.error("Error connecting to host: {0}, error: {1}"
.format(self.remote_host, error))
The problem was that i used the same ftp connection instance through, xftp = self.get_conn() and that is why i got errors that didn't seem to describe the source problem, how matter what changes did i introduce to my code to fix that.
The solution is to instantiate a new ftp connection for each process, so instead of the above line of code i do the folloying now: xftp = ftplib.FTP()
Maybe there is a kind soul that could bring a more 'pythonic' explanation to this issue. I would be gratefull

Not able to ignore socket.timeout error using Paramiko

I am trying to SSH into a bunch of routers and grep some info from each. Some of the router IPs might not be valid as they are got from the loop. For the ones that do not exist, i am getting a socket.timeout error (obviously) and the script exits execution at that point. I want python to ignore this error and continue with the rest of the iterations. I have included the except/pass/continue commands, but they somehow do not seem to be doing their job and i am still getting the socket.timeout error and exiting the script :
import paramiko
import socket
import subprocess
import time
site = ['sao2','mia2','par2','scs2','sin4','sjl1','syd3','tok2','tor1','wdc1','akl1','chc1','chi2','cop2','dal1','fra2']
csr_hostname = 'csr_1'
csr_pass = str('*******')
username = str('test_user')
csrcmd = 'sh ver \n'
f = open ("version_output.txt", 'r+')
#*********************************************************************
for DC in site :
y = str(DC)
mcp = socket.gethostbyname(y)
mcpip = mcp.split('.')
mcpip2 = int(mcpip[0])
mcpip3 = int(mcpip[1])
mcpip4 = int(mcpip[2])
print (DC + ":")
f.write('\n')
f.write(DC)
f.write('\n=====\n')
for i in range(5,33):
x=37+(2*i)
mcpcsr = (str(mcpip2) + ('.') + str(mcpip3)+ ('.') + str(mcpip4)+ ('.') + str(x))
fqdn1=(str(i)+'-01.'+str(DC))
print i
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(mcpcsr, username=username, password=csr_pass, look_for_keys = False, allow_agent = False, timeout=10)
router_channel = ssh.invoke_shell()
router_channel.settimeout(2)
try :
buff = ''
router_channel.send(csrcmd)
time.sleep(1)
buff = router_channel.recv(99999)
for item in buff.split("\n"):
if "Cisco IOS XE Software, Version" in item :
csrver = (fqdn1 + " : " + item.strip())
if "Cisco IOS Software" in item :
csrver1 = (item.strip())
print csrver
print csrver1
f.write(csrver)
f.write('\n')
f.write(csrver1)
f.write('\n')
fqdn1 = ''
ssh.close()
#*********************************************************************
except socket.timeout:
print ("bla bla")
pass
continue
except socket.error :
print ("bla bla")
pass
continue
except paramiko.SSHException, socket.error :
print ("bla bla")
pass
continue
except Exception :
print ("bla bla")
pass
continue
#*********************************************************************
Output :
Traceback (most recent call last):
File "csrnum.py", line 36, in ?
ssh.connect(mcpcsr, username=username, password=csr_pass, look_for_keys = False, allow_agent = False, timeout=10)
File "/usr/lib/python2.4/site-packages/paramiko/client.py", line 291, in connect
sock.connect(addr)
File "<string>", line 1, in connect
socket.timeout: timed out
Any insight into this will be much appreciated and grateful for!
Figured it out! The exceptions were set one indent more than the one where the ssh connections were attempted. Made a small change to the code to accomodate this and the exceptions are working as expected!

LDAP uid path not known

Here there my working python script:
#!/usr/bin/python
import ldap, sys
l = ldap.initialize('ldap://myldapserver:389')
username = "uid=%s,OU=folder1,OU=myCompany,O=MyCompanyGroup" % "myID"
password = "mypassword"
try:
l.protocol_version = ldap.VERSION3
l.simple_bind_s(username, password)
valid = True
print "OK"
l.unbind()
except Exception, error:
print error
Now I would like to bind the user, just only specifying "O=MyCompanyGroup", and searching for the uid on that subtree. For example I can filter based on "inetOrgPerson"
I tried
username = "(&(objectClass=inetOrgPerson )(uid=%s)(O=MyCompanyGroup))" % "myID"
but I get Invalid credentials.
Thanks!
Riccardo
Found the solution
#!/usr/bin/python
import ldap, sys
l = ldap.initialize('ldap://myldapserver:389')
search_filter = "(&(uid=myID)(objectClass=inetOrgPerson))"
base_dn="O=MyCompanyGroup"
password = "mypassword"
try:
l.protocol_version = ldap.VERSION3
result = l.search_s(base_dn, ldap.SCOPE_SUBTREE, search_filter, None)
user_dn = result[0][0]
print user_dn
l.simple_bind_s(user_dn, password)
valid = True
print "OK"
l.unbind()
except Exception, error:
print error
Good technique #Riccardo79
In my case, I was forced to login as the admin of the LDAP first to performe the user password check.
Here is my code :
import ldap
ldap_admin_dn = "cn=admin..."
ldap_admin_password = "..."
ldap_users_dn="ou=to,ou=my,ou=users..."
def connectLDAP(self, username, password) -> bool:
search_filter = "(&(uid={})(objectClass=inetOrgPerson))".format(username)
try:
connection = ldap.initialize(self.ldap_endpoint)
connection.protocol_version = ldap.VERSION3
connection.simple_bind_s(ldap_admin_dn, ldap_admin_password)
result = connection.search_s(ldap_users_dn, ldap.SCOPE_SUBTREE, search_filter)
user_dn = result[0][0]
connection.simple_bind_s(user_dn, password)
connection.unbind()
except ldap.LDAPError as e:
print(e)
return False
return result

Categories