Save ssh output to a variable

Viewed 46

I'm a bash scripting newb that is trying to write a script to retrieve ospf neighbors from routers that don't support that through snmp. The following script works:

#!/bin/bash

rm ./results.txt

sshpass -f pfile ssh -T admin@192.168.128.47 > results.txt <<"ENDSSH"
show ip route ospf neighbor
exit
ENDSSH

Rather than put the results in a text file I would like them to be in a variable so I can parse the results. I've tried the following to no avail:

MYVAR=$(sshpass -f pfile ssh -T admin@192.168.128.47 <<"ENDSSH"
show ip route ospf neighbor
exit
ENDSSH)

Any help is appreciated.

1 Answers

The line to end the here document has to be exactly ENDSSH, nothing else.

MYVAR=$(sshpass -f pfile ssh -T admin@192.168.128.47 <<"ENDSSH"
show ip route ospf neighbor
exit
ENDSSH
)

Still, I see no need for -T and no need for a here document, but maybe it's needed by the router. I would also use a lower case variable -- upper case variables are for exported variables.

myvar=$(sshpass -f pfile ssh admin@192.168.128.47 show ip route ospf neighbor)
Related