This commit is contained in:
2024-07-08 13:40:49 +02:00
parent 490fc9cb8a
commit 429958600e
4 changed files with 128 additions and 16 deletions

View File

@@ -0,0 +1,14 @@
[defaults]
#inventory = /home/user06/hosts
inventory = ./test.py
ansible_managed = Ansible managed: {file} modified on %Y-%m-%d %H:%M:%S on {host}
private_key_file = /home/jonnybravo/.ssh/ansible-test
gathering = smart
fact_caching = yaml
fact_caching_timeout = 3600
fact_caching_connection = fact
library = lib
lookup_plugins = lookup
filter_plugins = ./filter

66
play_ansible/inv_plugin/test.py Executable file
View File

@@ -0,0 +1,66 @@
#! /usr/bin/env python3.12
import scapy.all as scapy
import json
import socket
def scan(ip):
arp_request = scapy.ARP(pdst=ip)
broadcast = scapy.Ether(dst="ff:ff:ff:ff:ff:ff")
arp_request_broadcast = broadcast / arp_request
answered_list = scapy.srp(arp_request_broadcast, timeout=1, verbose=False)[0]
results = []
for element in answered_list:
result = {"ip": element[1].psrc, "mac": element[1].hwsrc, 'hostname': socket.gethostbyaddr(element[1].psrc)[0]}
results.append(result)
return results
def test_port(address: str, dest_port: int) -> bool:
try:
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock:
if sock.connect_ex((address, dest_port)) == 0:
return True
return False
except (OSError, ValueError):
return False
def create_ip_list(target_ip="192.168.50.1/24"):
only_ip_list = []
for i in scan(target_ip):
#print(test_port(address=i['ip'], dest_port=22))
if i['hostname'] != ".":
only_ip_list.append(i['hostname'])
else:
only_ip_list.append(i['ip'])
return only_ip_list
def scan_csv(csv_file=str):
pass
if __name__ == "__main__":
ip_list = create_ip_list()
output = {
"_meta": {
"hostvars": {
"webprod": {
"http_port": 123,
}
}
},
"network-scan": {
"hosts": ip_list,
"vars": {
"ansible_user": "jonnybravo",
"ansible_python_interpreter": "/usr/bin/python3",
"ansible_ssh_private_key_file": "/home/jonnybravo/.ssh/ansible-test"
}
}
}
print(json.dumps(output,indent=4, sort_keys=True))