Ubuntu/Scripts

From r00tedvw.com wiki
(Difference between revisions)
Jump to: navigation, search
(Code)
(Learned)
Line 58: Line 58:
 
  WRONG: hostname ≠ `hostname`
 
  WRONG: hostname ≠ `hostname`
 
  RIGHT: HOSTNAME = `hostname`
 
  RIGHT: HOSTNAME = `hostname`
*you can call a standard command and define its output as a variable, so long as you use " ` " around the command
+
*you can call a standard command and define its output as a variable, so long as you use " ` " around the command (old school) or "$( )" around it
 
  WRONG: HOSTNAME ≠ 'hostname'
 
  WRONG: HOSTNAME ≠ 'hostname'
 
  RIGHT: HOSTNAME = `hostname`
 
  RIGHT: HOSTNAME = `hostname`
 +
RIGHT: HOSTNAME = $(hostname)
 
*when using printf you should be careful which type of quote to use.  a single quote should be used for plain text, such as a static heading to an output.  a double quote should be used for defining variables.
 
*when using printf you should be careful which type of quote to use.  a single quote should be used for plain text, such as a static heading to an output.  a double quote should be used for defining variables.
 
  WRONG: printf "Address: " '$IP' "\n"
 
  WRONG: printf "Address: " '$IP' "\n"

Revision as of 12:54, 23 June 2014

Contents

Email current IP address

Variables needing to be defined:

  • sender domain (@sennder domain.com)
  • recipient (recipient address)
  • recipient mail exchanger (recipient mail exchanger)

Variables automatically defined

  • HOSTNAME - defined by using the hostname command
  • DATE - defined by using the date command
  • IP - queries checkip.dyndns.org and then parses the return to exclude everything except the IP address
  • UPTIME - defined by using the uptime command
  • USERS - defined by using the users command

Code

#!/bin/bash -x

#define variables
HOSTNAME=$(hostname)
from="$HOSTNAME"'@sender domain.com'
to='recipient address'
recipient_mx='recipient mail exchanger'
DATE=$(date)
messageid='[email protected]'
IP=$(curl checkip.dyndns.org|awk '/Address:/ {print$6}'|sed 's/<.*/ /')
UPTIME=$(uptime)
USERS=$(users)

#function containing mail commands.  Contains enough RFC required fields to satisfy gmail's requirements to allow the message, though it may be detected as spam if you do not have a SPF record.
function mail_input {
        sleep 1
        echo 'ehlo test.com\n'
        sleep 0.5
        printf 'mail from:<'"$from"'>\n'
        sleep 1
        printf 'rcpt to:<'"$to"'>\n'
        sleep 1
        printf 'data\n'
        sleep 0.5
        printf 'Return-Path: <'"$from"'>\n'
        printf 'Date: '"$DATE"'\n'
        printf 'Message-ID: <'"$messageid"'>\n'
        printf 'Subject: '"$HOSTNAME"' ip address= '"$IP"'\n'
        printf 'From: <'"$from"'>\n'
        printf 'To: <'"$to"'>\n'
        printf 'hostname='"$HOSTNAME"'\n'
        printf 'ip address= '"$IP"'\n'
        printf 'uptime= '"$UPTIME"'\n'
        printf 'users logged in= '"$USERS"'\n'
        printf '.\n'
        printf 'quit'
}

#sending mail with mail_input function
mail_input | telnet $recipient_mx 25

Learned

  • you cannot define a variable as the same exact spelling of the command.
WRONG: hostname ≠ `hostname`
RIGHT: HOSTNAME = `hostname`
  • you can call a standard command and define its output as a variable, so long as you use " ` " around the command (old school) or "$( )" around it
WRONG: HOSTNAME ≠ 'hostname'
RIGHT: HOSTNAME = `hostname`
RIGHT: HOSTNAME = $(hostname)
  • when using printf you should be careful which type of quote to use. a single quote should be used for plain text, such as a static heading to an output. a double quote should be used for defining variables.
WRONG: printf "Address: " '$IP' "\n"
RIGHT: printf 'Address: ' "$IP" '\n'

adding -x to your #!/bin/bash statement will cause everything to be printed, useful in troubleshooting

#!/bin/bash -x
Personal tools
Namespaces

Variants
Actions
Navigation
Mediawiki
Confluence
DevOps Tools
Ubuntu
Ubuntu 22
Mac OSX
Oracle Linux
AWS
Windows
OpenVPN
Grafana
InfluxDB2
TrueNas
OwnCloud
Pivotal
osTicket
OTRS
phpBB
WordPress
VmWare ESXI 5.1
Crypto currencies
HTML
CSS
Python
Java Script
PHP
Raspberry Pi
Canvas LMS
Kaltura Media Server
Plex Media Server
MetaSploit
Zoneminder
ShinobiCE
Photoshop CS2
Fortinet
Uploaded
Certifications
General Info
Games
Meal Plans
NC Statutes
2020 Election
Volkswagen
Covid
NCDMV
Toolbox