Good to Know Database

Shell Skript: External IP Publishing Script


Das folgende Shell Skript ermittelt die externe IP-Adresse, welche ihr Router beim Verbindungsaufbau erhalten hat, speichert diese mit einem Timestamp in eine Textdatei und lädt die Datei über SFTP auf einen Webserver hoch.

###########################################################################
##                                                                       ##
##                     External IP Publishing Script                     ##
##                                                                       ##
## Creation:    03.08.2004                                               ##
## Last Update: 20.10.2013                                               ##
##                                                                       ##
## Copyright (c) 2011-2013 by Georg Kainzbauer <http://www.gtkdb.de>     ##
##                                                                       ##
## This program is free software; you can redistribute it and/or modify  ##
## it under the terms of the GNU General Public License as published by  ##
## the Free Software Foundation; either version 2 of the License, or     ##
## (at your option) any later version.                                   ##
##                                                                       ##
###########################################################################
#!/bin/bash

# URL of the SFTP server
SFTPSERVER=Server-FQDN

# Port of the SFTP server
SFTPPORT=SFTP-Port

# Username for the SFTP server
SFTPUSERNAME=Benutzername

# Password for the SFTP server
SFTPPASSWORD=StrengGeheim

# Location on the SFTP server
SFTPDIR=/html

# Name of the status file
FILENAME=ip.txt

# Current timestamp
TIME=$(date +'%d.%m.%Y %H:%M')

# Current external IP address
IP=$(wget -q -O - http://checkip.dyndns.org | sed -e 's/.*: //' -e 's/<.*$//')

# Name of the log file
LOGFILE=/var/log/publish_external_ip.sh

# Who will be informed in case if anything goes wrong (if you do not want to be informed via mail, set this option to "")
MAILNOTIFY=""

###################################################################
# NORMALLY THERE IS NO NEED TO CHANGE ANYTHING BELOW THIS COMMENT #
###################################################################

# Check status of upload process
function status()
  {
    # Upload failed
    if [ $1 -gt 0 ] ; then
      error "Upload of status file failed!"

    # Upload successfully
    else
      log "External IP address ($IP) uploaded successfully."
    fi
  }

# Print log message to log file
function log()
  {
    echo `date +%d.%m.%Y%t%H:%M:%S` "    LOG:" $1 >>${LOGFILE}
  }

# Report error message
function error()
  {
    # Print error message to log file
    echo `date +%d.%m.%Y%t%H:%M:%S` "    ERROR:" $1 >>${LOGFILE}

    # Report error message by mail
    if [ -n "$MAILNOTIFY" ] ; then
      echo `date +%d.%m.%Y%t%H:%M:%S` "    ERROR:" $1 | mail -s "ERROR while publishing external IP address" $MAILNOTIFY
    fi

    # Exit script with error
    exit 1
  }

# Print timestamp and external IP address to the status file
echo "${TIME}   IP: ${IP}" > /tmp/$FILENAME

# Remove old status file on the SFTP server and upload the new status file
lftp -e "rm sftp://${SFTPUSERNAME}:${SFTPPASSWORD}@${SFTPSERVER}:${SFTPPORT}${SFTPDIR}/${FILENAME}; put -O sftp://${SFTPUSERNAME}:${SFTPPASSWORD}@${SFTPSERVER}:${SFTPPORT}${SFTPDIR} /tmp/${FILENAME}; quit" >/dev/null 2>&1
status $?

# Remove local status file
rm -f /tmp/$FILENAME

# Exit script successfully
exit 0


Dieser Eintrag wurde am 24.01.2016 erstellt.

Direkter Link zu dieser Seite: http://www.gtkdb.de/index_7_2747.html

[ Zur Startseite ]   [ Zur Kategorie ]


Valid XHTML 1.0 Transitional Valid CSS Valid Atom 1.0

© 2004-2018 by Georg Kainzbauer