Converting an ncat command on Windows from a Mac/Nix example - powershell

I'm working with the Google Healthcare API and there's a step in the walk through that uses netcat to send an HL7 message to the MLLP adapter.
(I used nmap to download ncat for Windows)
I have the adapter running locally but the command they provide is written for Mac/Nix users and I'm on Windows.
echo -n -e "\x0b$(cat hl7.txt)\x1c\x0d" | nc -q1 localhost 2575 | less
So I tried rewriting this for windows powershell:
$hl7 = type hl7.txt
Write-Output "-n -e \x0b" $hl7 "\x1c\x0d" | ncat -q1 localhost 2575 | less
When I try this, I get an error that "less" is invalid and also -q1 is also an invalid command.
If I remove -q1 and | less the command executes with no output or error message.
I'm wondering if I'm using ncat incorrectly here or the write-output incorrectly?
What is the -q1 parameter?
It doesn't seem to be a valid ncat parameter from what I've researched.
I've been following this walkthrough:
https://cloud.google.com/healthcare/docs/how-tos/mllp-adapter#connection_refused_error_when_running_locally

We're really converting the echo command, not the ncat command. The syntax for ascii codes is different in powershell.
[char]0x0b + (get-content hl7.txt) + [char]0x1c + [char]0x0d |
ncat -q1 localhost 2575
in ascii: 0b vertical tab, 1c file seperator, 0d carriage return http://www.asciitable.com
Or this. `v is 0b and `r is 0d
"`v$(get-content hl7.txt)`u{1c}`r" | ncat -q1 localhost 2575
If you want it this way, it's the same thing. All three ways end up being the same.
"`u{0b}$(get-content hl7.txt)`u{1c}`u{0d}" | ncat -q1 localhost 2575

Related

How do I filter from plain text output

I'm running wsl -l -v to get a list of WSL VMs on my computer and I get a list like this:
NAME STATE VERSION
Ubuntu-18-04 Stopped 2
Ubuntu-20-04 Running 2
I only want to see the ones that are running.
I tried:
wsl -l -v | Select-Object NAME
but I just get a list of blank lines.
just found this in --help docs
wsl -l --running
output:
Windows Subsystem for Linux Distributions:
Ubuntu (Default)
While your specific use-case can be handled by wsl --list --running (as mentioned by #X--FARZA_D--X's answer), there are two reasons why your filter wasn't working:
First, you probably were looking for Select-String Running. PowerShell's Select-Object would require a PowerShell object with a NAME property. All wsl.exe provides is a string output.
But more importantly, it still won't work even after the proper:
wsl -l -v | Select-String Running
This is due to a bug in wsl.exe that causes it output as a mangled UTF-16. See this and this answer for details.
Given your use-case, you should be able to properly filter with:
$console = ([console]::OutputEncoding)
[console]::OutputEncoding = New-Object System.Text.UnicodeEncoding
wsl -l -v | Select-String Running
[console]::OutputEncoding = $console
Alternatively, if you are using a recent release of WSL (0.64.0 or later) on Windows 11, you could simply:
$env:WSL_UTF8=1
wsl -l -v | Select-String Running

Piping commands in powershell to ssh not working

I'm trying to pipe commands to a host running a different OS via ssh. I need to send the commands as one string. Sending one at a time isn't an option. I can get this to work using quotes and newlines when I test on the ps cli. For example, sending 3 commands:
>Write-Output "Command1`nCommand2s`nCommand3`n" | ssh -tt user#host > out.txt
The out.txt file gets populated with my command output.
$ Command1
<output omitted>
$ Command2
<output omitted>
$ Command3
<output omitted>
When I try the same thing in ps script it doesn't work:
$cmds="`"Command1``nCommand2``nCommand3``n`""
Write-Output "commands to be sent:" $cmds
Write-Output $cmds | ssh -tt user#host > out.txt
The output I get shows that the string in $cmds is being formatted correctly as per the manual cli command:
commands to be sent:
"Command1`nCommand2`nCommand3`n"
But on my ssh host it's being interpreted as:
Error: command 'Command1`nCommand2`nCommand3`n' not recognized
Any idea why?
By escaping the $cmds string as you have you are literally sending the " and ` and n characters to the remote system. Did you try it as just:
$cmds="Command1`nCommand2`nCommand3`n"
This way the output from Write-Output "stuff" and $cmds="stuff" ; Write-Output $cmds would be the same.

Starting Point Hackthebox Error "Your port specifications are illegal"

I'm trying to scan the ports on the "Starting Point" CHallenge from Hackthebox.
i downloaded the .ovpn and established the vpn connnection in my Kali VM
typed in:
ports=$(nmap -p- --min-rate=1000 -T4 10.10.10.27 | grep ^[0-9] | cut -d '/' -f 1 | tr '\n' ',' | sed s/,$//)
but when i try
nmap -sC -sV -p$ports 10.10.10.27
I get the error message that my port specifications are illegal.
Happy for every help i can get!
My nmap scans worked on the first try. When I restarted my machine on another day, I had the same issue.
Re-download the connection pack.
This worked for me.

Check number of active meetings in Big Blue Button from command line

I want to check how many active meetings there are on the BBB server at any one time from the command line. I have tried
$ bbb-conf --network
but not getting anywhere. I have also checked the number of active connections to port 80 and 443
$ netstat -anp | grep :443 | grep ESTABLISHED | wc -l
but I'm not sure if I can trust that figure.
I know I can use the isMeetingRunning call from the API but I'm just looking for command line.
Any ideas would be appreciated
The following bash script, which can be run from command line on the same machine as the BigBlueButton server, will process the response to the BBB API getMeetings call.
#!/bin/bash
APICallName="getMeetings"
APIQueryString=""
X=$( bbb-conf --secret | fgrep URL: )
APIEndPoint=${X##* }
Y=$( bbb-conf --secret | fgrep Secret: )
Secret=${Y##* }
S=$APICallName$APIQueryString$Secret
Checksum=$( echo -n $S | sha1sum | cut -f 1 -d ' ' )
if [[ "$APIQueryString" == "" ]]
then
URL="${APIEndPoint}api/$APICallName?checksum=$Checksum"
else
URL="${APIEndPoint}api/$APICallName?$APIQueryString&checksum=$Checksum"
fi
wget -q -O - "$URL" | grep -o '<meetingID>' | wc -w
Tested on a live BBB machine.
Note:
The APICallName and APIQueryString can be modified to provide interface to other BBB API calls. See https://docs.bigbluebutton.org/dev/api.html
The command-line sha1sum will output a different result if a newline is appended to its input. This is the reason echo -n is used instead of echo.
In the last line, the script processes the XML output from the API call in a very naïve way, simply counting the number of occurences of the <meetingID> tag. More elaborate processing would probably require parsing the XML.

Passing piped commands via SSH

I'm using the Powershell module Posh-SSH to ssh into an Ubuntu server and run commands. I'm not having any difficulty passing simple lines such as:
$sshSession = New-SSHSession -ComputerName server001 -Credential $credential
$sshStream = New-SSHShellStream -Index $sshSession.sessionID
$sshStream.WriteLine("history")
$sshStream.Read()
The last line outputs exactly what it's supposed to. I want to run the following on the server:
for guest in `nova list --all-tenants --host serverName | grep Shutdown | awk '{ print $2 }'`; do nova start $guest; sleep 5; done"
Pasting this line right into $sshStream.WriteLine("") doesn't work at all as ` is an escape character in Powershell and $'s are used for variables already. I attempted to work around this by escaping some characters and putting it into a variable:
$block = "for guest in ``nova list --all-tenants --host server001 | grep Shutdown | awk '{ print `$2 }'`; do nova start `$guest; sleep 5; done"
$sshStream.WriteLine("$block")
$sshStream.WriteLine($block)
Both of my attempts above do not get read properly on the server. Any idea how I can work around this or if there's a better way to do this?
Thanks in advance
As TessellatingHeckler suggested, I used single-quotes and it worked:
$block = 'for guest in `nova list --all-tenants | grep Shutdown | awk ''{ print $2 }''`; do nova stop $guest; done'
$sshStream.WriteLine($block)
Try this
$block = #"
for guest in ``nova list --all-tenants --host server001 | grep Shutdown | awk '{ print `$2 }'`; do nova start `$guest; sleep 5; done
"#
$sshStream.WriteLine($block)
It will treat the "block" as a literal string (i.e. no escaping).