How to get IP address using q language - kdb

I am very new to q language and trying to get my IP address using system commands. I can get it in this format 127 0 0 1i using function
getIP:{"i"$0x0 vs .z.a}
But I need the answer to be in this format: 127.0.0.1.
Casting it to a symbol will not give me a dot format. So I must be using the wrong system command or wrong parsing!

You can use .z.a to get the ip address but it returns as an integer as you know. To get it in the format you want you can use
`$"." sv string 256 vs .z.a
where 256 vs .z.a turns it to the number base 256 and sv joins the returned numbers with a dot.
The ability to use a base with vs was added in v3.4 2015.12.13 and so won't work on an earlier version. You can check your version using .z.K. If you are using an earlier version then you can substitute 256 with "h"$0x0.
You can read more here: https://code.kx.com/q/ref/casting/#vs

Note that you can also make system calls to the machine to determine it's environment. Generally speaking anything you can find from a linux prompt you can also find in kdb via system calls.
q)system"hostname -i"
"12.345.6.789"

You can get the Hostname from .z.h , and pass the value from it get the IP address
`$"." sv string 256 vs .Q.addr .z.h

Related

Powershell - how to replace OS Version number with other OS Version (2008R2 becomes a 2016)

I need you'r help, I searched everywhere but couldn't find anything on the subject.
ACTUAL
A programm check my server version with this command :
[System.Environment]::OSVersion.Version.Major
6
The result is 6, this is perfectly good because it's a 2008R2 server.
EXCPECTED SITUATION
[System.Environment]::OSVersion.Version.Major
10
I need this same command to give a result equivalent to 10.
The objective is to deceive the program to make it believe that the server is a windows server 2016 but it is a windows 2008R2 server.
I don't need to just replace the string or other non persistant solution.
I need to change this value with a powershell script (or other non executable script) before launch the program that checks the OSversion.
The value is stored here, it seems to be directly related to the Common Language Runtime, i think i need to modify
So I think we should be able to modify a value in the CLR, but I don't know how to do it, and even if it is possible.
enter image description here

How do I decode xterm responses?

I'm building an application that uses pty.js to open up a pseudo terminal on my computer. I'm getting responses that look like:
]0;ec2-user#ip-172-31-62-237:~[?1034h[ec2-user#ip-172-31-62-237 ~]$ ls
]0;ec2-user#ip-172-31-62-237:~[ec2-user#ip-172-31-62-237 ~]$ pwd
/home/ec2-user
I'm assuming pty.js is sending back a specific encoding, but I'm not sure what the encoding is and how to decode it. Any help would be appreciated, thanks.
Those aren't responses (the terminal would respond), but control sequences sent by an application (not the terminal). I see a few instances (OSC might print as ^[], and CSI as ^[[ if the escape character were shown as ^[):
]0;ec2-user#ip-172-31-62-237:~
looks like the control for setting the window title (from xterm although several programs support it),
OSC Ps ; Pt BEL
OSC Ps ; Pt ST
...
Ps = 0 -> Change Icon Name and Window Title to Pt.
and
[?1034h
looks like another sequence from xterm's repertoire (generally not supported by other programs):
CSI ? Pm h
DEC Private Mode Set (DECSET).
...
Ps = 1 0 3 4 -> Interpret "meta" key, sets eighth bit.
(enables the eightBitInput resource).
For the given example, encoding isn't a factor.
For capturing output from your application, the script program is useful. I use a small utility (unmap) to translate the resulting typescript files into readable form, but cat -v is often adequate for this purpose.
Further reading: XTerm Control Sequences

How to run a disassembled code 6502?

I have to program in assembly the 6502.
I was forced to use the emulator Vice 128
I was told that the Commodore 128 is compatible with the instructions of 6502
I am a novice and I was made a practical demonstration but I did not understand anything.
There was an interface of 80 columns which passed with a command (which one?)
The instructions in machine language or assembly (the program)
were entered directly on this matrix of 80 columns.
Also the data are entered in this matrix.
So is this matrix the memory? Each line represents what?
I was told that this is disassembled code 6502. But I do not know what it means
I'm very confused
I want to run this simple program that
performs the sum of two numbers.
The two numbers are stored in the first page to the word zero and to the word one. I want to store the result in the second word of the first page.
I imagined that the first line contains 80 words. Is that right?
So I put here the data in hexadecimal (3 and 2).
$03 $02
LDA $00
ADC $01
STA $02
But I have a syntax error.
I hope someone can help me because it escapes me how things work.
Thanks in advance
Fir'st, in 6502, we use we deal with bytes, not words. (it's an 8 bit architecture)
You don't mention which macro assembler you are using, but I assume that its trying to interpret $03 as an op code, not data. I looked up two options
in ca65 you can use
.BYTE $03 $02
in dasm you use
HEX 03 02
In addition, 6502 has no concept of 80 anything (words, lines whatever). The only 80 I can think of is the old terminals that had 80 columns. I don't see how this is relevant here.
How to run a disassembled code 6502?
You have to assemble back the code.
Each 6502 instruction stands for 1, 2, or 3 bytes, the first is called the opcode, the optional second or third is the data used by the instruction (the operand).
You need a program to translate the instruction mnemonics to bytes. There were many such programs on the Commodore.
The Commodore 128 had a built-in monitor that let you enter instructions to assemble directly. You can enter it by typing MONITOR at the BASIC prompt. You would need to first set the address, then use "assemble" commands. Then use the "go" command at the starting address to run it. Use BASIC POKE command to set locations containing data, before you enter the monitor. The address 0B00 is a good address to use as it's the tape buffer which is unused except during tape I/O.
Good luck.

How can I evaluate a Q script in a running KDB server?

I have a large table that I want to partition loaded in a kdb+ server. The partitioning code is in a Q script that I need to evaluate on the server, but I don't have I/O control over it (the server is running in the background, data in memory was loaded through the C API).
So far the only solution I've found is to open a handle and convert all my statements to strings (all those \" I have to use look quite messy):
if[4 <> count .z.x; 0N!"usage: part_md.q host port db_dir date (YYYY.MM.DD)"; exit[1]]
arg_host: .z.x 0
arg_port: .z.x 1
arg_db_dir: .z.x 2
arg_date: "D"$(.z.x 3)
/get server handle
h:hopen `$(":",arg_host,":",arg_port)
set_db: "db: \":",arg_db_dir, "/mydb/\""
set_sym_path: "sym_path: \":",arg_db_dir,"\""
h set_db
h set_sym_path
/select fields to partition
h "mydb_select: select [-10] A,B,C from mydb"
/enumerate symbols
h "md_select_enum: .Q.en[`$sym_path] md_select"
h "delete md_select from `."
...
How can I evaluate my script directly without using a handle to the server on the same machine?
You could use a kdb IDE like qStudio http://www.timestored.com/qstudio to send the query by entering commands exactly as you would as if at the console.
Few other suggestions:
Read up on the function form of inter process of communication, an alternative to always sending strings. More powerful as it allows using objects.
Avoid underscores in variable names as they are also operators in kdb. (See here for q language guidelines: http://www.timestored.com/kdb-guides/q-coding-standards )
Checkout the many variations of the sv function, particularly (` sv `:file,`name) it allows quickly forming file paths in a platform independent way.
Regards,
Ryan
Another option is studioForKdb+ though it does not offer as many features as qstudio http://code.kx.com/wiki/StudioForKdb%2B

How to discover command line options (if any) for an undocumented executable of unknown origin?

Take an undocumented executable of unknown origin. Trying /?, -h, --help from the command line yields nothing. Is it possible to discover if the executable supports any command line options by looking inside the executable? Possibly reverse engineering? What would be the best way of doing this?
I'm talking about a Windows executable, but would be interested to hear what different approaches would be needed with another OS.
In linux, step one would be run strings your_file which dumps all the strings of printable characters in the file. Any constants chars will thus be shown, including any "usage" instructions.
Next step could be to run ltrace on the file. This shows all function calls the program does. If it includes getopt (or familiar), then it is a sure sign that it is processing input parameters. In fact, you should be able to see exactly what argument the program is expecting since that is the third parameter to the getopt function.
For Windows, you can see this question about decompiling Windows executables. It should be relatively easy to at least discover the options (what they actually do is a different story).
If it's a .NET executable try using Reflector. This will convert the MSIL code into the equivalent C# code which may make it easier to understand. Unfortunately private and local variable names will be lost, as these are not stored in the MSIL but it should still be possible to follow what's going on.