This is the new home of the egghelp.org community forum.
All data has been migrated (including user logins/passwords) to a new phpBB version.


For more information, see this announcement post. Click the X in the top right-corner of this box to dismiss this message.

Getting a bash script to communicate with an eggdrop

General support and discussion of Eggdrop bots.
Post Reply
o
onesikgypo
Voice
Posts: 4
Joined: Sat Jun 14, 2008 9:59 pm

Getting a bash script to communicate with an eggdrop

Post by onesikgypo »

Hi,

I have a bash script already made, however a new addition id like to make, is that whent he script is executed, my eggdrop msg's a channel with such details. I have already installed and setup my eggdrop, however i have no idea how i can get bash to talk with the eggdrop in order to perform what i require.

Any assistance will be greatly appreciated.

Thankyou.
User avatar
incith
Master
Posts: 275
Joined: Sat Apr 23, 2005 2:16 am
Location: Canada

Post by incith »

You will need eggdrop to exec the script.
o
onesikgypo
Voice
Posts: 4
Joined: Sat Jun 14, 2008 9:59 pm

Post by onesikgypo »

could you give a bit more detail on how to do this/how this works - ie. if i had a bash script with

#!/bin/bash
find *.txt >> fils.lst
rm -f *.txt
dir=`pwd`
;;MSG HERE- Msg #chan Cleaning $dir;;

as an example, how would i go about it - and if eggdrop needs to execute the script, im guessing then if executing via ssh then no msges will occur - if this is the case, is there a way i can write an ssh script, which will force the eggdrop to execute the said script (the point of which is that i can continue using ssh?)

Thanks.
n
nml375
Revered One
Posts: 2860
Joined: Fri Aug 04, 2006 2:09 pm

Post by nml375 »

I guess you could consider using named pipes to communicate with your eggdrop that way.

In that case, you'd have to set up a "listener" within your eggdrop (open, fconfigure, fileevent, gets, eof) to listen at the pipe, and take appropriate actions upon incoming transmissions.
In order to transmit to your eggdrop, it would be a mere matter of

Code: Select all

echo write #chan Clearing $dir >> /path/to/the/pipe
Also see the manpage for mkfifo for further information on named pipes.
NML_375
User avatar
incith
Master
Posts: 275
Joined: Sat Apr 23, 2005 2:16 am
Location: Canada

Post by incith »

I was thinking just returning certain codes from the shell script. If you return 1... well. Let's put it this way:

Code: Select all

#!/bin/sh
find *.txt >> fils.lst 
rm -f *.txt 
dir=`pwd`
return 1

Code: Select all

set foo [exec maihappyness.sh] .. if {[info exists foo]} { putserv hi2u }}
n
nml375
Revered One
Posts: 2860
Joined: Fri Aug 04, 2006 2:09 pm

Post by nml375 »

@incith: Wouldn't foo always exist in that example?
NML_375
User avatar
incith
Master
Posts: 275
Joined: Sat Apr 23, 2005 2:16 am
Location: Canada

Post by incith »

Well, I didn't test that and had posted it roughly 0.34 seconds after waking up, but...

Code: Select all

% set foo [exec lalala]
couldn't execute "lalala": no such file or directory
% set foo
can't read "foo": no such variable
Nevermind I think I know what you are getting at.. as long as it execs something, it should return 0 or 1, ya. And so you'd want to use if {$foo == x}.. or just if {[exec] == x}

I guess my method won't work anyway without some modification to make it a function..

Code: Select all

% set foo [exec ./test.sh]
./test.sh: line 2: return: can only `return' from a function or sourced script
n
nml375
Revered One
Posts: 2860
Joined: Fri Aug 04, 2006 2:09 pm

Post by nml375 »

Rather than using return, I'd make use of the fact that it's just an anonymous pipe... Hence anything sent to stdout would be returned by exec.

Still, this leaves the issue of being able to do this directly from the shell environment. I guess you could telnet into the eggdrop from the shell and do it that way...
NML_375
k
karrde
Voice
Posts: 2
Joined: Wed Jan 21, 2009 11:05 pm

Post by karrde »

nml375 wrote:I guess you could consider using named pipes to communicate with your eggdrop that way.

In that case, you'd have to set up a "listener" within your eggdrop (open, fconfigure, fileevent, gets, eof) to listen at the pipe, and take appropriate actions upon incoming transmissions.
In order to transmit to your eggdrop, it would be a mere matter of

Code: Select all

echo write #chan Clearing $dir >> /path/to/the/pipe
Also see the manpage for mkfifo for further information on named pipes.
Could you possibly describe how to set up the listener within eggdrop in a bit more detail? I'm new to the whole eggdrop thing. I'm trying to get a script to notify a channel when something external happens, if there's a better way to handle this then please let me know. Much appreciated. :)
n
nml375
Revered One
Posts: 2860
Joined: Fri Aug 04, 2006 2:09 pm

Post by nml375 »

One thing I did forget, is that opening a pipe in RDONLY-mode will block until another process opens the pipe in RWONLY-mode. With normal tcl-scripting this would probably not be such a big issue, as this would probably be some server-like application. With eggdrops however, this would prevent your eggdrop from performing it's normal tasks.

One option still would be using tcp-sockets, and use nc (if available). This however has the downside that anyone who knows the listening-port could connect there and send arbitrary data. You would thus have to implement some kind of safety-mechanism.
To achieve something like this however, you'd do something like this:

Code: Select all

listen 12345 script IncomingConnection

proc IncomingConnection {idx} {

 #We've got a new incoming connection, hand it over to our "handler"
 control $idx IncomingTransmission
}

proc IncomingTransmission {idx text} {
 #Did we recieve a new line, or did the remote end close the connection?
 if {$text != ""} {

  #Slit the text into a list and retrieve the command..
  set data [split $text]
  set command [lindex $data 0]

  #.. and see what we're supposed to do.
  #Hint: this would be a nice place to add a command for authentication...
  switch $command {
   write {

    #Use list-element 1 as channel, and the rest as the message to be sent
    #Considder adding a check whether the connection has authenticated or not yet.
    #Atleast we do check that it's a valid channel..
    if {[validchan [set chan [lindex $data 1]]]} {
     puthelp "PRIVMSG $chan :[join [lrange $data 2 end]]"
    }
   }
  }
 } else {
  #remote connection closed, do some cleanup here if needed
  #Hint: here you would un-authenticate the idx (connection identifier)
  #For now tho, we'll just do a putlog for fun
  putlog "Remote connection $idx dropped"
 }
}
This is a very, very crude example, and does no authentication whatsoever. You should thus extend this with some kind of protection/authentication/etc to prevent sending arbitrary messages to your channels.

To send a message from your bash-script, do something like this:

Code: Select all

#simple case, where we have no authentication or such:
echo "write #mychan Cleaning $dir" | nc localhost 12345


#Lets imagine we've implemented an authenticate-command that has to be issued before write...
#Send multiple commands like this:

nc localhost 12345 <<EOF
authenticate mysecretpassword
write #mychan Clearing $dir
EOF
edit: fixing minor typos
Last edited by nml375 on Fri Jan 23, 2009 9:39 am, edited 1 time in total.
NML_375
k
karrde
Voice
Posts: 2
Joined: Wed Jan 21, 2009 11:05 pm

Post by karrde »

Perfect! Thanks to your code, I'm a lot closer than I was before. Thanks for your help!

I did notice though that I can't actually do "echo write #mychan blah" because echo interprets the # as the start of a remark :) Oh, and I had to change

Code: Select all

listen 12345 IncomingConnection
to

Code: Select all

listen 12345 script IncomingConnection
because otherwise it'd complain about an invalid type.

But anyway, thanks again!
n
nml375
Revered One
Posts: 2860
Joined: Fri Aug 04, 2006 2:09 pm

Post by nml375 »

Sorry for that, two of those nasty typos that just slip through no matter how many times you re-read your post before submitting. Updated previous post to correct both issues.
NML_375
Post Reply