Last active
May 3, 2020 01:45
-
-
Save agusibrahim/2ccc9c1ca5c77bf2c73c7bf8e69544ce to your computer and use it in GitHub Desktop.
ftp connection checker
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import ftplib, time, urllib2, urllib | |
from configparser import ConfigParser | |
config=ConfigParser() | |
config.read("ftp_config.ini") | |
# atur disini yaa | |
HOSTNAME="localhost" | |
PORT=14147 | |
FTP_USER="user" | |
FTP_PASSWD="secret" | |
CHECK_DELAY=60 | |
if "FTPSettings" in config: | |
if "host" in config['FTPSettings']: | |
HOSTNAME=config['FTPSettings']["host"] | |
if "port" in config['FTPSettings']: | |
PORT=int(config['FTPSettings']["port"]) | |
if "user" in config['FTPSettings']: | |
FTP_USER=config['FTPSettings']["user"] | |
if "pwd" in config['FTPSettings']: | |
FTP_PASSWD=config['FTPSettings']["pwd"] | |
if "delay" in config['FTPSettings']: | |
CHECK_DELAY=int(config['FTPSettings']["delay"]) | |
else: | |
config['FTPSettings'] = {} | |
input_host=raw_input("HOSTNAME: ") | |
if input_host: | |
config['FTPSettings']["host"]=input_host | |
input_port=raw_input("PORT: ") | |
if input_port: | |
config['FTPSettings']["port"]=input_port | |
input_user=raw_input("USER: ") | |
if input_host: | |
config['FTPSettings']["user"]=input_user | |
input_pwd=raw_input("PASSWORD: ") | |
if input_pwd: | |
config['FTPSettings']["pwd"]=input_pwd | |
input_delay=raw_input("LONGPOOL INTERVAL (min): ") | |
if input_delay: | |
config['FTPSettings']["delay"]=input_delay | |
with open("ftp_config.ini","w") as configfile: | |
config.write(configfile) | |
ftp = ftplib.FTP() | |
trying_attemp=0 | |
has_fail_connect=False | |
def kirim_notif(err, success): | |
msg="..." | |
if(success): | |
msg=time.ctime()+" FTP kembali terhubung. "+str(err); | |
else: | |
msg=time.ctime()+" - FTP Server ga jalan. "+str(err) | |
data = urllib.urlencode({"chat_id": "-438461546", "text": msg}) | |
req = urllib2.Request("https://api.telegram.org/bot1173852760:AAHL8mtH7o_rvpiwre4EIyPJ1m46Elmen90/sendMessage", data) | |
response = urllib2.urlopen(req) | |
#print response.read() | |
# notif disini | |
def coba_konek(): | |
global trying_attemp, has_fail_connect | |
try: | |
ftp.connect(HOSTNAME, PORT) | |
ftp.login(FTP_USER, FTP_PASSWD) | |
msg=ftp.getwelcome() | |
if has_fail_connect: | |
has_fail_connect=False | |
kirim_notif(msg, 1) | |
trying_attemp=0 | |
print "Connected. "+msg | |
except Exception as e: | |
print("gagal terhubung ke ftp. "+str(trying_attemp+1)+". "+str(e)) | |
trying_attemp-=-1 | |
if trying_attemp<3: | |
time.sleep(3) | |
coba_konek() | |
else: | |
kirim_notif(e, 0) | |
has_fail_connect=True | |
trying_attemp=0 | |
print("Monitor FTP Berjalan") | |
while 1: | |
try: | |
coba_konek() | |
time.sleep(CHECK_DELAY*60) | |
except KeyboardInterrupt: | |
print("\nMenghentikan pengecekan... OK\nSelamat bekerja!") | |
import sys | |
sys.exit() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment