Community discussions

MikroTik App
 
karaYusuf
Frequent Visitor
Frequent Visitor
Topic Author
Posts: 54
Joined: Wed Oct 12, 2022 11:07 am

Changing an IP-Address

Mon Jan 23, 2023 3:13 pm

Hello Guys.
I wanted to write a script, which changes some ip-address values. I wanted to delete every char after the "/" in an ip-address.
As expample turning 192.168.1.100/24 to 192.168.1.100

I wrote the following code but can not get any output.
        :local Pstring 192.168.1.100/24
        :local cP;
        :local slash /;

            :for i from=0 to=([:len $Pstring] - 1) do={ 
                :local char [:pick $Pstring $i];
                :local oneAfter [:pick $Pstring [$i +1]];
                :local twoAfter [:pick $Pstring [$i +2]];
                :if ($char = $slash) do={

                    :set $char  "";
                    :set $oneAfter "";
                    :set $twoAfter "";
                }
                :set $cP ($cP . $char)
            }
            :put $cp;
What am I doing wrong?
Any help would be appreciated:)
 
User avatar
diamuxin
Member
Member
Posts: 319
Joined: Thu Sep 09, 2021 5:46 pm
Location: Alhambra's City

Re: Changing an IP-Address

Mon Jan 23, 2023 4:43 pm

Something like that?
{
:local ipAdd 192.168.1.100/24
:set ipAdd [:toip [:pick $ipAdd 0 [:find $ipAdd "/"]]]
:put $ipAdd
}
Output ==> 192.168.1.100
BR.
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11982
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Changing an IP-Address

Mon Jan 23, 2023 4:54 pm

@karaYusuf You do it on wrong way, and the topic title is completely inadequate.
{
:local lcidr 192.168.1.100/24
:local lip   [:pick $lcidr 0 [:find $lcidr "/"]]
:local lsub  [:pick $lcidr ([:find $lcidr "/"] + 1) [:len $lcidr]]

:put "$lcidr = $lip / $lsub"
}
Last edited by rextended on Mon Jan 23, 2023 4:54 pm, edited 1 time in total.
 
karaYusuf
Frequent Visitor
Frequent Visitor
Topic Author
Posts: 54
Joined: Wed Oct 12, 2022 11:07 am

Re: Changing an IP-Address

Mon Jan 23, 2023 4:54 pm

Something like that?
{
:local ipAdd 192.168.1.100/24
:set ipAdd [:toip [:pick $ipAdd 0 [:find $ipAdd "/"]]]
:put $ipAdd
}
Output ==> 192.168.1.100
BR.
Exactly what I needed. Thank you very much :)
 
karaYusuf
Frequent Visitor
Frequent Visitor
Topic Author
Posts: 54
Joined: Wed Oct 12, 2022 11:07 am

Re: Changing an IP-Address

Mon Jan 23, 2023 4:57 pm

@karaYusuf You do it on wrong way, and the topic title is completely inadequate.

I am sorry.
{
:local lcidr 192.168.1.100/24
:local lip   [:pick $lcidr 0 [:find $lcidr "/"]]
:local lsub  [:pick $lcidr ([:find $lcidr "/"] + 1) [:len $lcidr]]

:put "$lcidr = $lip / $lsub"
}
So which one should I use?
The one you wrote me or the other one?
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11982
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Changing an IP-Address

Mon Jan 23, 2023 5:01 pm

Both do the same, the second (my) do not uselessly use :toip‡ and containing also one example for extract subnet, if is needed,
and also the variable names are more adequate....

‡: depend on the rest of the script...

Who is online

Users browsing this forum: No registered users and 23 guests