Writing packet information into text file - networking

i wrote the following code to output source address and destination address of all packets that are in a .pcap file to a text file using lua and tshark.
#!/usr/bin/lua
do
local file = io.open("luawrite", "w")
local function init_listener()
local tap = Listener.new("ipv6")
function tap.packet(pinfo, tvb)
local srcadd = pinfo.src
local dstadd = pinfo.dst
file:write(tostring(srcadd), "\t", tostring(dstadd)"\n")
end
end
end
I am running this script using the following command:
tshark -r wireless.pcap -xlua_script:MyScript.lua
Why is nothing being written in my text file? Is there something wrong on the code? Help is very much appreciated. Thanks!

Probably because you are missing a comma before "\n":
---------------------------------------------------vv-----
file:write(tostring(srcadd), "\t", tostring(dstadd), "\n")
It may be useful to check for file value returned by the open call.
I don't see any other problems with the script; if you still have issues, I have a page on debugging Wireshark Lua scripts that may help.

Related

Openstack API - Creating instances does not accept user-data = <bash script>

I am automating instance creation using OpenstackSDK and passing bash script with commands as userdata. But the script does not excute even though the instance is crated. When I do this manually via GUI, the bash scripts executes fine to the newly created instance.
#Reading bash script
with open('elk.sh', 'r') as f:
init_script = f.read()
server = conn.compute.create_server(
name=name,
image_id=IMAGE_ID,
flavor_id=FLAVOUR_ID,
networks=[{"uuid": NETWORK_ID}],
user_data=init_script, # pass script to the instance
key_name=KEY_PAIR
)
Note: Also tried to encode as Base64 file butstill failed with
is not JSON serializable.
Code snippet:
with open(USER_DATA,'r') as file:
f = file.read()
bytes_content = bytes(f,encoding='utf-8')
init_script = base64.b64encode(bytes_content)
Can anyone advice on this, please?
Thanks
Python3 handles string and binary differently. Also, to pass bash/cloud-config file to --user_data via OpenstackSDK, it has to be base46 encoded.
Code snippet:
with open(USER_DATA,'r') as file:
f = encodeutils.safe_encode(file.read().encode('utf-8'))
init_script = base64.b64encode(f).decode('utf-8')

Crystal lang how to get binary file from http

In Ruby:
require 'open-uri'
download = open('http://example.com/download.pdf')
IO.copy_stream(download, '~/my_file.pdf')
How to do the same in Crystal?
We can do the following:
require "http/client"
HTTP::Client.get("http://example.org") do |response|
File.write("example.com.html", response.body_io)
end
This writes just the response without any HTTP headers to the file. File.write is also smart enough to not download the entire file into memory first, but to write to the file as it reads chunks from the given IO.
I found something that works:
require "http/request"
require "file"
res = HTTP::Client.get "https://ya.ru"
fl=File.open("ya.html","wb")
res.to_io(fl)
fl.close

why nacl sdk contains so many 0 byte files?

I'm newbie to nacl. And I find out there are so many 0 byte files in the directory (nacl_sdk/pepper_38/toolchain/win_*/bin).
When I change the project platform to NaCl64 and compile(hello_nacl_cpp), there comes out an error
(error MSB6006: “D:\nacl_sdk\pepper_38\toolchain\win_x86_newlib\bin\x86_64-nacl-gcc.exe”已退出,代码为 -1)
But I can debug the example "hello_world_gles" with PPAPI platform, so I'm not sure the environment is ok.
Anyone can tell me something? Thanks!
Answer my question.
As #binji says we should use cygtar.py(which is in the dirctory sdk_tools) to extract the file.
Here we go:
Open cygtar.py with your text editor, you will find a class named CygTar who is the real worker.
Move dwon, and insert a snippet of code below Main function.
def MyLogic():
os.chdir('D:\\nacl_sdk\\sdk')
# tar = CygTar('naclports.tar.bz2', 'r', True) #here must use linux file path
tar = CygTar('naclsdk_win.tar.bz2', 'r', True)
tar.Extract()
Then replace sys.exit(Main(sys.argv)) with sys.exit(MyLogic()) at last of file.That all.
Note: If you have learned python, you will know code indent is very important in python, be careful.
And the final code should looks like this:

How do I convert my 5GB 1 liner file to lines based on pattern?

I have a 5GB 1 liner file with JSON data and each line starts from this pattern "{"created". I need to be able to use Unix commands on my Mac to convert this monster of a 1 liner into as many lines as it deserves. Any commands?
ASCII English text, with very long lines, with no line terminators
If you have enough memory you can open the file once with the TextWrangler application (the free BBEdit cousin) and use regular search/replace on the whole file. Use \r in replace to add a return. Will be very slow at opening the file, may even hang if low on memory, but in the end it may probably work. No scripting, no commands,.. etc.. I did this with big SQL files and sometimes it did the job.
You have to replace your line-start string with the same string with \n or \r or \r\n in front of it.
Unclear how it can be a “one liner” file but then each line starts with "{"created", but perhaps python -mjson.tool can help you get started:
cat your_source_file.json | python -mjson.tool > nicely_formatted_file.json
Piping raw JSON through ``python -mjson.tool` will cleanly format the JSON to be more human readable. More info here.
OS X ships with both flex and bison, you can use those to write a parser for your data.
You can use PHP as a shell command (if PHP is installed), just save a text file with name "myscript" and appropriate code (I cannot test code now, but the idea is as follows)
UNTESTED CODE
#!/usr/bin/php
<?php
$REPLACE_STRING='{"created'; // anything you like
// open input file with fopen() in read mode
$inFp=fopen('big_in_file.txt', "r");
// open output file with fopen() in write mode
$outFp=fopen('big_out_file.txt', "w+");
// while not end of file
while (!feof($inFp)) {
// read file chunks here with fread() in variable $chunk
$chunk = fread($inFp, 8192);
// do a $chunk=str_replace($REPLACE_STRING,"\r".$REPLACE_STRING; // to add returns
// (or use \r\n for windows end of lines)
$chunk=str_replace($REPLACE_STRING,"\r".$REPLACE_STRING,$chunk);
// problem: if chunk contains half the string at the end
// easily solved if $REPLACE_STRING is a one char like '{'
// otherwise test for fist char { in the end of $chunk
// remove final part and save it in a var for nest iteration
// write $chunk to output file
fwrite($outFp, $chunk);
// End while
}
?>
After you save it you must make it executable whith sudo chmod a+x ./myscript
and then launch it as ./myscript in terminal
After this, the myscript file is a full unix command

Why does SQLite say it can't read SQL from a file?

I have a bunch of SQL in a file, which creates the tables and so forth. The problem is that the .read command simply returns "can't open xxx" when I try to execute it. I've set the permissions to everybody read/write, but that didn't help. I can cat the file from the command line and see it fine.
This is under Mac OS 10.6.3.
Anybody have any idea here? Thanks!
Here's the source code in shell.c, where the sqlite3 utility executes the .read and tries to read the .sql file:
if( c=='r' && strncmp(azArg[0], "read", n)==0 && nArg==2 ){
FILE *alt = fopen(azArg[1], "rb");
if( alt==0 ){
fprintf(stderr,"can't open \"%s\"\n", azArg[1]);
}else{
process_input(p, alt);
fclose(alt);
}
}else
You can see that the utility will print "can't open xxx" only when the call to fopen fails. Pity they don't print errno for you, but this info should really help you narrow down the problem as not specific to sqlite.
You're either specifying the path wrong (try quoting it), you don't have permission, or the file really doesn't exist (meaning that there's something different in how you're using cat and .read, like the current directory is different).
Watch out for the ; at the end. .read does not like ;'s.

Resources