I am working on a bash script, where it will send my commands to geth through a simple piping. The problem is for each piping I have to create geth process again, which preload my_script.js into geth all over again and this process actually consumes time since my_script.js is large.

echo "net" | sudo geth --datadir --preload "my_script.js" \
      "/home/MyEthereumEbloc" attach ipc:/home/MyEthereumEbloc/geth.ipc

Output (I can obtain the output correctly):

  listening: true,
  peerCount: 2,
  version: "23422",
  getListening: function(callback),
  getPeerCount: function(callback),
  getVersion: function(callback)

I want to pass information as input and get its output result from geth process that works at the background and required to load my_script.js only one time. In order to accomplish this, I have tried piping but I wasn't able to make it work. I am sorry if this question might be related to unix but any additional approach to solve this problem would be appreciated.

mkfifo program.pipe;
cat > program.pipe &
sudo geth --datadir "/home/MyEthereumEbloc" attach \
      ipc:/home/MyEthereumEbloc/geth.ipc console >> output.txt < sudo program.pipe

#On another terminal.
printf "%s\n" "net" >> sudo program.pipe

Thank you for your valuable time and help.

  • 1
    What exactly are you trying to accomplish? The whole point of geth is that it provides an RPC and IPC interface so that you don't need to do anything crazy like this... Dec 7 '16 at 0:42
  • I was using geth to call my contracts ( ex: mycontract.get_name() ) which are already deployed into my private blockchain or check transaction receipts (eth.getTransactionReceipt(<hash-id>)) and so on. So I just want to write a simple script (calling my contracts, sending transactions etc.) that would talk with my blockchain. [This might be an example on tests/test.py code part in populus]. Should I use another useful application to to this task?
    – alper
    Dec 7 '16 at 2:05
  • 1
    By far the easiest way is to use nodejs with the web3 library. You can do it using curl and/or sockets, but web3 is a great wrapper over those interfaces. There's a library in python as well. The command line options you're using are not designed for programmatic use; there are nice APIs for that Dec 7 '16 at 2:08
  • Thanks a lot, I understand my wrong approach: so basically I have to use nodejs with the web3 library to send/get information from my blockchain. Could you please guide me for a tutorial and link for the library in python, if possible?
    – alper
    Dec 7 '16 at 2:19
  • 1
    Sure, here's the python library (possibly less maintained than the JS one). There's some instructions into the readme, and the JS interface is exactly what you were using inside the geth command line script. Dec 7 '16 at 2:40

Take out the sudo in sudo program.pipe.

What's happening is that you're piping data into sudo which is baffled and does nothing. It never gets to the pipe in the first place.

Also, why are you using sudo with geth attach? This is almost certainly some kind of mistake--geth doesn't need root priviledges.

  • Thank you for your valuable advice. The reason I am using sudowith geth is I am facing with the following error: Fatal: Unable to attach to remote geth: dial unix /home/MyEthereumEbloc/geth.ipc: connect: permission denied.
    – alper
    Dec 7 '16 at 2:19
  • 1
    I believe you can use chmod +rw geth.ipc to set the correct permissions. (If it's owned by a different user, you can use chown obviously) Dec 7 '16 at 15:02

If you just want to get information from Geth, then you should be using the IPC or RPC interface. To access the RPC interface, you can use curl, as in

curl -X POST --data '{"jsonrpc":"2.0","method":"net_version","params":[],"id":67}'

{ "id":67, "jsonrpc": "2.0", "result": "59" }

For IPC, you can use socat to open concatenate the ipc socket to stdin:

socat "/home/MyEthereumEbloc/geth.ipc" -

or you can concat it to a named FIFO pipe for easy access:

mkfifo myPipe
socat  "/home/MyEthereumEbloc/geth.ipc" myPipe
cat myPipe
echo '{"jsonrpc":"2.0","method":"web3_clientVersion","params":[],"id":67}' >> myPipe

and then just use it like any unix fifo named pipe.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.