This is what I have written for hwid authentication:
try:
current_machine_id = subprocess.check_output('wmic csproduct get uuid').decode().split('\n')
[1].strip()
r = requests.get('https://github.com/reactxsw/hwiddump/blob/main/hwid.txt')
except:
print('Error : Internet connection')
b = input('')
time.sleep(2)
def Authenticator():
if not current_machine_id in r.text:
print('Error : HWID not in database')
print(f'Invalid HWID :' + current_machine_id)
a = input('')
time.sleep(5)
exit()
else:
print("Permission granted !")
return True
Authenticator()
This takes the hwid which is stored in github. I wanted to have a username for each and every hwid.
So I have to do something like 00000000-0000-0000-0000-00D86152166F = REACT in the.txt I want to make a code which checks for hwid and take the name.
Do a GET request on raw text URL.
Below is the revised code:
try:
current_machine_id = subprocess.check_output('wmic csproduct get uuid').decode().split('\n')
[1].strip()
r = requests.get('https://raw.githubusercontent.com/reactxsw/hwiddump/main/hwid.txt')
uuids = r.text.strip().split("\n")
users = {}
for i in uuids:
users[i.split("=")[0].strip()] = i.split("=")[1].strip()
except:
print('Error : Internet connection')
b = input('')
time.sleep(2)
def Authenticator():
if not current_machine_id in uuids:
print('Error : HWID not in database')
print(f'Invalid HWID :' + current_machine_id)
a = input('')
time.sleep(5)
exit()
else:
print(f'Hello {users[current_machine_id]}')
print("Permission granted !")
return True
Authenticator()
Related
I am a beginner in DevOps and a noob at programming. I have been assigned a task to autostart a group of instances with a specific sequence. Checking the health of its Linux services before starting the next one.
I found an auto stop and start python script that can be run as a lambda function, but I am clueless about how can I start the instances sequentially and check the server services health.
I would really appreciate, if something can help me out or guide me on how can I do that.
Thank you
import boto3
import request
import time
region = 'region'
instances = ['']
ec2 = boto3.client('ec2', region_name=region)
def Ec2Instance1(ec2start):
ec2.start_instances(InstanceIds=instances)
print('started your instances: ' + str(instances))
def lambda_handler(event, context):
websiteURL = ['https://example1.com','https://example2.com','https://example3.com']
topicArnCode = 'arn:aws:sns:ap-southeast-1:123:sample'
for x in websiteURL:
print (x)
r = requests.get(x,verify=False)
print (r)
if r.status_code == 200:
Ec2Instance1()
time.sleep(10)
elif r.status_code == 200:
Ec2Instance1()
else:
sns_client = boto3.client('sns')
sns_client.publish(
TopicArn = topicArnCode,
Subject = 'Website is not reachable ' + x,
Message = 'Website: ' + x + ' is down\n')
print('Website is dead')
import boto3
import requests
import time
AWS_Access_Key_ID =
AWS_Secret_Access_Key =
DELAY_TIME=10 # 10 Seconds
region = 'us-east-2'
# instances = ['']
instances = {
'instance id': 'http://link',
'instance id': 'http://link'
}
ec2 = None
try:
ec2 = boto3.client('ec2', aws_access_key_id=AWS_Access_Key_ID, aws_secret_access_key=AWS_Secret_Access_Key, region_name=region)
# ec2 = boto3.resource('ec2',aws_access_key_id=AWS_Access_Key_ID, aws_secret_access_key=AWS_Secret_Access_Key, region_name=region)
except Exception as e:
print(e)
print("AWS CREDS ERROR, Exiting...")
exit()
def startInstances(instancesIds):
if(type(instancesIds) != list):
instancesIds = [instancesIds]
try:
response = ec2.start_instances(InstanceIds=instancesIds, DryRun=False)
print(response)
print("Instances Started")
except ClientError as e:
print(e)
print("Instances Failed to Start")
def stopInstances(instancesIds):
if(type(instancesIds) != list):
instancesIds = [instancesIds
]
try:
response = ec2.stop_instances(InstanceIds=instancesIds, DryRun=False)
print(response)
print("Instances Stopped")
except ClientError as e:
print(e)
print("Instances Failed to Stop")
def check():
for x in instances:
retry = 0
live = False
print("Checking Webiste " + instances[x])
while(retry < 5):
try:
r = requests.get(instances[x] ,verify=True)
if(r.status_code == 200):
live = True
break
except:
print("Not Live, retry time " + str(retry + 1))
print("Delaying request for " + str(DELAY_TIME) + " seconds...")
retry += 1
time.sleep(DELAY_TIME)
if(live):
print("Website is live")
# call function to start the ec2 instance
startInstances(x)
else:
# call function to stop the ec2 instance
print('Website is dead')
stopInstances(x)
print("")
def main():
check()
if __name__ == '__main__':
main()
I'm not sure if this is allowed, but I would like to have the client send a notification, receive a response and then finally send back a final validation message. The first send and receive seems to work fine, but the final .sendall() doesn't seem to send to the server.
Client:
import threading
import time
import socket
import sys
alarm_on = False # Flag to stop the thread
# The thread function
def beep():
while alarm_on:
print("BEEP BEEP BEEP")
time.sleep(20)
try:
mysock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
except socket.error:
print("Failed to create Movement Socket")
mysock.connect(('1.1.1.1',1234))
try:
mysock.sendall(b'MOVEMENT')
except socket.error:
print("Failed to send")
sys.exit()
#Recieve command to turn ignore, turn on alarm, or turn off alarm
try:
command = mysock.recv(10000)
print(command)
except socket.error:
print("Error receiving data")
sys.exit()
print("Command is: " + str(command))
#Turn on command
if command == b'ON':
state = command
alarm_on = True
# Start the thread
thrd1 = threading.Thread(target=beep).start()
mysock.sendall(state) # ********Final Validation to server of state
#Ignore the movement for 30 min
elif command == b'NO':
state = b'Silent for 15 min'
print(state)
mysock.sendall(state) # ********Final Validation to server of state
time.sleep(900)
Server
import socket
import sys
try:
mysock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
except socket.error:
print("Failed to create socket")
sys.exit
try:
mysock.bind(("",1234))
except:
print("Failed to bind")
mysock.listen(5)
while True:
validation = False
conn,addr = mysock.accept()
data = conn.recv(1000)
print("Data recieved: " + str(data))
if data == b'MOVEMENT':
while not validation:
command = input("Movement detected, type ON enable Alarm or NO to ignore: ")
command = command.upper()
if command == "ON" :
message = command
validation = True
elif command == "NO":
message = command
validation = True
else:
print("Data is: " + str(data) + "is not a valid input")
sys.exit()
try:
conn.sendall(bytes(message.encode()))
except:
print("Failed to send")
sys.exit()
conn.close()
mysock.close()
Can you do a final send after an initial send and receive? If so, why isn't my last sendall working?
In order to receive the second message, a second .recv() needs to be established to catch the "validation message". I added the following line to the server code:
validation = conn.recv(1000)
print(validation)
The full server code:
import socket
import sys
try:
mysock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
except socket.error:
print("Failed to create socket")
sys.exit
try:
mysock.bind(("",1234))
except:
print("Failed to bind")
mysock.listen(5)
while True:
validation = False
conn,addr = mysock.accept()
data = conn.recv(1000)
print("Data recieved: " + str(data))
if data == b'MOVEMENT':
while not validation:
command = input("Movement detected, type ON enable Alarm or NO to ignore: ")
command = command.upper()
if command == "ON" :
message = command
validation = True
elif command == "NO":
message = command
validation = True
else:
print("Data is: " + str(data) + "is not a valid input")
sys.exit()
try:
conn.sendall(bytes(message.encode()))
except:
print("Failed to send")
sys.exit()
validation = conn.recv(1000)
print(validation)
conn.close()
mysock.close()
I am trying to run the python code in the linux machine where the weblogic server is located whose health status needs to be known. I am passing username and password as per below code. Below is part of the python code:
import urllib2
import httplib
import getpass
import json
import os
import sys
import time
import math
def getServerJson(url, username, password):
req = urllib2.Request(url)
passman = urllib2.HTTPPasswordMgrWithDefaultRealm()
passman.add_password(None, url, username, password)
authhandler = urllib2.HTTPBasicAuthHandler(passman)
opener = urllib2.build_opener(authhandler)
try:
response = opener.open(url)
json_string = response.read()
if response.getcode() != 200:
print("")
print("Error connecting to WLS Server!")
print("HTTP Error code = " + str(response.getcode()))
print("URL = "+url)
sys.exit(1)
except urllib2.URLError as e:
print(e.reason)
writeHTMLOutputExcep(str(e.reason))
sys.exit(1)
except urllib2.HTTPError, e:
print(e.reason)
writeHTMLOutputExcep(str(e.reason))
sys.exit(1)
except httplib.HTTPException, e:
print(e.reason)
writeHTMLOutputExcep(str(e.reason))
sys.exit(1)
return json.loads(json_string)
But I am getting below error on giving input:
Please enter the WLS Server Details
Please enter the server and port: IP:PORT
Please enter the weblogic user name: weblogic
Please enter the weblogic user password:
[Errno 111] Connection refused
Please find below the url calling part of the code:
def getServerJson(url, username, password):
req = urllib2.Request(url)
passman = urllib2.HTTPPasswordMgrWithDefaultRealm()
passman.add_password(None, url, username, password)
authhandler = urllib2.HTTPBasicAuthHandler(passman)
opener = urllib2.build_opener(authhandler)
try:
response = opener.open(url)
json_string = response.read()
if response.getcode() != 200:
print("")
print("Error connecting to WLS Server!")
print("HTTP Error code = " + str(response.getcode()))
print("URL = "+url)
sys.exit(1)
except urllib2.URLError as e:
print(e.reason)
writeHTMLOutputExcep(str(e.reason))
sys.exit(1)
except urllib2.HTTPError, e:
print(e.reason)
writeHTMLOutputExcep(str(e.reason))
sys.exit(1)
except httplib.HTTPException, e:
print(e.reason)
writeHTMLOutputExcep(str(e.reason))
sys.exit(1)
return json.loads(json_string)
#Get a list of all of the Admin and Managed Servers in the domain
def getServers(wlsDomain,wlsServerUrl,username,password ):
url = 'http://'+wlsServerUrl+'/management/wls/latest/servers'
serverJson = getServerJson(url, username, password)
for server in serverJson["links"]:
if (server['rel'] != "parent"):
wlsDomain.addServer(WLSServer(server["title"], server["uri"]))
# Get the url of the list of logs, along with some other basic
# server information
def getLogListUrl(server, username, password):
serverJson = getServerJson(server.serverUrl, username, password)
server.setState(serverJson["item"]["state"])
if server.state == "running":
server.setHealth(serverJson["item"]["health"]["state"])
for link in serverJson["links"]:
if link["rel"] == "logs":
logListULR = link["uri"]
return logListULR
#For the given server, get the url for each server log
def getServerLogUrl(server,username,password, logListUrl):
logListJson = getServerJson(logListUrl, username, password)
for link in logListJson["links"]:
if link["rel"] == "items.name":
if not link["title"].startswith("JMSMessageLog") and \
not link["title"].startswith("HTTPAccessLog"):
server.addLog(WLLog(link["title"],link["uri"]))
#Go and find all server logs and read them, and take note of
#the error messages
def searchServerLogs(wlsDomain, username, password):
for server in wlsDomain.serverList:
#get the url to the list of logs for the given server
logListUrl = getLogListUrl(server, username, password)
#we can't get the log of a server that is not running
if server.state != "running":
continue
#get the url for each server log of the given server
getServerLogUrl(server,username,password, logListUrl)
for log in server.logList:
#we are not interested in the HTTPAccessLog
if log.name != "HTTPAccessLog":
if server.state != "running":
continue
startTime = time.time()
print("Reading " + server.name + " : " + log.name)
serverLogJson = getServerJson(log.logUrl, username, password)
for logEntry in serverLogJson["items"]:
if logEntry["severity"] == "Error":
log.addLogEntry(LogEnty(logEntry["severity"],logEntry["timeStamp"],logEntry["message"]))
server.incrementError()
endTime = time.time()
log.setDuration(formatTimeOutput(math.floor(endTime-startTime)))
def run():
print("")
print("")
print("Please enter the WLS Server Details")
print("-------------------------------------------")
print("")
wlsServerUrl = raw_input('Please enter the server and port e.g localhost:7001 ')
username = raw_input("Please enter the weblogic user name: ")
password = getpass.getpass("Please enter the weblogic user password: ")
print("")
wlsDomain = WLSDomain()
getServers(wlsDomain,wlsServerUrl,username,password)
searchServerLogs(wlsDomain,username,password)
outputStatisticsConsole(wlsDomain)
writeHTMLOutput(wlsDomain)
According to the official documentation, I have to authenticate with OAuth1 in order to use their API. I can't seem to get all the necessary part to authenticate. Here's my code so far:
#!usr/bin/env python
#encoding=utf-8
import requests
import sys, getopt
import urllib2
LOCALE = 'zh-CN'
LANGUAGE = 'zh-CN'
def doRequest(imageUrl):
reqUrlA = 'https://api.cloudsightapi.com/image_requests/' # get token
reqUrlB = 'https://api.cloudsightapi.com/image_responses/' # get the final recognition result with token
headers = {
'Authorization' : 'CloudSight INSERT API KEY',
'Host' : 'api.cloudsightapi.com',
'Origin:' : 'https://cloudsightapi.com'
}
postData = {
'image_request[remote_image_url]' : imageUrl,
'image_request[locale]': LOCALE,
'image_request[language]': LANGUAGE
}
try:
response = requests.post(reqUrlA, headers=headers, data=postData)
except Exception, e:
print 'Error: connection error, please check your Internet and confirm the image url'
sys.exit()
if "error" in response.json():
# print "Error: %s" % response.json()["error"]
print "无法识别图片:请检查图片的连接是否合法"
print
sys.exit()
else:
token = response.json()['token']
# you may get some response with status 'not completed' for about some times before getting the final result
reqTimes = 20
isNotified = True
while reqTimes > 0:
try:
response = requests.get(reqUrlB + token, headers=headers)
except Exception, e:
print 'Error: connection error, please check your Internet and confirm the image url'
sys.exit()
status = response.json()['status']
if status == 'completed':
print 'RESULT: '
print '\timage url:', imageUrl
print '\timage name:', response.json()['name']
print
# return response.json()['name']
break
elif status == 'not completed':
if isNotified == True:
print 'recognition in progress'
isNotified = False
reqTimes -= 1
def usage():
print '''
usage:
cloudSightAPI ImageURL fdgdfgrtrgd
type `cloudSightAPI -h` to get help
'''
def main():
try:
opts, args = getopt.getopt(sys.argv[1:], 'h')
for op, value in opts:
if op == '-h':
usage()
sys.exit()
if len(args) == 0:
usage()
sys.exit()
except getopt.GetoptError as e:
print 'Error: using invalid parameter -%s' % e.opt
usage()
sys.exit()
imageUrl = sys.argv[1]
doRequest(imageUrl)
if __name__ == '__main__':
main()
doRequest ("INSERT IMAGE URL")
According to the official documentation, I need to get the oauth_nonce and oauth_token in order to create a signature that would allow me use the api. How would I go about getting these details? Is there a oauth creator available?
Ended up doing it in Ruby. It was a lot easier!
https://github.com/cloudsight/cloudsight-ruby
Hope it will work sorry for the indentation dont forget to import requests
def quest(imageUrl):
fa=''
LOCALE = 'en-US'
LANGUAGE = 'en-US'
reqUrlB = 'https://api.cloudsightapi.com/image_responses/'
header = {
'Authorization' : 'CloudSight API_KEY_HERE',
'Host' : 'api.cloudsightapi.com',
'Origin:' : 'https://cloudsightapi.com'
}
footer = postData = {
'image_request[remote_image_url]' : imageUrl,
'image_request[locale]': LOCALE,
'image_request[language]': LANGUAGE
}
r = requests.post("https://api.cloudsightapi.com/image_requests",headers=header,data=footer)
print r
token = r.json()['token']
status='lol'
while True:
response = requests.get(reqUrlB + token, headers=header)
status = response.json()['status']
if status=='completed':
name=response.json()['name']
fa=name
break
return fa
import ldap
try:
l = ldap.initialize("ldap://ldap.xxxxx.com:389")
username=raw_input("Enter the username : ")
password = raw_input("Enter the password :")
if(username == "" or password==""):
print "Login Error : Username or password can't be blank"
else:
l.simple_bind(username,password)
print "Contact..."
except ldap.LDAPError, e:
print e
baseDn = "ou=active, ou=employees, ou=people, o=xxxxx.com";
searchScope = ldap.SCOPE_ONELEVEL
#retrieve all attributes
retrieveAttributes = None
search_query = raw_input("Enter the query :")
searchFilter = "cn="+search_query
try :
ldap_result_id = l.search(baseDn, searchScope, searchFilter, retrieveAttributes)
result_set = []
while 1:
result_type, result_data = l.result(ldap_result_id, 0)
if(result_data == []):
break
else:
if result_type == ldap.RES_SEARCH_ENTRY:
result_set.append(result_data)
#print result_set
print len(result_set)
except ldap.LDAPError, e:
print e
#print result_set[0]
The above code uses python-ldap to access ldap services. The result_set type is displayed as list but the number of items when using the len() function turns out to be zero. I need to perform operations on the retrieved string.