In Zeek (e.g. main.zeek), how can I use the variable from another script (e.g .sh file) with packet_source() or any function? - function

My command in the .sh file is running.
The command is: ($ZEEK -C -r $i dir)
i: pcap (file) name to be processed
dir: directory to be extracted
When the command is running, there are the extract files in desired location. It works pretty well. But I need that filename in the main.zeek. The question was that how can i access the filename in the main.zeek (used in the .sh file).
As I learned from here, packet_source() function could be called in script. But I can not implement it because I just started using it and I'm trying to get used to the script of Zeek.
In my script (main.zeek), after loading script index which contains packet_source() as the built-in function (#load base/bif/zeek.bif.zeek), how can i define a variable and use it (e.g global filename: function packet_source():, is it valid)?
I would be glad if you help.

In main.zeek, the variable could be defined as global to use in the every function that script has.
global filename_s: string;
After that, packet_source() is used to access the value. With its $path value, which file is read in there from PCAP would get. It should be placed in event zeek_init().
event zeek_init()
{
local filename_source = packet_source();
filename_s = filename_source$path;
}
That filename_s has the directory of the file Zeek read. It could be used in that script file (e.g. main.zeek).

Related

Converting a directory content into a json file

I have some files with .txt extension (in directory A) and I need to make a json file for this specific directory (A) from a different directory (B) in which the name and location of all the files should be included.
Do you know how I can do it with a bash script? (Then I want to use this json file for my data analysis).
I'm unsure how to do this entirely via a bash script, but if you have knowledge of Python, you could use the included os and json modules to list all the files in the directory, add them to a dictionary. Then create the JSON module using json.dumps(). Once you have written the script, you can invoke it via the bash shell.

Rename a file in Tcl

from the documentation it is unclear how to do so?
file rename ?-force? ?- -? source target
file rename ?-force? ?- -? source ?source ...? targetDir
The first form takes the file or directory specified by pathname
source and renames it to target, moving the file if the pathname
target specifies a name in a different directory.
I don't see variable called pathname in function declaration.
Given a path/to/file.csv
How can I rename it to path/to/renamedfile.csv ?
set oldName foobar.txt
set newName bar.txt
file rename $oldName $newName
fails with permission denied, i guess it has to do with the file being in C: how can this be done?
I don't see variable called pathname in function declaration.
Your interpretation of the documentation wrong. Pathname is a description of the source and target arguments in the function declaration.
The first form takes the file or directory specified by (pathname)
source and renames it to (pathname) target...
Your code for renaming foobar.txt into bar.txt is correct. Creating and apparently also renaming files directly under C: requires administrator priviliges. You can get it by opening the shell (tclsh) or program with administrator priviliges, e.g. right click on icon and select "Run as administrator".

How to pick path of file from System properties in jmeter CSV Data Set Config

I am trying to use CSV Data Set Config to get some data from csv file to be used in jmeter script but i don't want to hardcode the file path as it will be changing according to the test environment. Is there a way i can pick this path from System properties i.e some export set in my bashrc file.
Export in my bashrc :
export NIMBUS4_PERFORMANCE_TEST_REPO=/Users/rahul/Documents/verecloud/performancetest/data/user.csv
I would suggest the following workaround:
Change "Filename" setting of the CSV Data Set Config to following:
${__BeanShell(System.getenv().get("NIMBUS4_PERFORMANCE_TEST_REPO"))}
Where:
System.getenv() - method which provides access to underlying operating system environment variables
__Beanshell() - JMeter built-in function which allows executing of arbitrary Beanshell code
you could create a softlink at some static path. For example,
say we have created a soft link to /user/data/csvs folder.
You are in say ~/Documents , there run below
ln -s /user/data/csvs
Now we can access it in the jmeter and you will also have the flexibility to modify the softlink to point to some other location too.
Only constraint i see is the pointed directory name shouldn't change.
Hope this will help!!!
You can have just users.csv if the file is in the same folder as the .jmx itself;
You can have ${location}\users.csv
And in your UserDefinedVariables you'll have
and in non-gui mode you'll refer as
%RUNNER_HOME%\Test.jmx -Jloc=%RUNNER_HOME%\users.csv -Jusers=100 -Jloop=1 -Jrampup=5

Google Cloud Deployment Manager: Passing variables into templates

I'm using Google Cloud Deployment and I am trying to get external input into my template. Namely, I want to set a metadata variable on my instance (when creating the instance) but provide this value on execution.
I've tried:
gcloud deployment-manager deployments create test-api-backend --config test-api-backend.yaml --properties 'my_value=hello'
Which fails (The properties flag should only be used when passing in a template as your config file.)
I've tried:
my_value=hello gcloud deployment-manager deployments create test-api-backend --config test-api-backend.yaml
And use {{env['my_value']}} but the value isn't picked up.
I guess I could add the property in a .jinja file and re-write this file before I run everything, but it feels like a hack. That, or my idea of passing a variable from shell into Deploy Manager is a hack. I'm honestly not sure.
As the error message indicates, the command line properties can only be used with a template. They are essentially meant to replace the config yaml file.
The easiest thing to do is to just rename your yaml file to a .py or .jinja file. Then use that template as the file in the gcloud command instead of the yaml file.
In that new template file, add any defaults you would like if you don't pass them in on the command line.
For python, something like:
if 'myparam' in context.properties:
valuetouse = context.properities['myparam']
else:
valuetouse = mydefaultvalue
If the template uses another template then you'll also need to create a schema file for the new, top level template so you can do the imports there instead of the yaml file.
See the schema file in this github example.
https://github.com/GoogleCloudPlatform/deploymentmanager-samples/blob/master/examples/v2/igm-updater/ha-service.py.schema
If you want, you can ignore all the properties and just do the imports section.

Accessing variables in JSON from within Azure VM?

I'm using the Azure Powershell command New-AzureRmResourceGroupDeployment together with a JSON template file, and I'm feeding a bunch of parameters to the command that it will use with the JSON file. The JSON also instructs the newly-created VM to download a Powershell script from Azure storage and run it.
I need to pass some values from my Azure Powershell script to that "VM-local" Powershell script. For argument's sake, let's say that my Azure Powershell script has a variable $foo with a value of bar, representing "the name of a folder to be created on C:\ (so C:\bar)".
How?
How can a script running within the VM access the value bar (by any means)? It's fine if I need to use the JSON file as "messenger", or any other necessary trick. I don't think I can modify the "VM-local" Powershell script between downloading it from Azure storage and subsequently running it.
If you use the script extension in your JSON template on the VM to run the script, you can specify the entire cmd line for that script. On that cmd line you would pass parameters just as you would running it interactively. IOW, think about the cmdline to run that script and that's what you would put into the script extension of the template.
Take a look at this example:
https://github.com/Azure/azure-quickstart-templates/blob/f18a95e857a4caf86b4c2e77e652cec678cd524c/201-vm-winrm-windows/azuredeploy.json
Look at the "commandToExecute" property. You can see how to invoke powershell.exe with params, the script file being one of those params and then the script file itself also accepts some params through a variable.
You could also do this with DSC (very similar in JSON, but very different PS) but if you already have a PS script you want to use, this should work.
Is that what you needed?
You can pass variables like this:
New-AzureRmResourceGroupDeployment -Name ((Get-ChildItem $TemplateFile).BaseName + '-' + ((Get-Date).ToUniversalTime()).ToString('MMdd-HHmm')) `
-ResourceGroupName $ResourceGroupName `
-TemplateFile $TemplateFile -TemplateParameterObject #{accountName=$AutomationAccount;moduleName=$Name;moduleURI=$ModuleURI} -Force -Verbose