Skip to content

Instantly share code, notes, and snippets.

@ruehowl
Created December 27, 2018 18:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ruehowl/4f5b2d575a442642b6a328c7eca186aa to your computer and use it in GitHub Desktop.
Save ruehowl/4f5b2d575a442642b6a328c7eca186aa to your computer and use it in GitHub Desktop.
wbox
# fabfile Beta V0.7
# Maintained by rahul.va@endurance.com
from fabric import Connection
from invoke import task
import dns.resolver
import ipaddress
import sys
#Tsak Wbox will whether the domain is a webhostbox.net server and retrave the user name.
#The username, domain, host, will be avilable for other tasks to perform as cnt.host cnt.domain cnt.user
@task
def Wbox(cnt,domain,host=False,user=None):
cnt.domain = domain
cnt.ip = domain if is_ip(domain) else find_host(domain,A=True)
if cnt.ip is None:
print "Domain/IP is not Valid or not Pointing :(\nPlease proivde a valid input.\nExiting.."
sys.exit(0)
cnt.host = domain if host else find_host(domain)[0]
beautify()
print "Domain:\t\t{}\nIP Address:\t{}\nHostname:\t{}".format(cnt.domain,cnt.ip,cnt.host)
if not any("webhostbox.net" in s for s in find_host(cnt.domain)) and not host:
print "{}Host is not a webhostbox.net hostname ;(".format(cnt.host)
inRput = raw_input("Please Enter the server Hostname/IP to continue:")
cnt.host = inRput if inRput != '' else sys.exit(0)
cnt.ip = domain if is_ip(cnt.host) else find_host(cnt.host,A=True)
if cnt.ip is None:
print "Domain/IP is not Valid or not Pointing :(\nPlease proivde a valid input.\nExiting.."
sys.exit(0)
print "Hostname: \t{}".format(cnt.host)
try:
cnt.user = Connection(cnt.host).sudo("/scripts/whoowns "+ cnt.domain, hide=True).stdout.strip() if not host else user
except:
print "Error Trying to find Username for {} on {}".format(cnt.domain,cnt.host)
cnt.user = user
print "user:\t\t{}".format(cnt.user)
# The task Dig can be userd as an alternative to dig. This will retern the DNS records for the list 'lookup'.
# fab Dig <Options> <Domain>
# -A This print the A record for Domain
# -h This option will give the Hostname of the A record.
@task
def Dig(cnt,domain, host=False, A=False):
lookup = [ 'A','NS', 'MX']
myResolver = dns.resolver.Resolver()
myResolver.nameservers = ['8.8.8.8', '8.8.4.4']
if host:
print find_host(domain)[0]
elif A:
print find_host(domain,A)
else:
print 'DNS Lookup...'
print 'Host ',find_host(domain)[0]
for query_type in lookup:
try:
resp = myResolver.query(domain ,query_type)
for result in resp:
print query_type, result
except:
print query_type, 'Query failed :('
@task
def port_block(cnt):
beautify()
print "Finding iptables rule for {}".format(cnt.user)
cmd=['/sbin/iptables -nL | grep '+cnt.user]
result = run_cmd(cnt.host,cmd)
if result[0] is not None:
print "{}".format(result[0].stdout.strip())
else:
print "No data found for {}".format(cnt.user)
beautify()
@task
def cpanel_session(cnt):
beautify()
print "Creatig cPanel session for user {}".format(cnt.user)
cmd=['/usr/sbin/whmapi1 create_user_session user='+cnt.user+' service=cpaneld']
result = run_cmd(cnt.host,cmd)
if result[0] is not None:
print "{}".format(result[0].stdout.strip())
else:
print "An error occurred. Could not fetch the data :("
beautify()
@task
def email_block(cnt):
beautify()
print "Searching /etc/exim/exim* for {}/{} entries".format(cnt.user,cnt.domain)
cmd=['grep -e {} -e {} /etc/exim/exim*'.format(cnt.user,cnt.domain)]
result = run_cmd(cnt.host,cmd)
if result[0] is not None:
print "{}".format(result[0].stdout.strip())
else:
print "No entries found for {}".format(cnt.user)
beautify()
@task
def quota_inode(cnt):
print "Disk Quota for {}".format(cnt.user)
cmd=['quota -g '+cnt.user]
result = run_cmd(cnt.host,cmd)
if result[0] is not None:
print "{}".format(result[0].stdout.strip())
else:
print "An error occurred. Could not fetch the data :("
beautify()
@task
def server_info(cnt):
beautify()
print "Fetching server info from {}...".format(cnt.host)
cmd = ["uname -a"]
result = run_cmd(cnt.host,cmd)
if result[0] is not None:
print "{}".format(result[0].stdout.strip())
else:
print "An error occurred. Could not fetch the data :("
beautify()
@task
def user_info(cnt):
beautify()
print 'Fetching user details from {} for user {}..'.format(cnt.host,cnt.user)
cmd=['grep '+cnt.user+ ' /etc/userdatadomains']
result = run_cmd(cnt.host,cmd)
if result[0] is not None:
print "{}".format(result[0].stdout.strip())
else:
print "An error occurred. Could not fetch the data :("
beautify()
@task
def exim_log(cnt,keyword):
beautify()
print "Serching for {} in exim_mainlog..".format(keyword)
cmd=['/usr/sbin/exigrep ' +keyword+' /var/log/exim_mainlog']
result = run_cmd(cnt.host,cmd)
if result[0] is not None:
print "{}".format(result[0].stdout.strip())
else:
print "An error occurred. Could not fetch the data :("
beautify()
@task
def ftp_log(cnt,keyword):
beautify()
print "Serching for {} in messages..".format(keyword)
cmd=['grep ' +keyword+' /var/log/messages']
result = run_cmd(cnt.host,cmd)
if result[0] is not None:
print "{}".format(result[0].stdout.strip())
else:
print "An error occurred. Could not fetch the data :("
beautify()
@task
def mail_log(cnt,keyword):
beautify()
print "Serching for {} in mail_log..".format(keyword)
cmd=['grep ' +keyword+' /var/log/maillog']
result = run_cmd(cnt.host,cmd)
if result[0] is not None:
print "{}".format(result[0].stdout.strip())
else:
print "An error occurred. Could not fetch the data :("
beautify()
@task
def inode_list(cnt,dir=None):
beautify()
print "finding inode details /hone/{}".format(cnt.user)
cmd=["for i in `sudo find /home/{}/ -maxdepth 1 -type d ` ; do echo `sudo find $i | wc -l` $i; done | sort -n".format(cnt.user)]
try:
Connection(cnt.host).run(cmd[0])
except:
print "An Error occoured could not find any data"
beautify()
@task
def reseller_domains(cnt):
beautify()
print "Finding all domains under{}".format(cnt.user)
cmd = "sudo grep c0rp0rac1n /etc/trueuserowners | awk -F \":\" '{print $1}' | xargs -I{} sudo grep {} /etc/trueuserdomains"
try:
Connection(cnt.host).run(cmd)
except:
print "An Error occoured could not find any data"
beautify()
@task
def put(cnt,host,filename):
try:
with Connection(host) as cxn:
cxn.put(filename)
except Exception as e:
print "error", e
@task
def get(cnt,host,filename):
try:
with Connection(host) as cxn:
cxn.get(filename)
except Exception as e:
print "error" , e
def run_cmd(host,CMDs):
results = []
with Connection(host) as cxn:
for cmd in CMDs:
try:
results.append(cxn.sudo(cmd, hide=True))
except Exception as e:
results.append(None)
return results
def find_host(domain,A=False):
ptr=[]
myResolver = dns.resolver.Resolver()
myResolver.nameservers = ['8.8.8.8', '8.8.4.4']
if is_ip(domain):
IP = domain
if A:
return IP
else:
try:
resp = myResolver.query(domain ,'A')
except Exception as e:
return None
if A:
for ip in resp:
return str(ip)
else:
for ip in resp:
IP = ip
req = '.'.join(reversed(str(IP).split("."))) + ".in-addr.arpa"
try:
myAnswers = myResolver.query(req, "PTR")
for rdata in myAnswers:
ptr.append(str(rdata))
return ptr
except Exception as e:
return ["not.found"]
def is_ip(ip_addr):
try:
ip = ipaddress.ip_address(unicode(ip_addr, "utf-8"))
return True
except:
return False
def beautify():
print "\n","+++"*10, "\n"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment