multiple lines into one

How it possible to do it in one line of code in terminal this command i found here on forum

/ip dhcp-server lease {
:foreach i in=[find (!dynamic && status=“bound”)] do={
:local activeAddress [get $i active-address]
:local activeMacAddress [get $i active-mac-address]
:local hostname [get $i host-name]

:put ($outputContent . “\n” . $activeAddress . “,” . $activeMacAddress . “,” . $hostname)
}
}

{ do={ command1; command2; command3; }}

Semicolon ;

Put it between any line that does not end in { or before a line that begins with }

EDIT: mrz beat me to it

It gives me error. It works directly to mirkotik but from ssh i cant do it.
Does it possible to resolve that issue?

One-liner

/ip dhcp-server lease { :foreach i in=[find (!dynamic && status="bound")] do={ :local activeAddress [get $i active-address]; :local activeMacAddress [get $i active-mac-address]; :local hostname [get $i host-name]; :put ($outputContent . "\n" . $activeAddress . "," . $activeMacAddress . "," . $hostname) } }

works for me when connecting to RB via ssh.

sshpass -p $pass ssh -o $log -n $user@$h -p 4111 /ip dhcp-server lease { :foreach i in=[find (!dynamic && status=“bound”)] do={ :local activeAddress [get $i active-address]; :local activeMacAddress [get $i active-mac-address]; :local hostname [get $i host-name]; :put ($outputContent . “\n” . $activeAddress . “,” . $activeMacAddress . “,” . $hostname) } } print file=$n

this is my ssh code…
but throw me error
line 11: syntax error near unexpected token `(’

That looks to me as an insufficient indication to bash what it should handle and what not..

Try to place the whole command for Mikrotik into quotes and escape the symbols ",$,\ you need to make it to Mikrotik:

sshpass -p $pass ssh -o $log -n $user@$h -p 4111 "/ip dhcp-server lease { :foreach i in=[find (!dynamic && status=**"bound*")] do={ :local activeAddress [get ***$i active-address]; :local activeMacAddress [get *$i active-mac-address]; :local hostname [get ***$i host-name]; :put (*$outputContent . ***"**\n*" . ***$activeAddress . **",*" . ***$activeMacAddress . **",*" . ***$hostname) } } print file=****$n"

Hint: to make sure it works properly, start by using echo instead of sshpass … -p 4111 and fine-tune the escaping until the output is exactly what you want the Mikrotik to get.

Actually it’s better to put everything into single quotes. In bash both types of quotes mean “treat this as single argument”, the big difference is that bash tries to substitute plenty of things within double-quoted text while it will pass verbatim whatever single-quoted.

Example:

N=4; echo "$N"

will produce line containing simple “4” while

N=4; echo '$N'

will produce line

$N

does it possible from that script to get only ip addresses with hostname i define?

Sorry, can you use another wording? It is not clear to me what you need.

some brackets are not good closed, or I am wrong. It returns me error with brackets..

Ok, look, i have my dhcp lease on several computers.
I want to get Ip address of hostname PC.
SO i wrote a bash script that connect via ssh to mikrotik and run a terminal command.
Problem is that I dont know how to get IP address of dhcp lease address with hostname=PC.

I got via

ip dhcp-server lease print where host-name=“pc”

but you help me how to get only Ip address without unnecessary information from result of command?

put [ip dhcp-server lease get [find hostname=“pc”] address]

it throws me “no such item”

Because a dash was missing in what I wrote. Now I got home and tried using the Tab button:

[me@MyTik] > put [ip dhcp-server lease get [find host-name=my-HP] address]
192.168.88.254

thanks man! It works now.
Only last problem,
i must put that into file.

sshpass -p $pass ssh -o $log -n $user@$h -p 4111:global n [ip dhcp-server lease get [find host-name=PC] address];

This thow me no such file..BUt problem is that I need to put it into file…I need file with some name to store this results..

I don’t know how to create an empty file, but you can create a non-empty file using e.g. /ip address print file=myfile and then replace its contents:
:file set myfile.txt contents=[ip dhcp-server lease get [find host-name=my-HP] address]

But I’m not sure why would you want to save a single value into a file and then download that file using ftp, as if you put it in the command line sent using ssh, the ssh returns that output it to the linux machine:

me@MyLinux:~$ ssh me@192.168.0.1 'put [/ip dhcp-server lease get [find mac-address=00:A3:81:4C:48:42] address]'
192.168.0.25

Or

me@MyLinux:~$ a=$(ssh me@192.168.0.1 'put [/ip dhcp-server lease get [find mac-address=00:A3:81:4C:48:42] address]')
me@MyLinux:~$ echo $a
192.168.0.25

thanks for reply.
Problem lies somewhere alse abvious.

When sshpass this command

ip dhcp-server lease print file=$n

my script execute without problem.
I use that variable n in later lines of code.
But i dont need all dhcp lease, only with PC hostname, we solve that, but what is difference with that code and this

:global n [ip dhcp-server lease get [find host-name=PC] address];/file print file=$n

Why this line of code gave me errors…

This line of code says:

  • set the value of a global-scoped variable named n to the ip address leased to device with hostname PC
  • print the list of existing files into a file whose name is retrieved from the global variable n
    So you end up with a file named ip.add.re.ss.txt. which contains a list of all files currently available on the machine. On my machine it gives no errors if entered directly, so if it causes trouble via ssh, you either haven’t used single quotes as Metod has recommended or there is some other error.

/file print is equivalent to ls / in linux; /file print file=something is equivalent to ls / > something