Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python3
- import paramiko
- import psycopg2
- import os
- import socket
- from pymongo import MongoClient
- def find_sitesheet_record(site):
- client = MongoClient("mongodb://sitedata:Gibson9371@172.18.64.35/sitesheets")
- db = client.get_default_database()
- sitedata = db['sitedata']
- record = sitedata.find_one({"site_id" : site})
- return record
- def site_timezone(site):
- EST5EDT = [ "CTHA1", "DCWA1", "DCWA2", "FLFM1", "FLJA1", "FLMI1", "FLMI2", "FLOR1", "FLTA1", "FLWP1",
- "GAAT1", "KYLE1", "KYLO1", "MABO1", "MASP1", "MDBA2", "MEPO1", "MIDE1", "MIFL1",
- "MIGR1", "NCCH1", "NCGR1", "NCRA1", "NYAL1", "NYBU1", "NYNY1", "NYRO1", "NYSY1",
- "OHCI1", "OHCL1", "OHCO1", "OHDA1", "OHTO1", "OHYO1", "PAHA1", "PAJO1", "PAPH2",
- "PAPI1", "PAWB1", "RIPR1", "SCGR1", "TNKN1", "TNTC1", "VANO1", "VARI1", "VARO1",
- "VTBU1", "WVCH1", "TNCH1", "QCMO2", "QCMO3", "FLOR2", "FLTA2", "GASA1", "NYNY2",
- "SCCO1", "SCCH1", "INFW1", "MILA1", "MITC1", "SCMB1", "NCGV1", "FLTH1", "GAMA1",
- "NTV1", "CTV2", "CTV3", "CTV4", "CTV5", "CTV6", "CTV7", "CTV8", "CTV9", "DTV1", "DTV2",
- "MWDV1", "MWDV2", "MABO2", "MABO3", "MABO4", "ONTO1", "ONTO2", "ONTO3", "QCMO1", "FLGA1",
- "ININ1", "INSB1", "SCTV1", "SCTV2", "SCTV3", "SCTV4", "GAAU1", "GACO1", "NCWI1", "WVWH1",
- "GAAL1", "MEBA1", "PAER1", "INTH1", "MDSA1", "NYUT1", "VAHA1", "WVBB1", "WVCL1",
- "WVPA1", "MIMQ1", "NYBI1", "NYWA1", "OHLI1", "NYEM1", "INLA1", "OHZA1", "MIAL1",
- "MIGR1", "NCCH1", "NCGR1", "NCRA1", "NYAL1", "NYBU1", "NYNY1", "NYRO1", "NYSY1",
- "VACH1", "MEPI1" ]
- CST6CDT = [ "ALBI1", "ALMO1", "ARLR1", "IACR1", "IADA1", "IADM1", "ILCH1", "ILCH2", "INEV1",
- "KSKC1", "KSWI1", "KYPA1", "LANO1", "LASH1", "MNMI1", "MOSL1", "NEOM1", "TXWA1",
- "OKOC1", "OKTU1", "TNME1", "TNNA1", "TXAU1", "TXDA1", "TXDA2", "TXHO1", "TXHO2",
- "TXSA1", "WIGB1", "WIMA1", "WIMI1", "MSJA1", "MOSP1", "LABR1", "ALHU1", "ILCP1",
- "TXBR1", "KSTO1", "ALMG1", "ARFS1", "ILPE1", "LALA1", "TXAM1", "TXLU1", "TXTY1",
- "TXCC1", "MSBI1", "MOCO1", "MOJO1", "MSTU1", "LAMO1", "NDFA1", "NELI1", "WILC1",
- "WIWA1", "TXBE1", "ILRO1", "IASC1", "TXWF1", "TXOM1", "SDSF1", "MNDU1", "FLPC1",
- "MNRO1", "MSHA1", "NDBI1", "TXAB1", "ALDO1", "LALC1", "LAAL1", "KYBG1", "TXSN1",
- "ARJO1", "ILQU1", "MSME1", "TXLA1", "MSGR1", "TXSH1", "IAOT1", "TNJA1", "TXVI1",
- "MNMA1", "NENP1", "MOSJ1" ]
- MST7MDT = [ "COCS1", "CODE1", "CODE2", "IDBO1", "NMAL1", "TXEP1", "UTSL1", "ABCA1", "ABED1",
- "MTBI1", "MTMI1", "IDTF1", "IDIF1", "SDRC1", "WYCH1", "MTBU1", "COGJ1", "MTHE1",
- "MTGF1", "WYCA1", "MTGL1" ]
- Phoenix = [ "AZPH1", "AZTU1", "AZYU1" ]
- PST8PDT = [ "CAFR1", "CALA1", "CALA2", "CASA1", "CASD1", "CASF1", "CASF2", "NVLV1", "ORPO1",
- "WASE1", "WASP1", "NVLV2", "CASF3", "CASF4", "CABA1", "NVRE1", "BCVA1", "CASF3",
- "CAMO1", "CAPS1", "OREU1", "WAYA1", "CACH1", "ORME1", "ORBE1", "CAEU1",
- "CASB1", "CTV1W", "NTV1W", "PAWC1" ]
- Anchorage = [ "AKAN1", "AKFA1", "AKJU1" ]
- if site in EST5EDT:
- return "EST5EDT"
- elif site in CST6CDT:
- return "CST6CDT"
- elif site in MST7MDT:
- return "MST7MDT"
- elif site in PST8PDT:
- return "PST8PDT"
- elif site in Phoenix:
- return "America/Phoenix"
- elif site in Anchorage:
- return "America/Anchorage"
- elif site == "HIHO1":
- return "Pacific/Honolulu"
- elif site == "NSHA1":
- elif site == "NSHA1":
- return "America/Halifax"
- else:
- return "Unknown"
- def central_site(cmd):
- import telnetlib
- central = '172.18.64.250'
- tn = telnetlib.Telnet(host=central)
- tn.read_until("Enter Choice>")
- tn.write('TACL\n')
- tn.write('logon mw.v188\n')
- tn.read_until()
- def is_host_up(ip):
- import subprocess
- r = subprocess.Popen(['timeout', '0.8' , 'ping', '-c1', ip], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
- r.wait()
- if r.returncode == 0:
- return True
- else:
- return False
- def get_version(ip):
- client = paramiko.SSHClient()
- keys = []
- keys.append(os.environ['HOME'] + '/.ssh/mw8')
- keys.append(os.environ['HOME'] + '/.ssh/mw14')
- client.load_system_host_keys()
- client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
- try:
- client.connect(ip, username='mediawatch', key_filename=keys)
- except socket.error:
- print("Could not connect to " , ip)
- return "Unknown"
- stdin,stdout,stderr = client.exec_command('rpm -qa | grep -- -manager- | sort')
- result = stdout.read()
- version2output = "gnome-volume-manager-2.17.0-8.fc8\ntns-manager-digital-1.6-1.0\ntns-manager-remops2-2.0.5-1.fc8\n"
- version3output = "gnome-volume-manager-2.17.0-8.fc8\nmediawatch-config-postgresql-manager-1.0.0-1.fc8\ntns-manager-digital-1.7-1.1\ntns-manager-remops3-3.0.0-1.fc8\n"
- version4output = "mediawatch-4-manager-4.0.0-1.fc14.i686\nmediawatch-4-manager-base-4.0.3-1.fc14.i686\nmediawatch-config-postgresql-manager-1.0.0-4.fc14.i686\n"
- stdin.close()
- stdout.close()
- stderr.close()
- client.close()
- if result == version2output:
- return "2"
- elif result == version3output:
- return "3"
- elif result.startswith("mediawatch-4-manager-4."):
- return "4"
- else:
- print("Debug: ", result)
- return "Unknown"
- def get_service_tag(ip):
- client = paramiko.SSHClient()
- keys = []
- keys.append(os.environ['HOME'] + '/.ssh/mw8')
- keys.append(os.environ['HOME'] + '/.ssh/mw14')
- try:
- client.load_system_host_keys()
- client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
- client.connect(ip, username='mediawatch', key_filename=keys)
- stdin,stdout,stderr = client.exec_command('sudo dmidecode -s system-serial-number')
- result = stdout.read()
- stdin.close()
- stderr.close()
- client.close()
- return result.rstrip('\n')
- except:
- return "Error in get_service_tag"
- def guess_hostname(ip):
- import subprocess, unidecode
- from bs4 import BeautifulSoup
- try:
- useless1, useless2, subnet, host = ip.split('.')
- except ValueError:
- return "Error in split " +str(ip)
- cmd = 'curl -v --silent http://172.18.64.38/index.php?title=Sitesheets:Subnets 2>&1 | grep -B1 "<td> ' +str(subnet)+ ' </td>"'
- result = subprocess.check_output(cmd, shell=True)
- soup = BeautifulSoup(result, 'lxml')
- to_process = unidecode.unidecode(soup.text)
- try:
- name, subnet, useless = to_process.split('\n')
- except ValueError:
- return "Error in split " +str(ip)
- res_site = name.strip(' ')
- res_subnet = subnet.strip(' ')
- if host == '10':
- host_guess = 'MGR'
- elif host == '41':
- host_guess = 'C1'
- elif host == '42':
- host_guess = 'C2'
- elif host == '43':
- host_guess = 'C3'
- elif host == '44':
- host_guess = 'C4'
- elif host == '45':
- host_guess = 'C5'
- else:
- host_guess = 'UNK'
- output = res_site + '-' + host_guess
- return output
- def get_hostname(ip):
- client = paramiko.SSHClient()
- keys = []
- keys.append(os.environ['HOME'] + '/.ssh/mw8')
- keys.append(os.environ['HOME'] +'/.ssh/mw14')
- client.load_system_host_keys()
- client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
- try:
- client.connect(ip, username='mediawatch', key_filename=keys)
- stdin,stdout,stderr = client.exec_command('hostname')
- result = stdout.read()
- stdin.close()
- stderr.close()
- client.close()
- return result.rstrip('\n')
- except:
- return "Error in hostname determination " + str(ip) + " (" + str(guess_hostname(ip)) + ") "
- def get_site_list():
- sitel = []
- conn = psycopg2.connect(database="oplogs", host="mwops.tnsmi-cmr.com", user="remote", password="littleblue")
- cursor = conn.cursor()
- sitesq = cursor
- sitesq.execute("SELECT * from sitelist WHERE nonsite='f' AND webremops='t' ORDER BY site;")
- sites = sitesq.fetchall()
- for site in sites:
- sitel.append(site[0])
- return sitel
- def execute_command(ip, command):
- try:
- client = paramiko.SSHClient()
- keys = []
- keys.append(os.environ['HOME'] + '/.ssh/mw8')
- keys.append(os.environ['HOME'] +'/.ssh/mw14')
- client.load_system_host_keys()
- client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
- client.connect(ip, username='mediawatch', key_filename=keys)
- stdin,stdout,stderr = client.exec_command(command)
- stdoutr = stdout.read()
- stderrr = stderr.read()
- stdin.close()
- stdout.close()
- stderr.close()
- client.close()
- return stdoutr, stderrr
- except:
- return "Error running command", "Error running command"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement