Python IRC bot won't join

2020-03-04 03:12发布

问题:

I get the error message

:irc.evilzone.org NOTICE AUTH :* Looking up your hostname...

:irc.evilzone.org NOTICE AUTH :* Found your hostname (cached)

PING :7091A8FB

:irc.evilzone.org 451 JOIN :You have not registered

:irc.evilzone.org 451 PRIVMSG :You have not registered

server = "irc.evilzone.org" # Server 
port = 6667 #port connect through IRC standard is :(6667 or 9999)
irc = socket.socket ( socket.AF_INET, socket.SOCK_STREAM )
irc.connect ( ( server, port ) )
print irc.recv ( 4096 )
nick = 'Piebot' #bots name
chan = 'test' #channel
version= "1.0" #current version
irc.send ( 'NICK Pizebot\r\n' ) 
irc.send ( 'USER Pizebot Pibot Pibot :Python IRC\r\n' )
irc.send ( 'JOIN #test\r\n' ) # YOU MUST CHANGE THE CHANNEL HERE AND BELOW!!
irc.send ( 'PRIVMSG #test :Hello World.\r\n' )

while True:
    readbuffer= irc.recv(4096)

    temp=string.split(readbuffer, "\n")
    Check = readbuffer.split(':')
    print readbuffer

Keeping in mind that some of the commands I use need the temp= string.split(readbuffer,"\n") portion of the code.But with code like this

network = 'irc.evilzone.org'
port = 6667
irc = socket.socket ( socket.AF_INET, socket.SOCK_STREAM )
irc.connect ( ( network, port ) )
print irc.recv ( 4096 )
irc.send ( 'NICK ipbot\r\n' )
irc.send ( 'USER ipbot completely real :Jxxx\r\n' )
irc.send ( 'JOIN #test\r\n' )
irc.send ( 'PRIVMSG #test:Oh Hai.\r\n' )
while True:
   data = irc.recv ( 4096 )

I can successfully connect to the channel etc. Any idea?

回答1:

I see two possible reasons for that:

  • You are sending the commands to early. Usually you need to wait quite a while before the connection is completely established and the server accepts your commands (especially JOIN). So you need to wait for server responses first (after sending a NICK command, you get a MODE command back at some point; after that, you can call normal commands, including JOIN).
  • The other possibility, which might not be the real solution here but is still important if you want to stay on the IRC server, is the PING. The server randomly sends a PING command. You are usually required to respond with a PONG command (parameter the same as the received PING). Otherwise the server might kick you.


回答2:

I noticed that you do not handle PING requests, some servers do not accept any other commands until you have replied to the PING request (hence not registered). You would want to connect, then NICK, check for a PING, then USER, check for PING again if there was none before USER.

Some servers like to send it after NICK, others after USER.

PING :7091A8FB\r\n

To respond to this PING, simply send:

PONG :7091A8FB\r\n

Between the : and '\r\n will be a random string that you need to send back with your PONG as shown above.



回答3:

The time between sending "USER ..." and "JOIN ..." needs to be increased. I have encountered this issue while doing the same code in Bash. Here's how I did it:

#!/bin/bash
(
echo NICK bashscript
echo USER bashscript 8 \* : Centreon Notifier
sleep 2
# echo 'JOIN #netops'
echo 'PRIVMSG #netops' $1
echo QUIT
) | nc 127.0.0.1 6667


回答4:

PING :7091A8FB

This is the issue preventing you from registering on the IRC Server.

While you should (technically) be able to register on IRC with a NICK / USER combination, the PING you're receiving on logon is a very simple DoS protection mechanism employed by most IRC servers these days.

You need to reply to a ping as follows:

PONG :7091A8FB

The string should change every single time you receive the PING. You will also get PING requests later to make sure the connection is still alive, so writing the code to reply will ensure that the server doesn't automatically QUIT you (ping timeout)

Lastly, you should be waiting until you have logged on (you'll know because you will receive the raw numeric 001) before you send JOIN / PRIVMSG / other commands.



回答5:

This is probably a problem with your client. You can double-check this by connecting to the server with telnet and issue commands similar to this:

NICK aaron
USER aaron ignored ignored :Aaron
PONG <number>

(After issuing the 'NICK' command, you should get a 'PING' from the client with a number; this is the number you should substitute for "" above.)

This should connect you with the server, and you should receive the MOTD and other connect messages immediately after this. From here, you can try "JOIN #test-channel" and ensure that you can join channels. Assuming all of this works as I have described, your problem is likely with your IRC client.

 sec@irc:~/simple-irc-bot$ telnet 192.168.1.100 6667
 Trying 192.168.1.100...
 Connected to 192.168.1.100.
 Escape character is '^]'.
 NOTICE AUTH :*** Looking up your hostname
 NOTICE AUTH :*** Checking Ident
 NOTICE AUTH :*** Couldn't look up your hostname
 NICK TENOTICE AUTH :*** No ident response
 NICK testtest002
 PING :2153560274
 :loal.irc-server.com 461 TNICK USER :Not enough parameters
 USER test test 0 :sec
 PONG :2153560274
 :loal.irc-server.com 001 TNICK :Welcome IRC Network,
 :loal.irc-server.com 002 TNICK :Your host is loal.irc-server.com, running version u2.10.12.14

Please try PONG :2153560274 behind USER command.



标签: python irc