In this scenario How can I Find The Public Ip behind the modem By mikrotik script ?
Users<-->Switch<-->Mikrotik<--------->Modem<----?--->Internet
*Modem Get Dynamic And Public IP From Internet.
*Mikrotik Get Local IP From Modem By DHCP.
{ /tool fetch url="http://myip.dnsomatic.com/" mode=http dst-path=mypublicip.txt local ip [file get mypublicip.txt contents ] put $ip }
# Set needed variables :global extinterface "ether1-gateway" :global ExtIpListName "external-ip" :global extip "" :global oldextip "" # Grab the current IP address on that interface. :local extip2 [/ip address get [/ip address find interface=$extinterface ] address]; :set extip [:pick $extip2 0 [:find $extip2 "/"]]; :log info "Current external IP = $extip" :if ([:len [/ip firewall address-list find list=$ExtIpListName]] > 0) do={ :set oldextip [/ip firewall address-list get [/ip firewall address-list find list=$ExtIpListName] address]; :if ($oldextip != $extip) do={ /ip firewall address-list set [/ip firewall address-list find list=$ExtIpListName address=$oldextip] address=$extip :log info "External IP relpace from $oldextip to $extip" } else={ :log info "External IP not changed" }; } else={ /ip firewall address-list add list=$ExtIpListName address=$extip :log info "New external IP added: $extip" };Copy to terminal:
add name=Refresh_External_IP policy=ftp,read,write,test,winbox,api source="# S\ et needed variables\r\ \n:global extinterface \"ether1-gateway\"\r\ \n:global ExtIpListName \"external-ip\"\r\ \n:global extip \"\"\r\ \n:global oldextip \"\"\r\ \n\r\ \n# Grab the current IP address on that interface.\r\ \n:local extip2 [/ip address get [/ip address find interface=\$extinterfac\ e ] address];\r\ \n:set extip [:pick \$extip2 0 [:find \$extip2 \"/\"]];\r\ \n:log info \"Current external IP = \$extip\"\r\ \n\r\ \n:if ([:len [/ip firewall address-list find list=\$ExtIpListName]] > 0) d\ o={\r\ \n :set oldextip [/ip firewall address-list get [/ip firewall address-li\ st find list=\$ExtIpListName] address];\r\ \n :if (\$oldextip != \$extip) do={\r\ \n /ip firewall address-list set [/ip firewall address-list find list\ =\$ExtIpListName address=\$oldextip] address=\$extip\r\ \n :log info \"External IP relpace from \$oldextip to \$extip\"\r\ \n } else={\r\ \n :log info \"External IP not changed\"\r\ \n };\r\ \n} else={\r\ \n /ip firewall address-list add list=\$ExtIpListName address=\$extip\r\ \n :log info \"New external IP added: \$extip\"\r\ \n};\r\ \n"Use as is free!
.../myrosip.php?who=<<SOURCE_LABEL>>" mode=http dst-path=mypublicip.txt
2016-02-27 10:30:03 Error xx.xx.128.85 403 GET /myrosip.php?who=Fantastic ROS HTTP/1.0 Mikrotik/6.x Fetch 462 Apache access
2016-02-27 10:30:03 Error xx.xx.128.85 ModSecurity: [file "/etc/httpd/crs/activated_rules/modsecurity_crs_20_protocol_violations.conf"] [line "52"] [id "960911"] [rev "2"] [msg "Invalid HTTP Request Line"] [data "GET /myrosip.php?who=Fantastic ROS HTTP/1.0"] [severity "WARNING"] [ver "OWASP_CRS/2.2.8"] [maturity "9"] [accuracy "9"] [tag "OWASP_CRS/PROTOCOL_VIOLATION/INVALID_REQ"] [tag "CAPEC-272"] Warning. Match of "rx ^(?i:(?:[a-z]{3,10}\\\\s+(?:\\\\w{3,7}?://[\\\\w\\\\-\\\\./]*(?::\\\\d+)?)?/[^?#]*(?:\\\\?[^#\\\\s]*)?(?:#[\\\\S]*)?|connect (?:\\\\d{1,3}\\\\.){3}\\\\d{1,3}\\\\.?(?::\\\\d+)?|options \\\\*)\\\\s+[\\\\w\\\\./]+|get /[^?#]*(?:\\\\?[^#\\\\s]*)?(?:#[\\\\S]*)?)$" against "REQUEST_LINE" required. [hostname "mysite.com"] [uri "/myrosip.php"] [unique_id "VtFei5BMStIADvtxPsMAAAAV"] Apache error
2016-02-27 10:30:03 Error xx.xx.128.85 ModSecurity: [file "/etc/httpd/crs/activated_rules/modsecurity_crs_30_http_policy.conf"] [line "78"] [id "960034"] [rev "2"] [msg "HTTP protocol version is not allowed by policy"] [data "ROS HTTP/1.0"] [severity "CRITICAL"] [ver "OWASP_CRS/2.2.8"] [maturity "9"] [accuracy "9"] [tag "OWASP_CRS/POLICY/PROTOCOL_NOT_ALLOWED"] [tag "WASCTC/WASC-21"] [tag "OWASP_TOP_10/A6"] [tag "PCI/6.5.10"] Warning. Match of "within %{tx.allowed_http_versions}" against "REQUEST_PROTOCOL" required. [hostname "mysite.com"] [uri "/myrosip.php"] [unique_id "VtFei5BMStIADvtxPsMAAAAV"] Apache error
2016-02-27 10:30:03 Error xx.xx.128.85 ModSecurity: [file "/etc/httpd/crs/activated_rules/modsecurity_crs_49_inbound_blocking.conf"] [line "26"] [id "981176"] [msg "Inbound Anomaly Score Exceeded (Total Score: 5, SQLi=0, XSS=0): Last Matched Message: HTTP protocol version is not allowed by policy"] [data "Last Matched Data: GET /myrosip.php?who=Fantastic ROS HTTP/1.0"] Access denied with code 403 (phase 2). Pattern match "(.*)" at TX:960911-OWASP_CRS/PROTOCOL_VIOLATION/INVALID_REQ-REQUEST_LINE. [hostname "mysite.com"] [uri "/myrosip.php"] [unique_id "VtFei5BMStIADvtxPsMAAAAV"] Apache error
2016-02-27 10:30:03 Error xx.xx.128.85 ModSecurity: [file "/etc/httpd/crs/activated_rules/modsecurity_crs_60_correlation.conf"] [line "37"] [id "981204"] [msg "Inbound Anomaly Score Exceeded (Total Inbound Score: 5, SQLi=0, XSS=0): HTTP protocol version is not allowed by policy"] Warning. Operator GE matched 5 at TX:inbound_anomaly_score. [hostname "mysite.com"] [uri "/myrosip.php"] [unique_id "VtFei5BMStIADvtxPsMAAAAV"]
Thanks very much... I used this on the script for DDNS update...this script check your publick ip and return in a local variable.
ros code
{ /tool fetch url="http://myip.dnsomatic.com/" mode=http dst-path=mypublicip.txt local ip [file get mypublicip.txt contents ] put $ip }
Yes, you can do this:is there any way you can make it print the variable or does it have to save to a file.
:global currentIP [:resolve myip.opendns.com server=208.67.222.222];
Plese write code her when my router rebooted without proper-shutdown then this file send to me in my emailthis script check your publick ip and return in a local variable.
ros code
{ /tool fetch url="http://myip.dnsomatic.com/" mode=http dst-path=mypublicip.txt local ip [file get mypublicip.txt contents ] put $ip }
:put [/ip cloud get public-address]
:local filename "currentIP.txt"
:local ip [file get currentIP.txt contents];
delay 1
:local cip ([:resolve myip.opendns.com server=208.67.222.222]);
delay 1
:local sub ("New WAN address: $cip");
:local bod ("old WAN IP = $ip \nnew WAN IP = $cip");
delay 3
:if ($ip != $cip) do={/tool e-mail send to="your.email@email.com" subject="$sub" body="$bod";
delay 4
/file set $filename contents=$cip
}
}
# Install this script and name it "GetIPAddress"
# Enable the scheduler to run once a day and also on boot
/system scheduler add name=RunGetIPAddress1 interval=1d on-event="{:delay 10; /system script run GetIPAddress}"
/system scheduler add name=RunGetIPAddress2 start-time=startup on-event="{:delay 120; /system script run GetIPAddress}"
# SCRIPT
{
# declare variables
:local filename "GetIPAddress.txt";
:local sNewIP ([:resolve myip.opendns.com server=208.67.222.222]);
:local sOldIP ("");
:local sURL ("https://server/GetIPAddress.php");
:local sDate [/system clock get date];
:local sTime [/system clock get time];
:local sIdentity [/system identity get name];
# message to post when ip address changes
:local sFrom ("PutSomethingHere");
:local sSubject ("IP Address");
:local sBody ("Identity: $sIdentity | IP Address: $sNewIP | Date: $sDate| Time: $sTime");
# begin execution
# create file if not found
:if ([:len [/file find name=$filename]] <= 0) do={:put ("create file"); /file print file=$filename; delay 3; /file set $filename contents="file";}
# read in file
:set $sOldIP [/file get $filename contents];
# See if ip address has changed
:if ($sOldIP != $sNewIP) do={
# post json string to server
:log info "GetIPAddress: posting data to server";
/tool fetch keep-result=no mode=https http-method=post url="$sURL" http-data="{\"From\":\"$sFrom\",\"Subject\":\"$sSubject\",\"Body\":\"$sBody\"}";
# update ip address in file
:log info "GetIPAddress: updating IP Address in file";
/file set $filename contents=$sNewIP;
} else={
:log info "GetIPAddress: ip address has not changed";
}
}
<?php
// Notifications from MikroTik units out in the field. Customize the $To field below.
// time setup
date_default_timezone_set('UTC');
// read in the type of request
$request = http_build_query($_POST);
$size = strlen($request);
// verify client has not sent raw POST
if($size == 0)
{
// if client does not send "Content-Type" header then $_POST data is stored in php://input ($HTTP_RAW_POST_DATA).
$request = file_get_contents("php://input");
$size = strlen($request);
$_POST = $request;
}
// act on the type of request
if($size != 0)
{
// POST call
ProcessPost();
}
else
{
echo "error";
}
// Client post a JSON string that look like:
// "{'From': 'from', 'Subject': 'subject', 'Body': 'body'}";
function ProcessPost()
{
# Set to the email address you want posted data to go to.
$To = 'test@mailinator.com';
// read in the POST JSON string into object
$contents = utf8_encode($_POST);
$json = json_decode($contents);
// Build the email properties
$subject = $json->{'Subject'};
$headers = 'Content-Type: text/plain; charset=utf-8' . "\n" . 'Content-Transfer-Encoding: 8bit' . "\n" . 'From: ' . $json->{'From'} . "\n" . 'Reply-To: ' . $To . "\n" . 'X-Mailer: PHP/' . phpversion();
$message = $json->{'Body'};
// send the email
mail($To, $subject, $message, $headers);
}
?>
this script check your publick ip and return in a local variable.
ros code
{ /tool fetch url="http://myip.dnsomatic.com/" mode=http dst-path=mypublicip.txt local ip [file get mypublicip.txt contents ] put $ip }
/tool fetch output=user url=https://ipinfo.co.za;
status: finished
downloaded: 0KiBC-z pause]
data: Your IP address is: 81.180.71.14
:put [:resolve myip.opendns.com server=208.67.222.222]
:put [/ip cloud get public-address]
Can you give some example on that.all other disadvantages...
Works like a charm! Thank you..Why use all these complicate code, when you can just go to IP Cloud and turn it on. Then router does everything for you.
To get the IP address in code:Code: Select all:put [/ip cloud get public-address]
Jotne, I want to put the dynamic wanip in a dstnat rule. Note the comment for identification/location purposes.Why use all these complicate code, when you can just go to IP Cloud and turn it on. Then router does everything for you.
To get the IP address in code:Code: Select all:put [/ip cloud get public-address]
{
:local test [/ip cloud get public-address]
:put $test
}
Jotne, I want to put the dynamic wanip in a dstnat rule. Note the comment for identification/location purposes.
/ip firewall nat
chain=dstnat dst-address-type=local in-interface=WAN2 protocol=udp dst-port=wg-port action=dst-nat to-addresses=dynamic-ip comment="wireguard-workaround'
The only similar script I have is the one to identify new Gateway and insert into routes...
:if ($bound=1) do={
:local gw $"gateway-address"
/ip route set [ find comment="wireguard-workaround" gateway!=$gw ] gateway=$gw
}
How do I translate that to wanip versus gateweayIP?