def get_proc_etime(self,pid):
fmt = subprocess.getoutput("ps -A -opid,etime | grep '^ *%d ' | awk '{print $NF}'" % pid).strip()
if fmt == '':
return -1
parts = fmt.split('-')
days = int(parts[0]) if len(parts) == 2 else 0
fmt = parts[-1]
parts = fmt.split(':')
hours = int(parts[0]) if len(parts) == 3 else 0
parts = parts[len(parts)-2:]
minutes = int(parts[0])
seconds = int(parts[1])
return ((days * 24 + hours) * 60 + minutes) * 60 + seconds
# compute the billing val this running hour
# if isreal is True, it will also make users' beans decrease to pay for the bill.
# return the billing value in this running hour
python类getoutput()的实例源码
def extend_swap(size):
if size < 0:
(mem_free, mem_total) = system_manager.get_memory_sample()
size = (mem_total + mem_total // 8) // 1024
nid = 128
while subprocess.getoutput("cat /proc/swaps | grep cg-loop | awk '{print $1}' | awk -F\- '{print $NF}' | grep %d$" % nid) != "":
nid = nid + 1
start_time = time.time()
# setup
os.system('dd if=/dev/zero of=/tmp/cg-swap-%d bs=1G count=0 seek=%d >/dev/null 2>&1' % (nid, size))
os.system('mknod -m 0660 /dev/cg-loop-%d b 7 %d >/dev/null 2>&1' % (nid, nid))
os.system('losetup /dev/cg-loop-%d /tmp/cg-swap-%d >/dev/null 2>&1' % (nid, nid))
os.system('mkswap /dev/cg-loop-%d >/dev/null 2>&1' % nid)
success = os.system('swapon /dev/cg-loop-%d >/dev/null 2>&1' % nid) == 0
# detach
# os.system('swapoff /dev/cg-loop-%d >/dev/null 2>&1' % nid)
# os.system('losetup -d /dev/cg-loop-%d >/dev/null 2>&1' % nid)
# os.system('rm -f /dev/cg-loop-%d /tmp/cg-swap-%d >/dev/null 2>&1' % (nid, nid))
end_time = time.time()
return {"setup": success, "time": end_time - start_time }
def scan(network):
myip_subnet = subprocess.getoutput("/sbin/ip -o -f inet addr show | awk '/scope global/ {print $4}'")
nm.scan(hosts=myip_subnet, arguments='nmap -sn')
iplist = []
# add localhost
iplist.append({'ip_addr': '127.0.0.1', 'host': 'localhost'})
for host in nm.all_hosts():
try:
ip_a = (nm[host]['addresses']['ipv4'])
except KeyError:
ip_a = "[Unknown IP]"
try:
host_name = nm[host].hostname()
except KeyError:
host_name = "[Unknown hostname]"
iplist.append({'ip_addr': ip_a, 'host': host_name})
return iplist
def test_getoutput(self):
self.assertEqual(subprocess.getoutput('echo xyzzy'), 'xyzzy')
self.assertEqual(subprocess.getstatusoutput('echo xyzzy'),
(0, 'xyzzy'))
# we use mkdtemp in the next line to create an empty directory
# under our exclusive control; from that, we can invent a pathname
# that we _know_ won't exist. This is guaranteed to fail.
dir = None
try:
dir = tempfile.mkdtemp()
name = os.path.join(dir, "foo")
status, output = subprocess.getstatusoutput('cat ' + name)
self.assertNotEqual(status, 0)
finally:
if dir is not None:
os.rmdir(dir)
def hex_dump(hex_cmd):
"""
return hexdump in html formatted data
:param hex_cmd:
:return: str
"""
hex_string = getoutput(hex_cmd)
# Format the data
html_string = ''
hex_rows = hex_string.split('\n')
for row in hex_rows:
if len(row) > 9:
off_str = row[0:8]
hex_str = row[9:58]
asc_str = row[58:78]
asc_str = asc_str.replace('"', '"')
asc_str = asc_str.replace('<', '<')
asc_str = asc_str.replace('>', '>')
html_string += '<div class="row"><span class="text-info mono">{0}</span> ' \
'<span class="text-primary mono">{1}</span> <span class="text-success mono">' \
'{2}</span></div>'.format(off_str, hex_str, asc_str)
# return the data
return html_string
def test_getoutput(self):
self.assertEqual(subprocess.getoutput('echo xyzzy'), 'xyzzy')
self.assertEqual(subprocess.getstatusoutput('echo xyzzy'),
(0, 'xyzzy'))
# we use mkdtemp in the next line to create an empty directory
# under our exclusive control; from that, we can invent a pathname
# that we _know_ won't exist. This is guaranteed to fail.
dir = None
try:
dir = tempfile.mkdtemp()
name = os.path.join(dir, "foo")
status, output = subprocess.getstatusoutput('cat ' + name)
self.assertNotEqual(status, 0)
finally:
if dir is not None:
os.rmdir(dir)
def api_synced():
root_meta_dict = dict()
root_data_dict = dict()
root_links_dict = dict()
root_meta_dict["version"] = 1
root_meta_dict["name"] = "Synced"
root_meta_dict["state"] = "In Progress"
root_meta_dict["Underlying Command"] = "electrum is_synchronized"
synced = subprocess.getoutput(root_meta_dict["Underlying Command"])
root_data_dict["electrum_synced"] = synced
return jsonify(data=root_data_dict, meta=root_meta_dict, links=root_links_dict)
def git_info():
head, diff, remote = None, None, None
try:
head = subprocess.getoutput('git rev-parse HEAD').strip()
except subprocess.CalledProcessError:
pass
try:
diff = subprocess.getoutput('git diff --no-color')
except subprocess.CalledProcessError:
pass
try:
remote = subprocess.getoutput('git remote -v').strip()
except subprocess.CalledProcessError:
pass
git_dict = {'head': head or 'Unknown',
'diff': diff or 'Unknown',
'remote': remote or 'Unknown'}
return git_dict
def add_to_desktop(self, widget, desktopEntry):
try:
# Determine where the Desktop folder is (could be localized)
import subprocess
#sys.path.append('/usr/lib/ubuntu-mate/common')
from configobj import ConfigObj
config = ConfigObj(GLib.get_home_dir() + "/.config/user-dirs.dirs")
desktopDir = GLib.get_home_dir() + "/Desktop"
tmpdesktopDir = config['XDG_DESKTOP_DIR']
tmpdesktopDir = subprocess.getoutput("echo " + tmpdesktopDir)
if os.path.exists(tmpdesktopDir):
desktopDir = tmpdesktopDir
# Copy the desktop file to the desktop
os.system("cp \"%s\" \"%s/\"" % (desktopEntry.desktopFile, desktopDir))
os.system("chmod a+rx %s/*.desktop" % (desktopDir))
except Exception as detail:
print (detail)
def get_workspaces(self):
Workspaces = namedtuple('Workspaces', 'horz vert total')
dconf_horz = 'dconf read /org/compiz/profiles/unity/plugins/core/hsize'
dconf_vert = 'dconf read /org/compiz/profiles/unity/plugins/core/vsize'
workspaces_horz = subprocess.getoutput(dconf_horz)
workspaces_vert = subprocess.getoutput(dconf_vert)
# If workspace changer hasn't been enabled h- and v-size doesn't seem to be set.
if not workspaces_horz:
self.logger.debug("unity/plugins/core/hsize not set - setting horisontal workspaces to '1'")
workspaces_horz = '1'
if not workspaces_vert:
self.logger.debug("unity/plugins/core/vsize not set - setting vertical workspaces to '1'")
workspaces_vert = '1'
workspaces_total = int(workspaces_vert) * int(workspaces_horz)
self.logger.debug("Horisontal number of workspaces is: " + str(workspaces_horz))
self.logger.debug("Vertical number of workspaces is: " + str(workspaces_vert))
self.logger.debug("Total number of workspaces is: " + str(workspaces_total))
workspaces = Workspaces(
int(workspaces_horz),
int(workspaces_vert),
workspaces_total)
return workspaces
def test_getoutput(self):
self.assertEqual(subprocess.getoutput('echo xyzzy'), 'xyzzy')
self.assertEqual(subprocess.getstatusoutput('echo xyzzy'),
(0, 'xyzzy'))
# we use mkdtemp in the next line to create an empty directory
# under our exclusive control; from that, we can invent a pathname
# that we _know_ won't exist. This is guaranteed to fail.
dir = None
try:
dir = tempfile.mkdtemp()
name = os.path.join(dir, "foo")
status, output = subprocess.getstatusoutput(
("type " if mswindows else "cat ") + name)
self.assertNotEqual(status, 0)
finally:
if dir is not None:
os.rmdir(dir)
def run(self, edit, query):
## get the contents of the tab that is the focus in sublime
allcontent = sublime.Region(0, self.view.size())
## convert to string
allcontent = self.view.substr(allcontent)
## get a unique string for use in the tmp files
unique_filename = str(uuid.uuid4())
## open db and query files in /tmp and write allcontent (i.e. the 'page' open, with the sublime console at the bottom)
db_handle = '/tmp/%s.sublime_blast.tmp_db.fa' % unique_filename
with open(db_handle, 'w') as fo:
fo.write(allcontent)
query_handle = '/tmp/%s.sublime_blast.tmp_query.fa' % unique_filename
with open(query_handle, 'w') as fo:
fo.write('>query\n%s\n' % query)
## make a blastdb of the page open in sublime
subprocess.call(['/usr/local/bin/makeblastdb', '-dbtype', 'nucl', '-in', '%s' % db_handle])
## run blast, taking the query as what is input in the console
blast_output = subprocess.getoutput(['/usr/local/bin/blastn -db {0} -query {1} -outfmt 6'.format(db_handle, query_handle)])
## add the headers and the blast output to the end of the window of focus
## self.view.size() is where you want to insert the text, in this case, at the end of the file so we use self.view.size() which is the total size
## edit is a token which classes the insert as a single edit which can e.g. be undone with one 'undo'
self.view.insert(edit, self.view.size(), '\nqseqid sseqid pident length mismatch gapopen qstart qend sstart send evalue bitscore\n')
self.view.insert(edit, self.view.size(), blast_output)
def __get_remote_index():
"""
Gets the index of news crawl files from commoncrawl.org and returns an array of names
:return:
"""
# cleanup
subprocess.getoutput("rm tmpaws.txt")
# get the remote info
cmd = "aws s3 ls --recursive s3://commoncrawl/crawl-data/CC-NEWS/ --no-sign-request > .tmpaws.txt && " \
"awk '{ print $4 }' .tmpaws.txt && " \
"rm .tmpaws.txt"
__logger.info('executing: %s', cmd)
stdout_data = subprocess.getoutput(cmd)
lines = stdout_data.splitlines()
return lines
def __get_remote_index(self):
"""
Gets the index of news crawl files from commoncrawl.org and returns an array of names
:return:
"""
# cleanup
subprocess.getoutput("rm tmpaws.txt")
# get the remote info
cmd = "aws s3 ls --recursive s3://commoncrawl/crawl-data/CC-NEWS/ --no-sign-request > tmpaws.txt && " \
"awk '{ print $4 }' tmpaws.txt && " \
"rm tmpaws.txt"
self.__logger.info('executing: %s', cmd)
stdout_data = subprocess.getoutput(cmd)
print(stdout_data)
lines = stdout_data.splitlines()
return lines
def test_getoutput(self):
self.assertEqual(subprocess.getoutput('echo xyzzy'), 'xyzzy')
self.assertEqual(subprocess.getstatusoutput('echo xyzzy'),
(0, 'xyzzy'))
# we use mkdtemp in the next line to create an empty directory
# under our exclusive control; from that, we can invent a pathname
# that we _know_ won't exist. This is guaranteed to fail.
dir = None
try:
dir = tempfile.mkdtemp()
name = os.path.join(dir, "foo")
status, output = subprocess.getstatusoutput(
("type " if mswindows else "cat ") + name)
self.assertNotEqual(status, 0)
finally:
if dir is not None:
os.rmdir(dir)
def get_our_pids(this_base):
''' Runs ps ax, returns the list of PIDs whose comand starts with "/res_binaries/ '''
# Get the id of the running box; Vagrant should accept the name but doesn't; https://github.com/mitchellh/vagrant/issues/8691
id_of_base = ""
for this_line in (subprocess.getoutput("vagrant global-status")).splitlines():
if this_base in this_line:
(id_of_base, _) = this_line.split(" ", 1)
break
if not id_of_base:
die("No id could be found for {}.".format(this_base))
this_command = "vagrant ssh --command \"ps ax o pid,args --no-headers\" {}".format(id_of_base)
this_ps = subprocess.getoutput(this_command)
pids_to_return = []
for this_line in this_ps.splitlines():
(this_pid, this_cmd) = (this_line.strip()).split(" ", 1)
if this_cmd.startswith("/res_binaries/"):
pids_to_return.append((this_pid, this_cmd))
return pids_to_return
def detect(hostname):
"""
Performs CDN detection through the DNS, using the nslookup command.
Parameters
----------
hostname : str
Hostname to assess
"""
print('[+] DNS detection\n')
hostname = urlparse.urlparse(hostname).netloc
regexp = re.compile('\\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\\b')
out = commands.getoutput("host " + hostname)
addresses = regexp.finditer(out)
for addr in addresses:
CDNEngine.find(commands.getoutput('nslookup ' + addr.group()))
def detect(hostname):
"""
Performs CDN detection through whois command's.
Parameters
----------
hostname : str
Hostname to assess
"""
print('[+] Whois detection\n')
hostname = urlparse.urlparse(hostname).netloc
out = commands.getoutput("whois " + hostname)
CDNEngine.find(out.lower())
def detect(hostname):
"""
Performs CDN detection by trying to access the cdn subdomain of the
specified hostname.
Parameters
----------
hostname : str
Hostname to assess
"""
print('[+] CDN subdomain detection\n')
hostname = "cdn." + urlparse.urlparse(hostname).netloc
out = commands.getoutput("host -a " + hostname)
CDNEngine.find(out.lower())
def detect(hostname):
"""
Performs CDN detection thanks to information disclosure from server error.
Parameters
----------
hostname : str
Hostname to assess
"""
print('[+] Error server detection\n')
hostname = urlparse.urlparse(hostname).netloc
regexp = re.compile('\\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\\b')
out = commands.getoutput("host " + hostname)
addresses = regexp.finditer(out)
for addr in addresses:
res = request.do('http://' + addr.group())
if res is not None and res.status_code == 500:
CDNEngine.find(res.text.lower())
def getcommands(helpcmd):
"""Recursively crawl all subgroups, starting from specified help command.
Passed command assumed to end with -h. For example, 'az vm -h'
"""
indentation = 4 * (len(helpcmd.split(' ')) - 3)
print(indentation*' ' + helpcmd)
stdoutdata = subprocess.getoutput(helpcmd) # capture help command output
subgroups = False # flag to track whether inside the subgroup section
for line in stdoutdata.split('\n'):
if line.strip() == 'Subgroups:':
subgroups = True # found start of subgroup section
continue # skip the 'Subgroups:' line
if not subgroups:
continue # skip all lines before subgroup section
if subgroups and (not line.strip() or line.lstrip() == line):
break # blank or non-indented line is end of subgroup section
subhelp = subcommand(helpcmd, line) # create help command for subgroup
getcommands(subhelp) # enumerate help commands for this subgroup
def _deSubstPath(path):
"""desubstitude craft short path"""
if not OsDetection.isWin():
return path
drive, tail = os.path.splitdrive(path)
drive = drive.upper()
if CraftStandardDirs._SUBST == None:
tmp = subprocess.getoutput("subst").split("\n")
CraftStandardDirs._SUBST = {}
for s in tmp:
if s != "":
key, val = s.split("\\: => ")
CraftStandardDirs._SUBST[key] = val
if drive in CraftStandardDirs._SUBST:
deSubst = CraftStandardDirs._SUBST[drive] + tail
return deSubst
return path
def __init__(self,test=False):
global laststopcpuval
global workercinfo
threading.Thread.__init__(self)
self.thread_stop = False
self.interval = 2
self.billingtime = 3600 # billing interval
self.test = test
self.cpu_last = {}
self.cpu_quota = {}
self.mem_quota = {}
self.net_stats = {}
self.cores_num = int(subprocess.getoutput("grep processor /proc/cpuinfo | wc -l"))
containers = self.list_container()
for container in containers: # recovery
if not container == '':
try:
vnode = VNode.query.get(container)
laststopcpuval[container] = vnode.laststopcpuval
laststopruntime[container] = vnode.laststopruntime
workercinfo[container] = {}
workercinfo[container]['basic_info'] = {}
workercinfo[container]['basic_info']['billing'] = vnode.billing
workercinfo[container]['basic_info']['billing_history'] = get_billing_history(container)
workercinfo[container]['basic_info']['RunningTime'] = vnode.laststopruntime
workercinfo[container]['basic_info']['a_cpu'] = a_cpu
workercinfo[container]['basic_info']['b_mem'] = b_mem
workercinfo[container]['basic_info']['c_disk'] = c_disk
workercinfo[container]['basic_info']['d_port'] = d_port
except:
laststopcpuval[container] = 0
laststopruntime[container] = 0
return
# list containers on this worker
def user_live_add(form, args):
if not os.path.exists('/var/lib/docklet/global/users/%s' % form['user']):
return False
subprocess.getoutput('echo live > /var/lib/docklet/global/users/%s/status' % form['user'])
return True
# curl -L -X POST -F user=docklet http://0.0.0.0:1728/v1/user/live/remove
def user_live_remove(form, args):
subprocess.getoutput('rm -f /var/lib/docklet/global/users/%s/status' % form['user'])
return True
# curl -L -X POST http://0.0.0.0:1728/v1/user/live/list
def user_live_list(form, args):
return subprocess.getoutput('ls -1 /var/lib/docklet/global/users/*/status 2>/dev/null | awk -F\/ \'{print $(NF-1)\'}').split()
def get_cpu_sample():
[a, b, c, d] = subprocess.getoutput("cat /proc/stat | grep ^cpu\ | awk '{print $2, $3, $4, $6}'").split()
cpu_time = int(a) + int(b) + int(c) + int(d)
return (cpu_time, time.time())
def get_memory_sample():
mem_free = int(subprocess.getoutput("awk '{if ($1==\"MemAvailable:\") print $2}' /proc/meminfo 2>/dev/null")) // 1024
mem_total = int(subprocess.getoutput("awk '{if ($1==\"MemTotal:\") print $2}' /proc/meminfo 2>/dev/null")) // 1024
return (mem_free, mem_total)
def get_swap_sample():
swap_free = int(subprocess.getoutput("awk '{if ($1==\"SwapFree:\") print $2}' /proc/meminfo 2>/dev/null")) // 1024
swap_total = int(subprocess.getoutput("awk '{if ($1==\"SwapTotal:\") print $2}' /proc/meminfo 2>/dev/null")) // 1024
return (swap_free, swap_total)
def get_system_loads():
if 'last_cpu_sample' not in system_manager.__dict__:
system_manager.last_cpu_sample = system_manager.get_cpu_sample()
time.sleep(1)
cpu_sample = system_manager.get_cpu_sample()
(mem_free, mem_total) = system_manager.get_memory_sample()
(swap_free, swap_total) = system_manager.get_swap_sample()
ncpus = int(subprocess.getoutput("grep processor /proc/cpuinfo | wc -l"))
cpu_free = ncpus - (cpu_sample[0] - system_manager.last_cpu_sample[0]) * 0.01 / (cpu_sample[1] - system_manager.last_cpu_sample[1])
cpu_free = 0.0 if cpu_free <= 0.0 else cpu_free
system_manager.last_cpu_sample = cpu_sample
return {"mem_free": mem_free, "mem_total": mem_total, "swap_free": swap_free, "swap_total": swap_total, "cpu_free": cpu_free, "cpu_total": ncpus }