Score:-2

How do I make the ssh connection from my country only?

bf flag

I tried to do this: apt install geoip-bin geoip-database

I created this script in: /usr/local/bin/

nano filter.sh

#!/bin/bash
ALLOW_COUNTRIES="SA"
if [ $# -ne 1 ]; then
  echo "Usage:  `basename $0` <ip>" 1>&2
  exit 0
fi

COUNTRY=`/usr/share/GeoIP $1 | awk -F ": " '{ print $2 }' | awk -F "," '{ print $1 }' | head -n 1`

[[ $COUNTRY = "IP Address not found" || $ALLOW_COUNTRIES =~ $COUNTRY ]] && RESPONSE="ALLOW" || RESPONSE="DENY"
if [ $RESPONSE = "ALLOW" ]
then
  exit 0
else
  logger "$RESPONSE sshd connection from $1 ($COUNTRY)"
  exit 1
fi

chmod +X filter.sh

to enable it, In /etc/hosts.deny I added:

sshd: ALL

in /etc/hosts.allow I added:

sshd: ALL: aclexec /usr/local/bin/filter.sh %a

but the problem is when I type bash filter.sh anyIP, I get /usr/share/GeoIP is a directory error.

uz flag
Jos
There are several packages and utilities called GeoIP. Which one are you using, and is `/usr/share/GeoIP` an executable?
Bodo avatar
pt flag
What's not clear in `/usr/share/GeoIP is a directory`? How would you use geoip manually? Add a link to the instructions you followed. Copy&paste the output of `dpkg -L geoip-bin`.
ibraheem mohsen avatar
bf flag
this is the package: apt install geoip-bin geoip-database. no, /usr/share/GeoIP, but How I can use the database provided?
ibraheem mohsen avatar
bf flag
@Bodo: https://www.axllent.org/docs/ssh-geoip/
Bodo avatar
pt flag
@ibraheemmohsen Please [edit] your question to add requested information, don't use comments for this purpose. The linked instructions tell you to run a command `geoiplookup`, and the output of `dpkg -L geoip-bin` would show you that this command is in `/usr/bin/geoiplookup`.
Score:1
cn flag
raj

According to these descriptions (geoip-bin, geoip-database), /usr/share/GeoIP is indeed a directory, being a part of geoip-database package, that contains two database files, GeoIP.dat and GeoIPv6.dat.

You cannot use a directory name as a command. You also cannot use a database file as a command. You have to use a name of an actual executable command that can be run. This will be probably /usr/bin/geoiplookup from the package geoip-bin (see description in the first link).

So replace /usr/share/GeoIP in your script with /usr/bin/geoiplookup and it should work.

I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.