Created
November 25, 2015 09:25
-
-
Save gmastrokostas/79a7a36f3a48ab0c8ce0 to your computer and use it in GitHub Desktop.
Python: Check IPs for DNS entries and see if host is UP or DOWN, check SSH login. Export info to a CSV file
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import socket | |
| import subprocess | |
| import netifaces | |
| import csv | |
| import paramiko | |
| def checkPING(IP): | |
| try: | |
| ping = subprocess.check_output(['ping', '-c1', ip]) | |
| return "Host is UP" | |
| except: | |
| return "Host is DOWN" | |
| def checkDNS(IP): | |
| try: | |
| dns = socket.gethostbyaddr(ip) | |
| return dns[0] | |
| except: | |
| return "No DNS entry found" | |
| def checkSSH(IP): | |
| try: | |
| ssh = paramiko.SSHClient() | |
| ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) | |
| ssh.connect(ip, username='root', password='Password!') | |
| ssh.close() | |
| return "SSH OK" | |
| except: | |
| return "SSH NO" | |
| ip_list = [] | |
| dns_list = [] | |
| status_list = [] | |
| ssh_list = [] | |
| csvfile= open('file.csv', 'w') | |
| for loop_ip in range (30): | |
| ip = '10.0.0.%d' % loop_ip | |
| print ip, checkDNS(ip), checkPING(ip),checkSSH(ip) | |
| data = ip+" ",checkDNS(ip)+" ",checkPING(ip)+" ",checkSSH(ip) | |
| #data_list.append(data) | |
| ip_list.append(ip) | |
| dns_list.append(checkDNS(ip)) | |
| status_list.append(checkPING(ip)) | |
| ssh_list.append(checkSSH(ip)) | |
| #print data_list | |
| writer = csv.writer(csvfile, dialect='excel') | |
| writer.writerows(zip(ip_list, dns_list, status_list, ssh_list)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment