DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
11 Monitoring and Observability Tools for 2023
Learn more
  1. DZone
  2. Data Engineering
  3. Databases
  4. On Using Neo4j-Shell with Basic-Auth Over Http on a Remote Server

On Using Neo4j-Shell with Basic-Auth Over Http on a Remote Server

Michael Hunger user avatar by
Michael Hunger
·
Mar. 24, 14 · Interview
Like (0)
Save
Tweet
Share
3.01K Views

Join the DZone community and get the full member experience.

Join For Free

Accessing your beloved Neo4j-Shell via RMI works ok on localhost or in your intranet. But over the internet you don’t really want to expose RMI ports.

So most installations of Neo4j, e.g. on EC2 use basic auth as simplest security measure.
Also the Neo4j hosting providers like GrapheneDB.com or GraphHost.com offer basic auth by default to “secure” access your Neo4j instance, hopefully also SSL soon.

How do you access the Neo4j Shell on the server from your usual terminal command line ?

If you remember, the Neo4j 1.9 WebAdmin, had a built in Neo4j-Shell tab. This one is still happy and alive and can be accessed via http://localhost:7474/webadmin.

And it uses a simple http endpoint to send shell commands to the server and receives the prompt and command output back.

So you can use curl to easily execute shell commands. And as curl supports basic-auth you have this part covered too.

It looks like this:

curl -s  -H accept:application/json -H content-type:application/json \
     -u username:password \
     -d "{\"command\":\"schema\",\"engine\":\"shell\"}" \

http://localhost:7474/db/manage/server/console

results in a json array result containing the rendered text and the new prompt.

[ "No indexes\n\nNo constraints\n", "neo4j-sh (?)$ " ]

So you can execute any shell command or cypher statement (remember to end them with Semicolon “;”) across the wire.

To make it directly useable I hacked together a small script (a better version of this would use a language like perl, python, ruby or javascript or at least a json library like jq).

#!/bin/bash
# usage neo.sh [-h host:port] [-u user:pass] [cmd]
# end cypher statements with semicolon
# terminate read loop with ^d or return

HOST="localhost:7474"
if [ "$1" == "-h" ]; then
  shift; HOST="$1";shift;
fi
AUTH=""
if [ "$1" == "-u" ]; then
  shift; AUTH="-u $1";shift;
fi

URL="http://${HOST}/db/manage/server/console"
HEADERS=' -H accept:application/json -H content-type:application/json '

function run {
CMD="$@"
DATA="{\"command\":\"${CMD}\",\"engine\":\"shell\"}"
RES=`curl -s $HEADERS $AUTH -d "$DATA" "$URL"`
# bash substitution
RES=${RES#'[ "'}
PROMPT_PATTERN="\", \"neo4j-sh (*)$ \" ]"
RES=${RES%$PROMPT_PATTERN}
# continue reading, incomplete command
if [ "$RES" == "\", \"> \" ]" ]; then
return 1;
else
echo -e "${RES//\\\\n/\\n}"
return 0;
fi
}

P0="neo4j-sh (*)\$ "

if [ "$@" ]; then
run "$@";
exit $?
fi

read -p "$P0" CMD;
while [ "$CMD" ]; do
run "$CMD"
if [ $? == 0 ]; then
read -p "$P0" CMD;
else
#continue reading, incomplete command
read -p "> " CMD1;
CMD="${CMD} ${CMD1}"
fi
done

See also this GitHub Gist for a ready-to use version of my hackey script.


workplace Command (computing) Neo4j shell Python (language) GitHub terminal security Gist (computing) Measure (physics)

Published at DZone with permission of , DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Required Knowledge To Pass AWS Certified Solutions Architect — Professional Exam
  • 10 Best Ways to Level Up as a Developer
  • Multi-Cloud Integration
  • Create Spider Chart With ReactJS

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: