Community discussions

MikroTik App
 
jnegle
just joined
Topic Author
Posts: 3
Joined: Fri Feb 05, 2021 11:52 am

string to ip data type

Mon Feb 08, 2021 4:10 pm

Hello.

I am trying to update dns list with script. Problem is that the command asks for ip data type. But when I convert from string to ip the variable typeof becomes nil and it is empty ofcourse.
Here is the code:
 
 :local content
:local contentLen
:local lineEnd 0
:local line
:local lastEnd 0
:local comma
:local dnsName
:local dnsIp

:set content [/file get [/file find name=flash/test.csv] contents]
:set contentLen [:len $content]

:do {
	:set comma [:find $content "," $lastEnd ]
	:set lineEnd [:find $content "\n" $lastEnd ]
	#:put $lineEnd
	:set dnsName [:pick $content $lastEnd $comma]
	:set dnsIp [:pick $content ($comma+1) $lineEnd]
	#:put $dnsIp
                #:put $dnsName
	:set lastEnd ($lineEnd+1)
                #:put $lastEnd
	
	:if ( [ pick $dnsIp 0 1] !="#" ) do={
	
		:if ([:len $dnsIp] > 0 && [:len $dnsName] > 0) do={
	:put $dnsIp
                :put [:typeof $dnsIp]
                :set $dnsIp [:toip $dnsIp]                                                                        #Cconverting from string to ip
                :put [:typeof $dnsIp]                                                                                #here the dnsIp var is nil
                :put $dnsIp

		[/ip dns static add address=$dnsIp name=$dnsName]		}	
	}
	} while ($lineEnd < $contentLen)
From command line after execution :
127.0.0.1
str
nil

invalid value for argument address:
    invalid value for argument ip
    invalid value for argument ipv6

What am I missing?
Thank you!
 
2frogs
Forum Veteran
Forum Veteran
Posts: 713
Joined: Fri Dec 03, 2010 1:38 am

Re: string to ip data type  [SOLVED]

Tue Feb 09, 2021 4:54 am

Are you getting a blank space in your $dnsIp by chance?
As expected:
:local dnsIp "172.0.0.1"; :put "length=$[:len $dnsIp]"; :put "v
alue=$dnsIp"; :put "type=$[:typeof $dnsIp]"; :set $dnsIp [:toip $dnsIp]; :put "v
alue=$dnsIp"; :put "type=$[typeof $dnsIp]";
length=9
value=172.0.0.1
type=str
value=172.0.0.1
type=ip
Error:
:local dnsIp "172.0.0.1 "; :put "length=$[:len $dnsIp]"; :put "
value=$dnsIp"; :put "type=$[:typeof $dnsIp]"; :set $dnsIp [:toip $dnsIp]; :put "
value=$dnsIp"; :put "type=$[typeof $dnsIp]";
length=10
value=172.0.0.1 
type=str
value=
type=nil
 
jnegle
just joined
Topic Author
Posts: 3
Joined: Fri Feb 05, 2021 11:52 am

Re: string to ip data type

Thu Feb 11, 2021 3:26 pm

Yep, you were right, it had space after ip address.
Thank you!

Who is online

Users browsing this forum: alexantao and 16 guests