Manual:Tools/Fetch: Difference between revisions

From MikroTik Wiki
Jump to navigation Jump to search
mNo edit summary
No edit summary
 
(30 intermediate revisions by 6 users not shown)
Line 1: Line 1:
{{ ... }}
{{Versions|v6 +}}
{{Versions|v3, v4}}


<div class=manual>
__TOC__


<h2>Summary</h2>
==Summary==
<p><b>Sub-menu:</b> <code>/tool fetch</code></p>
<p id="shbox"><b>Sub-menu:</b> <code>/tool fetch</code>
<p><b>Standards:</b> <code></code></p>
<br />
<br />
<p>
<b>Standards:</b> <code></code></p>


Fetch is one of the console tools in Mikrotik RouterOS. It is used to copy files from the network device to a Mikrotik.
Fetch is one of the console tools in Mikrotik RouterOS. It is used to copy files to/from a network device via HTTP, FTP or SFTP (Support for SFTP added on v6.45), it can also be used to sent POST/GET requests and send any kind of data to a remote server.
HTTPS protocol is supported; by default no certificate checks are made, but setting '''check-certificate''' to ''yes'' enables trust chain validation from local certificate store.
<h2>Properties</h2>


<table class="styled_table">
==Properties==
<tr>
 
  <th width="40%">Property</th>
{{Mr-arg-table-h
  <th >Description</th>
|prop=Property
</tr>
|desc=Description
<tr>
}}
    <td><var><b>address</b></var>(<em>string</em>; Default: <b></b>)</td>
 
    <td>IP address of the device to copy from.</td>
{{Mr-arg-table
</tr>
|arg=address
<tr>
|type=string
    <td><var><b>dst-path</b></var>(<em>string</em>; Default: <b></b>)</td>
|default=
    <td>Title of the input file and file path.</td>
|desc=IP address of the device to copy file from.
</tr>
}}
<tr>
 
    <td><var><b>host</b></var>(<em>string</em>; Default: <b></b>)</td>
{{Mr-arg-table
    <td>Domain name or virtual domain name (if used on web-site, from which you want to copy information). For example,
|arg=as-value
|type=set {{!}} not-set
|default=not-set
|desc=Store the output in a variable, should be used with the <var>output</var> property.
}}
 
{{Mr-arg-table
|arg=ascii
|type=yes {{!}} no
|default=no
|desc=
}}
 
{{Mr-arg-table
|arg=check-certificate
|type=yes {{!}} no
|default=no
|desc=Enables trust chain validation from local certificate store.
}}
 
{{Mr-arg-table
|arg=dst-path
|type=string
|default=
|desc=Destination filename and path
}}
 
{{Mr-arg-table
|arg=host
|type=string
|default=
|desc=Domain name or virtual domain name (if used on web-site, from which you want to copy information). For example,
<pre>address=wiki.mikrotik.com host=forum.mikrotik.com</pre>
<pre>address=wiki.mikrotik.com host=forum.mikrotik.com</pre>
In this example the resolved ip address is the same (66.228.113.27), but hosts are different.</td>
In this example the resolved ip address is the same (66.228.113.27), but hosts are different.
</tr>
}}
<tr>
    <td><var><b>keep-result</b></var>(<em>yes|no</em>; Default: <b>yes</b>)</td>
    <td>If yes, creates an input file.</td>
</tr>
<tr>
    <td><var><b>mode</b></var>(<em>ftp|http|tftp</em>; Default: <b>http</b>)</td>
    <td>Choose the protocol of connection - http, ftp or tftp.</td>
</tr>
<tr>
    <td><var><b>user</b></var>(<em>string</em>; Default: <b></b>)</td>
    <td>User name, which is needed to login the remote device.</td>
</tr>
<tr>
    <td><var><b>password</b></var>(<em>string</em>; Default: <b></b>)</td>
    <td>Password, which is needed to login the remote device.</td>
</tr>
<tr>
    <td><var><b>port</b></var>(<em>integer</em>; Default: <b></b>)</td>
    <td>Connection port.</td>
</tr>
<tr>
    <td><var><b>src-path</b></var>(<em>string</em>; Default: <b></b>)</td>
    <td>Title of the remote file you need to copy.</td>
</tr>
</table>


The following example shows how to copy the file with filename "conf.rsc" from device with ip address 192.168.88.2 by FTP protocol and save it as file with filename "123.rsc". User and password are needed to login into the device.  
{{Mr-arg-table
|arg=http-method
|type={{!}}delete{{!}}get{{!}}post{{!}}put
|default=get
|desc=the HTTP method to use
}}
 
{{Mr-arg-table
|arg=http-data
|type=string
|default=
|desc=the data, that is going to be send, when using PUT or POST methods
}}
 
{{Mr-arg-table
|arg=http-header-field
|type=string
|default=*empty*
|desc=list of all header fields and their values, in the form of <code>http-header-field=h1:fff,h2:yyy</code>
}}
 
{{Mr-arg-table
|arg=keep-result
|type=yes {{!}} no
|default=yes
|desc=If yes, creates an input file.
}}
 
{{Mr-arg-table
|arg=mode
|type=ftp{{!}}http{{!}}tftp {!} https
|default=http
|desc=Choose the protocol of connection - http, https , ftp or tftp.
}}
 
 
{{Mr-arg-table
|arg=output
|type=none{{!}}file{{!}}user
|default=file
|desc=Sets where to store the downloaded data.
* <code>none</code> - do not store downloaded data
* <code>file</code> - store downloaded data in a file
* <code>user</code> - store downloaded data in the <var>data</var> variable
}}
 
 
{{Mr-arg-table
|arg=password
|type=string
|default=anonymous
|desc=Password, which is needed for authentication to the remote device.
}}
 
{{Mr-arg-table
|arg=port
|type=integer
|default=
|desc=Connection port.
}}
 
{{Mr-arg-table
|arg=src-path
|type=string
|default=
|desc=Title of the remote file you need to copy.
}}
 
{{Mr-arg-table
|arg=upload
|type=yes {{!}} no
|default=no
|desc=If enabled then fetch will be used to upload file to remote server. Requires ''src-path'' and ''dst-path'' parameters to be set.
}}
 
{{Mr-arg-table
|arg=url
|type=string
|default=
|desc=URL pointing to file. Can be used instead of <b>address</b> and <b>src-path</b> parameters.
}}
 
{{Mr-arg-table-end
|arg=user
|type=string
|default=anonymous
|desc=User name, which is needed for authentication to the remote device.
}}
 
==Examples==
 
==== Downloading files to the router ====
 
The following example shows how to copy the file with filename "conf.rsc" from a device with ip address 192.168.88.2 by FTP protocol and save it as file with filename "123.rsc". User and password are needed to login into the device.  
<pre>
<pre>
[admin@mt-test] /tool> fetch address=192.168.88.2 src-path=conf.rsc  
[admin@mt-test] /tool> fetch address=192.168.88.2 src-path=conf.rsc \
user=admin mode=ftp password=123 dst-path=123.rsc port=21  
user=admin mode=ftp password=123 dst-path=123.rsc port=21 \
host="" keep-result=yes
host="" keep-result=yes
</pre>
</pre>


<nowiki>[</nowiki>[[#content|Back to Content]]<nowiki>]</nowiki>
Example to upload file to other router:
<pre>
[admin@mt-test] /tool> fetch address=192.168.88.2 src-path=conf.rsc \
user=admin mode=ftp password=123 dst-path=123.rsc upload=yes
</pre>
 
 
Another file download example that demonstrates the usage of <var>url</var> property.
<pre>
[admin@test_host] /> /tool fetch url="http://www.mikrotik.com/img/netaddresses2.pdf" mode=http
  status: finished
 
[admin@test_host] /> /file print
# NAME                    TYPE                  SIZE                CREATION-TIME     
...
5 netaddresses2.pdf        .pdf file            11547                jun/01/2010 11:59:51
 
</pre>
 
==== Sending information to a remote host ====
 
It is possible to use HTTP POST request to send information to a remote server, that is prepared to accept it. In the following example, we send geographic coordinates to a PHP page:
 
<pre>
/tool fetch http-method=post http-header-field="Content-Type: application/json" http-data="{\"lat\":\"56.12\",\"lon\":\"25.12\"}" url="http://testserver.lv/index.php"             
</pre>
 
Of course, you can use Fetch with scripts and fill the above command with variables from the RouterOS GPS menu.
 
==== Return value to a variable ====
 
Since RouterOS v6.43 it is possible to save the result of fetch command to a variable. For example, it is possible to trigger a certain action based on the result that a HTTP page returns. You can find a very simple example below that disables '''ether2''' whenever a PHP page returns "0":
<pre>
{
    :local result [/tool fetch url=http://10.0.0.1/disable_ether2.php as-value output=user];
    :if ($result->"status" = "finished") do={
        :if ($result->"data" = "0") do={
            /interface ethernet set ether2 disabled=yes;
        } else={
            /interface ethernet set ether2 disabled=no;
        }
    }
}
</pre>
 
 
==== SFTP ====
 
Since 6.45beta50 <code>/tool fetch</code> support SFTP (SSH File Transfer Protocol) protocol.
 
<pre>
[admin@MikroTik] > /tool fetch url="sftp://10.155.126.200/home/x86/Desktop/50MB.zip" user=x86 password=root dst-path=disk1
      status: downloading
  downloaded: 1048KiB
      total: 51200KiB
    duration: 6s
-- [Q quit|D dump|C-z pause]
 
</pre>
 
 
{{cont}}


[[Category:Manual|F]]
[[Category:Manual|F]]
[[Category:Unfinished]]
[[Category:Tools|F]]

Latest revision as of 11:26, 21 January 2021

Version.png

Applies to RouterOS: v6 +

Summary

Sub-menu: /tool fetch
Standards:

Fetch is one of the console tools in Mikrotik RouterOS. It is used to copy files to/from a network device via HTTP, FTP or SFTP (Support for SFTP added on v6.45), it can also be used to sent POST/GET requests and send any kind of data to a remote server. HTTPS protocol is supported; by default no certificate checks are made, but setting check-certificate to yes enables trust chain validation from local certificate store.

Properties

Property Description
address (string; Default: ) IP address of the device to copy file from.
as-value (set | not-set; Default: not-set) Store the output in a variable, should be used with the output property.
ascii (yes | no; Default: no)
check-certificate (yes | no; Default: no) Enables trust chain validation from local certificate store.
dst-path (string; Default: ) Destination filename and path
host (string; Default: ) Domain name or virtual domain name (if used on web-site, from which you want to copy information). For example,
address=wiki.mikrotik.com host=forum.mikrotik.com
In this example the resolved ip address is the same (66.228.113.27), but hosts are different.
http-method (|delete|get|post|put; Default: get) the HTTP method to use
http-data (string; Default: ) the data, that is going to be send, when using PUT or POST methods
http-header-field (string; Default: *empty*) list of all header fields and their values, in the form of http-header-field=h1:fff,h2:yyy
keep-result (yes | no; Default: yes) If yes, creates an input file.
mode (ftp|http|tftp {!} https; Default: http) Choose the protocol of connection - http, https , ftp or tftp.
output (none|file|user; Default: file) Sets where to store the downloaded data.
  • none - do not store downloaded data
  • file - store downloaded data in a file
  • user - store downloaded data in the data variable
password (string; Default: anonymous) Password, which is needed for authentication to the remote device.
port (integer; Default: ) Connection port.
src-path (string; Default: ) Title of the remote file you need to copy.
upload (yes | no; Default: no) If enabled then fetch will be used to upload file to remote server. Requires src-path and dst-path parameters to be set.
url (string; Default: ) URL pointing to file. Can be used instead of address and src-path parameters.
user (string; Default: anonymous) User name, which is needed for authentication to the remote device.

Examples

Downloading files to the router

The following example shows how to copy the file with filename "conf.rsc" from a device with ip address 192.168.88.2 by FTP protocol and save it as file with filename "123.rsc". User and password are needed to login into the device.

[admin@mt-test] /tool> fetch address=192.168.88.2 src-path=conf.rsc \
user=admin mode=ftp password=123 dst-path=123.rsc port=21 \
host="" keep-result=yes

Example to upload file to other router:

[admin@mt-test] /tool> fetch address=192.168.88.2 src-path=conf.rsc \
user=admin mode=ftp password=123 dst-path=123.rsc upload=yes


Another file download example that demonstrates the usage of url property.

[admin@test_host] /> /tool fetch url="http://www.mikrotik.com/img/netaddresses2.pdf" mode=http 
  status: finished

[admin@test_host] /> /file print 
 # NAME                     TYPE                  SIZE                 CREATION-TIME       
 ...
 5 netaddresses2.pdf        .pdf file             11547                jun/01/2010 11:59:51

Sending information to a remote host

It is possible to use HTTP POST request to send information to a remote server, that is prepared to accept it. In the following example, we send geographic coordinates to a PHP page:

/tool fetch http-method=post http-header-field="Content-Type: application/json" http-data="{\"lat\":\"56.12\",\"lon\":\"25.12\"}" url="http://testserver.lv/index.php"              

Of course, you can use Fetch with scripts and fill the above command with variables from the RouterOS GPS menu.

Return value to a variable

Since RouterOS v6.43 it is possible to save the result of fetch command to a variable. For example, it is possible to trigger a certain action based on the result that a HTTP page returns. You can find a very simple example below that disables ether2 whenever a PHP page returns "0":

{
    :local result [/tool fetch url=http://10.0.0.1/disable_ether2.php as-value output=user];
    :if ($result->"status" = "finished") do={
        :if ($result->"data" = "0") do={
            /interface ethernet set ether2 disabled=yes;
        } else={
            /interface ethernet set ether2 disabled=no;
        }
    }
}


SFTP

Since 6.45beta50 /tool fetch support SFTP (SSH File Transfer Protocol) protocol.

[admin@MikroTik] > /tool fetch url="sftp://10.155.126.200/home/x86/Desktop/50MB.zip" user=x86 password=root dst-path=disk1
      status: downloading
  downloaded: 1048KiB
       total: 51200KiB
    duration: 6s
-- [Q quit|D dump|C-z pause]


[ Top | Back to Content ]