- Notifications
You must be signed in to change notification settings - Fork2
Add Script
To help to integrate your script in the WebScripts environment i develop a python package with a CLI.
Features:
- Get the log file path for your script (Python and CLI)
- Get the path of the WebScripts data to use the WebScripts databases (Python and CLI)
- Import the WebScripts upload manager (Python Only)
- Get the current user using your script (Python only)
Links:
Write inauth.sh
:
#!/bin/env bashif [$#-ne 2 ]thenecho'USAGE: auth.sh <username> <password>'exit 1fiif ["${1}"=="Admin" ]&& ["${2}"=="Admin" ]thenecho"{\"ip\":\"${REMOTE_IP}\",\"id\":\"2\",\"name\":\"Admin\",\"groups\":\"50,1000\"}"# you can add 'categories' and 'scripts' permissions and any custom attributes you need in your scripts and moduleselseecho"{\"ip\":\"${REMOTE_IP}\",\"id\":\"0\",\"name\":\"Not Authenticated\",\"groups\":\"0\"}"fiexit 0
Two methods can be used to configure a script, using aspecific configuration file or themain configuration file.
In themain configuration file:
{"scripts": {"auth.sh":"config_auth" },"config_auth": {"configuration_file":"/path/to/configuration/file.json" }}
In thespecific configuration file (named/path/to/configuration/file.json
):
{"script": {"launcher":"bash","category":"Authentication","args":"auth_args","description":"Authentication script."},"auth_args": {"username":"arg_username","password":"arg_password"},"arg_username": {"example":"username","html_type":"password","description":"Your username"},"arg_password": {"example":"password","html_type":"password","description":"Your password"}}
In amain configuration file:
{"scripts": {"auth.sh":"config_auth" },"config_auth": {"launcher":"bash","category":"Authentication","args":"auth_args","description":"Authentication script."},"auth_args": {"username":"arg_username","password":"arg_password"},"arg_username": {"example":"username","html_type":"password","description":"Your username"},"arg_password": {"example":"password","html_type":"password","description":"Your password"}}
Restart the server.
Sometimes you need to use a file uploaded to the WebScripts server in your python script or write a file and find it in the WebScripts uploads.
fromWebScripts.scripts.uploads.modules.uploads_managementimport (Upload,get_file,read_file,write_file,delete_file,get_file_content,get_visible_files,)frombase64importb64decode,b64encodewrite_file("\x00string content\xff",# if is binary you can use base64 or decode it with latin-1"my_filename.txt",# File name0,# Read access (0 == everyone can read it)0,# Write access (0 == everyone can write it)0,# Delete access (0 == everyone can delete it)False,# Hidden (if False, this file will be visible to other authenticated users)False,# Is binaryTrue,# Compress the fileFalse,# Is encoded as Base64with_access=True,# Check access to write this file (some scripts should write a file with an unauthenticated user))# Write a new file named "my_filename.txt"content2=b64encode(b'\x00version 2\xff').decode()filenames:List[str]= []forfileinget_visible_files():assertisinstance(file,Upload)filenames.append(file.name)assert"my_filename.txt"infilenames# file is not hidden and not deletedwrite_file(content2,# if is binary you can use base64 or decode it with latin-1"my_filename.txt",# File name0,# Read access (0 == everyone can read it)1000,# Write access (1000 == Admin can write it)1000,# Delete access (1000 == Admin can delete it)True,# HiddenTrue,# Is binaryTrue,# Compress the fileTrue,# Is encoded as Base64with_access=False,# Check access to write this file (some scripts should write a file with an unauthenticated user))# Write a new version of this filefilenames:List[str]= []forfileinget_visible_files():filenames.append(file.name)assert"my_filename.txt"notinfilenames# file is hiddenversions,counter=get_file("my_filename.txt")assertlen(versions)==2assertb64decode(read_file("my_filename.txt").encode())==b"\x00version 2\xff"# read_file check accessdata,filename=get_file_content(name="my_filename.txt")assertb64decode(data.encode())==b"\x00version 2\xff"# get_file_content don't check accessdelete_file("my_filename.txt")try:get_file_content(name="my_filename.txt")read_file("my_filename.txt")exceptFileNotFoundError:pass# a deleted file can't be read using the filenamedata,filename=get_file_content(id_="1")assertb64decode(data.encode())==b"\x00string content\xff"# get_file_content can read an old version (and deleted file)
Force to flush the output is required afterprint
,echo
...
- In Python scripts, add this two lines on the top of the script:
fromfunctoolsimportpartialprint=partial(print,flush=True)
alternately, you can add the flush argument on all of theprint
calls:
print("first output",flush=True)print("second output",flush=True)...
or usesys.stdout.flush
:
importsysprint("first output")sys.stdout.flush()sys.stdout.write("second output\n")sys.stdout.flush()...
- In Bash scripts, there is no impact.
- In PHP scripts, call the
flush
function afterecho
:
echo"first output";flush();echo"second output";flush();
- In Ruby scripts, add this line on the top of the script:
$stdout.sync=true
alternately, call the$stdout.flush
function after$stdout.print
orputs
orprint
:
$stdout.print"first output"$stdout.flushputs"second output"$stdout.flushprint"third output"$stdout.flush
- In Perl scripts, add this line on the top of the script:
local$| = 1;
or
STDOUT->autoflush(1)
alternately, callselect()->flush
function afterprint
:
print"first output";select()->flush();print"second output";select()->flush();