Skip to content

Instantly share code, notes, and snippets.

@sarahg
Last active November 29, 2019 23:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sarahg/f778807090ce4098f67d9f0cab7c2a46 to your computer and use it in GitHub Desktop.
Save sarahg/f778807090ce4098f67d9f0cab7c2a46 to your computer and use it in GitHub Desktop.
This is a Golang script to generate a MySQL query that you can use to block an IP range in Drupal 8. Run it like this: go run blockrange.go 123.456.789 (in a situation where you need to block 123.456.789.*). For Drupal 7, change the table name in row 12 to blocked_ips.
package main
import (
"fmt"
"strconv"
"os"
)
func main() {
baseIP := "('" + os.Args[1] + "."
queryStart := "INSERT INTO ban_ip (ip) VALUES"
var ipList string
for n := 0; n <= 255; n++ {
ipList = ipList + baseIP + strconv.Itoa(n) + "')"
if n == 255 {
continue;
}
ipList = ipList + ", "
}
queryEnd := ";"
fmt.Println(queryStart + ipList + queryEnd)
}
@sarahg
Copy link
Author

sarahg commented Nov 29, 2019

Would be nice to make this interactive (prompt for the baseIP)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment