Mikrotik - result of script job as var is empty - router

I need to now some info about LTE/3G connection from output of command with Zabbix. Routerboard is 6.47.7 version.
[admin#Mikrotik_24] > interface lte info lte1 once
pin-status: ok
registration-status: registered
functionality: full
manufacturer: "MikroTik"
model: "R11e-LTE"
revision: "MikroTik_CP_2.160.000_v008"
current-operator: MTS
psc: 295
lac: 5205
current-cellid: 241903616
access-technology: 3G
session-uptime: 21h3m18s
imei: 355654090621868
imsi: 250016652966098
uicc: 89701011266529660988
earfcn: 10762
ecno: 0dB
rssi: -95dBm
For example "rssi", "access-technology", "uicc" and so on.
The problem with such command described by themselves - the data goes infinitely, and you cant catch it easy.
https://wiki.mikrotik.com/wiki/Manual:Scripting_Tips_and_Tricks#Get_values_from_looped_interactive_commands_like_.22monitor.22
So I have a script to get some values as a global var.
/interface lte info lte1 once do={:global at $"access-technology" }
and ":put $at" in terminal works fine. But I think to take result with SNMP by executing another script to show this generated var $at. It is possible with OID they did for "script result" purpose.
The question is how to output this var in script?
[admin#Mikrotik_24] > /system script export compact
# oct/31/2020 01:52:57 by RouterOS 6.47.7
# software id = PW8X-NNQ5
#
# model = RouterBOARD wAP R-2nD
/system script
add dont-require-permissions=no name=at owner=admin policy=\
ftp,reboot,read,write,policy,test,password,sniff,sensitive,romon source=\
"/interface lte info lte1 once do={:global at \$\"access-technology\" } \r\
\n"
add dont-require-permissions=no name=at_result owner=admin policy=\
ftp,reboot,read,write,policy,test,password,sniff,sensitive,romon source=":put \$at"
[admin#Mikrotik_24] > :environment print
at="3G"
[admin#Mikrotik_24] > /system script environment print
# NAME VALUE
0 at 3G
[admin#Mikrotik_24] > /system script run at_result
[admin#Mikrotik_24] >
You see, /system script run at_result is empty, but in terminal I can see it
[admin#Mikrotik_24] > :put $at
3G
[admin#Mikrotik_24] >

Ok. The right form of script var is
{
:global test
set $test "my data"
}
I have to declare it first.

Related

Powershell, modify TXT/XML file to add a string after a specific variable?

Trying to edit the DyanmoSettings.xml file without installing any specific packages on the Windows systems (sed, awk, etc)
to do:
need to modify this file;
%appdata%\dynamo\Dynamo Revit\2.3\DynamoSettings.xml
and within that file above, find this line;
>
> <CustomPackageFolders>
> <string>C:\Users\user1.mydomain\AppData\Roaming\Dynamo\Dynamo Revit\2.3</string>
> </CustomPackageFolders>
>
and add 'C:\Users%USERNAME%\OneDrive\DT-s\Revit Scripts\Dynamo\Custom Packages '
so it looks like this;
>
> <CustomPackageFolders>
> <string>C:\Users\user1.mydomain\AppData\Roaming\Dynamo\Dynamo Revit\2.3</string>
> <string>C:\Users\%USERNAME%\OneDrive\DT-s\Revit Scripts\Dynamo\Custom Packages </string>
> </CustomPackageFolders>
>
>
TIA!
tried using standard >> method in CMD but this didn't work.
Powershell uses c#. So any c# code can run inside powershell without any installation. Code below uses the c# library Xml Linq. Try following
using assembly System
using assembly System.Xml.Linq
$Filename = "c:\temp\test.xml"
$xDoc = [System.Xml.Linq.XDocument]::Load($Filename)
$customFolder = $xDoc.Descendants("CustomPackageFolders")
$newElement = [System.Xml.Linq.XElement]::new("string","C:\Users\%USERNAME%\OneDrive\DT-s\Revit Scripts\Dynamo\Custom Packages")
$customFolder.Add($newElement)
Write-Host "customFolder = " $customFolder
$xDoc.Save("c:\temp\test1.xml")
If that xml file looks anything like this
<DynamOconfig>
<CustomPackageFolders>
<string>C:\Users\user1.mydomain\AppData\Roaming\Dynamo\Dynamo Revit\2.3</string>
</CustomPackageFolders>
</DynamOconfig>
then
# load the settings file
$xml = [System.Xml.XmlDocument]::new()
$xml.Load("$env:APPDATA\dynamo\Dynamo Revit\2.3\DynamoSettings.xml")
# create a new <string> node
$newNode = $xml.CreateElement('string')
$newNode.InnerText = 'C:\Users\%USERNAME%\OneDrive\DT-s\Revit Scripts\Dynamo\Custom Packages'
# find the <CustomPackageFolders> node and append the new node to it
[void]$xml.SelectSingleNode('//CustomPackageFolders').AppendChild($newNode)
# save to (for safety NEW) file
$xml.Save("$env:APPDATA\dynamo\Dynamo Revit\2.3\NEW_DynamoSettings.xml")
Result:
<DynamOconfig>
<CustomPackageFolders>
<string>C:\Users\user1.mydomain\AppData\Roaming\Dynamo\Dynamo Revit\2.3</string>
<string>C:\Users\%USERNAME%\OneDrive\DT-s\Revit Scripts\Dynamo\Custom Packages</string>
</CustomPackageFolders>
</DynamOconfig>
P.S. If your intention is to have %USERNAME% interpolated in the string so it expands to your username, then create the new node's innertext like ths:
$newNode.InnerText = "C:\Users\$env:USERNAME\OneDrive\DT-s\Revit Scripts\Dynamo\Custom Packages"
(mind you need double-quotes now)

Setting output variable using newman / postman is getting cut off

I have an output variable siteToDeploy and siteToStop. I am using postman to run a test script against the IIS Administration API. In the test portion of one of the requests I am trying to set the azure devops output variable. Its sort of working, but the variable value is getting cut off for some reason.
Here is the test script in postman:
console.log(pm.globals.get("siteName"))
var response = pm.response.json();
var startedSite = _.find(response.websites, function(o) { return o.name.indexOf(pm.globals.get("siteName")) > -1 && pm.globals.get("siteName") && o.status == 'started'});
var stoppedSite = _.find(response.websites, function(o) { return o.name.indexOf(pm.globals.get("siteName")) > -1 && o.status == 'stopped'});
if(stoppedSite && startedSite){
console.log('sites found');
console.log(stoppedSite.id)
console.log('##vso[task.setvariable variable=siteToDeploy;]' + stoppedSite.id);
console.log('##vso[task.setvariable variable=siteToStop;]' + startedSite.id);
}
Here is the output form Newman:
Here is the output from a command line task echoing the $(siteToDeploy) variable. It's getting set, but not the entire value.
I've tried escaping it, but that had no effect. I also created a static command line echo where the variable is set and that worked fine. So I am not sure if it is a Newman issue or Azure having trouble picking up the varaible.
The issue turned our to be how Azure is trying to parse the Newman console log output. I had to add an extra Powershell task to replace the ' coming back from the Newman output.
This is what is looks like:
##This task is only here because of how Newman is writing out the console.log
Param(
[string]$_siteToDeploy = (("$(siteToDeploy)") -replace "'",""),
[string]$_siteToStop = (("$(siteToStop)") -replace "'","")
)
Write-Host ("##vso[task.setvariable variable=siteToDeploy;]{0}" -f ($_siteToDeploy))
Write-Host ("##vso[task.setvariable variable=siteToStop;]{0}" -f ($_siteToStop))

net-snmp perl subagent not being triggered by snmpget

I've been working on a custom SNMP Mib and I've come up against a wall while trying to get an agent to return the proper data.
MIB (validated by running smilint -l 6):
IDB-MIB DEFINITIONS ::= BEGIN
IMPORTS
MODULE-IDENTITY, OBJECT-TYPE, Integer32, enterprises
FROM SNMPv2-SMI
MODULE-COMPLIANCE, OBJECT-GROUP FROM SNMPv2-CONF;
idb MODULE-IDENTITY
LAST-UPDATED "201307300000Z" -- Midnight 30 July 2013
ORGANIZATION "*********"
CONTACT-INFO "email: *******"
DESCRIPTION "description"
REVISION "201307300000Z" -- Midnight 29 July 2013
DESCRIPTION "First Draft"
::= { enterprises 42134 }
iDBCompliance MODULE-COMPLIANCE
STATUS current
DESCRIPTION
"Compliance statement for iDB"
MODULE
GROUP testGroup
DESCRIPTION
"This group is a test group"
::= {idb 1}
test2 OBJECT-TYPE
SYNTAX Integer32
UNITS "tests"
MAX-ACCESS read-write
STATUS current
DESCRIPTION
"A test object"
DEFVAL { 5 }
::= { idb 3 }
testGroup OBJECT-GROUP
OBJECTS {
test2
}
STATUS current
DESCRIPTION "all test objects"
::= { idb 2 }
END
Agent file:
#!/usr/bin/perl
use NetSNMP::OID(':all');
use NetSNMP::agent(':all');
use NetSNMP::ASN(':all');
sub myhandler {
my ($handler, $registration_info, $request_info, $requests) = #_;
print "Handling request\n";
for ($request = $requests; $request; $request = $request->next()) {
#
# Work through the list of varbinds
#
my $oid = $request->getOID();
print "Got request for oid $oi\n";
if ($request_info->getMode() == MODE_GET) {
if ($oid == new NetSNMP::OID($rootOID . ".3")) {
$request->setValue(ASN_INTEGER, 2);
}
}
}
}
{
$subagent = 0;
print "Running new agent\n";
my $rootOID = ".1.3.6.1.4.1.42134";
my $regoid = new NetSNMP::OID($rootOID);
if (!$agent) {
$agent = new NetSNMP::agent('Name'=>'my_agent_name','AgentX' => 1);
$subagent = 1;
print "Starting subagent\n";
}
print "Registering agent\n";
$agent->register("my_agent_name", $regoid, \&myhandler);
print "Agent registered\n";
if ($subagent) {
$SIG{'INT'} = \&shut_it_down;
$SIG{'QUIT'} = \&shut_it_down;
$running = 1;
while ($running) {
$agent->agent_check_and_process(1);
}
$agent->shutdown();
}
}
sub shut_it_down() {
$running = 0;
print "Shutting down agent\n";
}
When I run the agent I get the following:
Running new agent
Starting subagent!
Registering agent with oid idb
Agent registered
So I know that much is working. However when I run the following command:
snmpget -v 1 -c mycommunity localhost:161 test2.0
I get this error message:
Error in packet
Reason: (noSuchName) There is no such variable name in this MIB.
Failed object: IDB-MIB::test2.0
I know from snmptranslate that the mib file is set correctly. I have even looked through the debug for snmpget (using -DALL) to make sure that the mib is being loaded and parsed correctly.
So my question is: Why is my subagent not being passed the request?
Update:
I've been told by #EhevuTov that my MIB file is not valid, however smilint does not report any issues and running snmpget -v 2c -c mycommunity localhost:161 .1.3.6.1.4.1.42134.3.0 does report the NAME of the object (IDB-MIB::test2.0) correctly, but does not find any data for it.
I am getting IDB-MIB::test2 = No Such Object available on this agent at this OID, which makes me think that my agent is not registering properly, however it's not throwing any errors.
Update 2:
I've been fiddling around with the agent code a bit. Based on the CPAN documentation (http://metacpan.org/pod/NetSNMP::agent), it looks like the $agent->register function call is supposed to return 0 if successful. So I checked the return code and got this:
Agent registered. Result: NetSNMP::agent::netsnmp_handler_registration=SCALAR(0x201e688)
Printing it out using Data::Dumper results in:
$VAR1 = bless( do{\(my $o = 34434624)}, 'NetSNMP::agent::netsnmp_handler_registration' );
I vaguely understand what bless does, but even so, I have no idea what this result is supposed to mean. So I'm starting to think that the agent is wrong somehow. Does anyone know how to debug these agents? Is there somewhere I can look to see if it's getting loaded properly into the master snmpd?
And I've solved the problem. It wasn't with the MIB, it was with the agent (which I had THOUGHT was working fine the whole time so I never bothered to check it).
I'd been running the agent stand-alone, because it seemed like it was working fine (never threw any errors when registering the handler). Apparently though, it needs to be run directly by snmpd.
I moved it to a directory that snmpd can access (because also apparently snmpd can't run scripts from /root, even though it's running as root), and added these lines in snmpd.conf:
perl print "\nRunning agents now\n";
perl do "/usr/share/snmp/agent.pl" || print "Problem running agent script: $!\n";
perl print "Agents run\n";
Note that these two lines were already present:
disablePerl false
perlInitFile /usr/share/snmp/snmp_perl.pl
I can now run the snmpget command and get the expected response.
> snmpget -v 2c -c mycommunity localhost:161 .1.3.6.1.4.1.42134.3
IDB-MIB::test2 = INTEGER: 2 tests

How to get list of TFS builds running at the moment from command line?

I'm trying to automate the deployment process, and as part of it, I need to run my release build from command line. I can do it, using command like
.\TFSBuild start http://server-name:8080/tfs/project-collection project-name build-name priority:High /queue
It even returns some code for the queued build — Build queued. Queue position: 2, Queue ID: 11057.
What I don't know, is how to get info about currently running builds, or about the state of my running build from powershell command line? The final aim is to start publishing after that build completes.
I've already got all necessary powershell scripts to create the deployment package from the build results, zip it, copy to production and install there. All I need now — to know when my build succeedes.
This function will wait for a build with the Queue ID given by TFSBuild.exe:
function Wait-QueuedBuild {
param(
$QueueID
)
[void][Reflection.Assembly]::LoadWithPartialName('Microsoft.TeamFoundation.Build.Client')
[void][Reflection.Assembly]::LoadWithPartialName('Microsoft.TeamFoundation.Client')
$uri = [URI]"http://server-name:8080/tfs/project-collection"
$projectCollection = [Microsoft.TeamFoundation.Client.TfsTeamProjectCollectionFactory]::GetTeamProjectCollection($uri)
$buildServer = $projectCollection.GetService([Microsoft.TeamFoundation.Build.Client.IBuildServer])
$spec = $buildServer.CreateBuildQueueSpec('*','*')
do {
$build = $buildServer.QueryQueuedBuilds($spec).QueuedBuilds| where {$_.Id -eq $QueueID}
sleep 1
} while ($build)
}
You can get the id returned by TFSBuild.exe, then call the function.
$tfsBuild = .\TFSBuild start http://server-name:8080/tfs/project-collection project-name build-name priority:High /queue
Wait-QueuedBuild [regex]::Match($tfsBuild[-1],'Queue ID: (?<id>\d+)').Groups['id'].Value
Using the work by E.Hofman available here it is possible to write a C# console app that uses TFS SDK and reveals if any build agent is currently running as follows:
using System;
using Microsoft.TeamFoundation.Build.Client;
using Microsoft.TeamFoundation.Client;
namespace ListAgentStatus
{
class Program
{
static void Main()
{
TfsTeamProjectCollection teamProjectCollection = TfsTeamProjectCollectionFactory.GetTeamProjectCollection(new Uri("http://TFSServer:8080"));
var buildServer = teamProjectCollection.GetService<IBuildServer>();
foreach (IBuildController controller in buildServer.QueryBuildControllers(true))
{
foreach (IBuildAgent agent in controller.Agents)
{
Console.WriteLine(agent.Name+" is "+agent.IsReserved);
}
}
}
}
}
The parameter .IsReserved is what toggles to 'True' during execution of a build.
I 'm sorry my powershell skills are not good enough for providing with a PS variant of the above. Please take a look here, where the work by bwerks might help you do that.
# load classes for execution
[Reflection.Assembly]::LoadWithPartialName("Microsoft.TeamFoundation.Build.Client") | Out-Null
[Reflection.Assembly]::LoadWithPartialName("Microsoft.TeamFoundation.Client") | Out-Null
# declare working variables
$Uri = New-Object System.Uri "http://example:8080/tfs"
# get reference to projection collection
$ProjectCollection = [Microsoft.TeamFoundation.Client.TfsTeamProjectCollectionFactory]::GetTeamProjectCollection($Uri)
# get reference to build server
$BuildServer = $ProjectCollection.GetService([Microsoft.TeamFoundation.Build.Client.IBuildServer])
# loop through the build servers
foreach($Controller in $BuildServer.QueryBuildControllers($true))
{
# loop through agents
foreach($BuildAgent in $Controller.Agents)
{
Write-Host "$($BuildAgent.Name) is $($BuildAgent.IsReserved)"
}
}

how to receive a vlc flow in a socket?

I'm developping a streaming application with tcl.
I have a vlc sever that broadcast a flow in http mode. what I'm trying to do is to develop a client who will try to connect to server with a particular ip adress and port number, and then try to save the flow in a file.
the code that i'm using is simple:
set server localhost
set sockChan [socket $server 1234]
set line [read $sockChan 1000]
puts " vidéo: $line"
close $sockChan
the problem when i try to test my script, I see that I realise the connection, but I can't read the flow!
the 'puts' doesn't show anything in the console...
Have you any ideas!
thank you..
If you're just wanting to save the contents of a URL to a file, the standard http package has a -channel option which lets you dump directly. For example:
package require http
set f [open video.dump w]
fconfigure $f -translation binary
set tok [http::geturl "http://server:port/url" -channel $f]
close $f
if {[http::ncode $tok] != 200} {
# failed somehow...
} else {
# succeeded
}
http::cleanup $tok
Edit: Doing it asynchronously (requires the event loop going, e.g. via vwait forever):
package require http
set f [open video.dump w]
fconfigure $f -translation binary
proc done {f tok} {
close $f
if {[http::ncode $tok] != 200} {
# failed somehow...
} else {
# succeeded
}
http::cleanup $tok
}
http::geturl "http://server:port/url" -channel $f -command "done $f"
# Your code runs here straight away...
Note that the code's recognizably similar, but now in a slightly different order! If you've got Tcl 8.5 — if not, why not? — then you can use a lambda application instead to make the apparent order of the code even more similar:
package require http
set f [open video.dump w]
fconfigure $f -translation binary
http::geturl "http://server:port/url" -channel $f -command [list apply {{f tok} {
close $f
if {[http::ncode $tok] != 200} {
# failed somehow...
} else {
# succeeded
}
http::cleanup $tok
}} $f]
# Your code runs here straight away...
Since you are working with HTTP, I would suggest looking at libcurl bindings for TCL.