Script: SSH to another Server, Grab Info

Hi all

I would like to SSH/Telnet into a remote server and grab some info of it. I was trying to get the SSH / Telnet commands to work from the CLI and from within a script.

Has anyone accomplished this? I read quite a couple of posts but couldn’t find my answer, perhaps someone could redirect me.

What I need to accomplish, is to login to an upstream provider’s BGP server. On the server one can find all local (South African) IP Addresses. We would like to use the ISP with the BGP server only for local traffic, and the rest of the traffic should be routed via the other ISP. We used to do this manually, but as the list changes on almost a weekly basis, I would like to automize it.

To see what I mean, Telnet to: route-server.is.co.za and use the command: show ip bgp to show the list over SA IP’s.

The script I need would take all the IP’s, add them to my ROUTE list and specify a variable somewhere set in the script as the gateway.

Any help will be appreciated.

G :sunglasses:

This will be a great if you can get it to work but i am also trying to get the info of the bgp server from the is router

Thanks for the support Camel Lights.

Any help would greatly be appreciated.

you cannot use SSH via scripting within routerOS, would be great, but you cant currently.

But what would work now to get the it working.Have done some googeling and there are some scrips out there but not for Mt

#! /bin/bash

bgp_server=$1
shift
route_spec=(“$@”)

[ -z “${bgp_server}” ] && echo -e “USAGE: $0 bgp_server route spec\neg: $0 route-server.is.co.za dev ppp0” && exit -1
[ -z “${route_spec}” ] && echo -e “USAGE: $0 bgp_server route spec\neg: $0 route-server.is.co.za dev ppp0” && exit -1

ip=/sbin/ip
nc=/usr/bin/nc
awk=/usr/bin/awk
dos2unix=/usr/bin/dos2unix
find=/usr/bin/find
cacheperiod=720

routes_file=/var/lib/bgp_route_cache/${bgp_server}

need_update=$($find “${routes_file}” -mmin “${cacheperiod}” | wc -l)

trap “rm -f ‘${routes_file}.tmp’” EXIT
[[ $need_update -gt 0 ]] && echo -e “terminal length 0\nshow ip bgp\nexit” | $nc “${bgp_server}” 23 | $dos2unix | awk ‘/^*/ {print $2}’ |
awk -F’[./]’ ‘{ printf $0;
if (NF==5) {
print “”
} else {
if ($1 < 128) {
print “/8”
} else if ($1 < 192) {
print “/16”
} else {
print “/24” }
}
}’ > “${routes_file}.tmp”

[[ $(wc -l “${routes_file}.tmp”) -gt 0 ]] && mv “${routes_file}.tmp” “${routes_file}”

exec 4<“${routes_file}”
while read subnet <&4; do
$ip route add “${subnet}” “${route_spec[@]}”
done

and

use:Telnet;

$prompt = ‘/public-route-server>/’;
$server=“route-server.is.co.za”;

print “Connecting to $server\n”;
my $session = Net::Telnet->new(Host => $server,Prompt => $prompt,Timeout=>30);
unlink(“t.log”);
$session->dump_log(“t.log”);

$session->waitfor($prompt);

#turn off paging
$session->cmd(“terminal length 0”);

#get list of local routes
print “Retrieving BGP routes\n”;
my @output = $session->cmd(“show ip bgp\n”);
print @output;
print “Route list received\n”;
$session->close;
last

can this be changed to work vor MT

thanks

Any news on this topic?

It shouldn’t be too difficult to make the scripting tool use the SSH commands already working from Winbox.

Please Mikrotik, help us here!

Re, G

any news on this toppic

You cannot use telnet/SSH from within RouterOS scripts.

To work around this specific problem, put up a small Linux/BSD/whatever box that retrieves the data for you, mangles the data into whatever you need and then makes it available via HTTP/FTP. You can then use “/tool fetch” within RouterOS scripts to retrieve the result.

because mikrotik supports UPS managing, could be usefull to script telnet / ssh sessions to shutdown any other box device (nas) in the network.

This can be easly done i you have linux server somewhere there as well.

do you have one ?