Showing posts with label python. Show all posts
Showing posts with label python. Show all posts

Monday, July 27, 2020

How To Post Codes In Blogs As Html Using Pygmentize


https://pygments.org/download/


For Command Line Help:-
>pygmentize -h
>pygmentize -H

To see all available lexer (language supported), formatter, etc ...
>pygmentize -L

To see all Options available for HTML formatter:-
>pygmentize -H formatter html

To convert lines of codes into html format so that u can post it to a blog:-
>cat report_waiverfile_errors.py | pygmentize -l python -f html -O noclasses


To include line numbers:-
>cat report_waiverfile_errors.py | pygmentize -l python -f html -O 'noclasses,linenos'

To Highlight certain lines:-
>cat report_waiverfile_errors.py | pygmentize -l python -f html -O 'noclasses,linenos,hl_lines="12 13 14 15 16 17"'

Example:-
1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
#!/usr/bin/env python

import os
import sys
#sys.path.insert(0, '/nfs/site/disks/da_infra_1/users/yltan/depot/da/infra/dmx/main/lib/python')
import logging
import dmx.tnrlib.waiver_file


LOGGER = logging.getLogger()


def main():
    wf = dmx.tnrlib.waiver_file.WaiverFile()
    wf.load_from_file(sys.argv[1])

if __name__ == "__main__":
    logging.basicConfig(format='[%(asctime)s] - %(levelname)s-[%(module)s]: %(message)s', level=logging.DEBUG)
    main()




Tuesday, October 11, 2016

How To Run A Script As The Owner Of The Script


For the explanation below, just keep in mind that these assumption is true:-
- owner of file == yltan(uid=742)
- user that executes the script == icetools(uid=48102)

How to run a python script as the owner of the script.

1. Create any simple python script, eg a.py:-
#!/usr/bin/env python
import os

print "Before swap::" + str(os.getresuid())
os.system("whoami")

os.setreuid(os.geteuid(),os.getuid())

print "After Swap:" + str(os.getresuid())
os.system("whoami")
2. This is the magic wrapper script, setuid_swap.c. :-
/* This binary is intended to be a setuid script wrapper for dbRsync.pl.
  
   Example compilation on an hp machine:  gcc dbRsync.c -o dbRsync
   This compilation creates an executable called dbRsync. After compiling,
   chmod +s dbRsync (or whatever the executable is) to set the sticky bit or
   it won't work.
*/

int main(int ac, char **av) {
    execv( "/path/to/your/a.py", av );
}
3. Compile the setuid_wrap.c to wrapper_a :-
gcc setuid_swap.c -o wrapper_a
4. Change the setuid bit:-
chmod 4755 wrapper_a
5. Run wrapper_a as someone else other than the owner of the file:-
sudo su - icetools

./wrapper_a
6. ... and this is the output that you get (Running as icetools):-
Before swap::(48102, 742, 742)
icetools
After Swap:(742, 48102, 48102)
yltan






For a detail explanation of how setuid work,

https://drive.google.com/open?id=0B_HHt58thGk_MTVwQkhleWNmZE0



Alternatively, you could swap the users in the compiled code itself by doing this:-
/* This binary is intended to be a setuid script wrapper for dbRsync.pl.
  
   Example compilation on an hp machine:  gcc dbRsync.c -o dbRsync
   This compilation creates an executable called dbRsync. After compiling,
   chmod +s dbRsync (or whatever the executable is) to set the sticky bit or
   it won't work.
*/

int main(int ac, char **av) {
    int uid;
    uid = geteuid();
    setreuid(uid, uid);
    execv( "/path/to/your/a.py", av );
}

Wednesday, May 22, 2013

Working With Perforce -G (Python Marshalled Object) Option

Most common pitfall is to directly get the output pirnted in stdout and marshal it:-

#!/usr/bin/env python
cmd = 'p4 -G changelists -m 3'
mo = subprocess.Popen(cmd, shell=True, stderr=subprocess.STDOUT, stdout=subprocess.PIPE).communicate()[0]
result = marshal.loads(mo)
pprint(result)

Nope. This won't work correctly.
You will only get the last data in the list of 3 items.

This is the correct way to do it:-

#!/usr/bin/env python
cmd = 'p4 -G changelists -m 3'
mo = subprocess.Popen(cmd, shell=True, stderr=subprocess.STDOUT, stdout=subprocess.PIPE)

result = []
try:
   while 1:
      output = marshal.load(mo.stdout)
      result.append(output)
except EOFError:
   pass
finally:
   mo.stdout.close()

pprint(result)

Tuesday, March 5, 2013

Reading/Writing xlsx File In Python

I'm using the python library openpyxl.
The below shows an example of how to iterate thru cells ...


#!/usr/bin/env python

import openpyxl
from pprint import pprint

wb = openpyxl.reader.excel.load_workbook('test.xlsx')
s = wb.get_sheet_by_name(name='Sheet1')

d = s.calculate_dimension()

r = s.range(d)
print "Range:" + str(r)
for row in r:
   for cell in row:
      print cell.get_coordinate()
      print cell.value
If you need help, u can use the python interactive session:-
%python
>>> import openpyxl
>>> help(openpyxl.reader.excel)
>>> wb = openpyxl.reader.excel.load_workbook('test.xlsx')
>>> help(wb)
>>> ws = wb.get_sheet_by_name(name='Sheet1')
>>> help(ws)
>>> cell = ws.cell(row=0, column=0)
>>> help(cell)

How To Bypass Kerberos(kinit) Authentication

Whenever you try to setuid and impersonate as someone else to run something, it is very likely that you will run into kerberos/kinit issues....