JeremyInDenver
JeremyInDenver

Reputation: 21

Save ssh output to a variable

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 [email protected] > 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 [email protected] <<"ENDSSH"
show ip route ospf neighbor
exit
ENDSSH)

Any help is appreciated.

Upvotes: 2

Views: 282

Answers (1)

KamilCuk
KamilCuk

Reputation: 141030

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

MYVAR=$(sshpass -f pfile ssh -T [email protected] <<"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 [email protected] show ip route ospf neighbor)

Upvotes: 5

Related Questions