2

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.

1
  • Have you tried it all on one line? MYVAR=$(sshpass -f pfile ssh -T [email protected] <<"ENDSSH"; show ip route ospf neighbor; exit; ENDSSH;) Commented Aug 2, 2022 at 17:06

1 Answer 1

5

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)
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, moving the parentheses did the trick. FWIW the -T is required with a Moxa router. I also couldn't use your second suggestion because without the "exit" command the router just kept the connection open until the five minute timeout.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.