Ubuntu/Scripts

From r00tedvw.com wiki
(Difference between revisions)
Jump to: navigation, search
 
(19 intermediate revisions by one user not shown)
Line 1: Line 1:
 +
[[Ubuntu/Scripts|Scripts]] | [[Ubuntu/Scripts/SSL_Expire|SSL Expire]] | [[Ubuntu/Scripts/PulseAudio|PulseAudio]]
 +
 
==Email current IP address==
 
==Email current IP address==
 
====Variables needing to be defined:====
 
====Variables needing to be defined:====
*sender domain  (@sennder domain.com)
+
*sender domain  (@sender domain.com)
 
*recipient (recipient address)
 
*recipient (recipient address)
*recipient mail exchanger (recipient mail exchanger)
 
  
 
====Variables automatically defined====
 
====Variables automatically defined====
Line 9: Line 10:
 
*DATE - defined by using the date 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
 
*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
 +
*RECIP_DOMAIN - defined by parsing the domain from the recipients' address
 +
*RECIP_MX - defined by querying the recipient's MX and picking the record with the highest priority
 +
*SENDER_DOMAIN - defined by parsing the domain from the senders' address, used for the messageid
 +
*MESSAGEID - automatically created using random numbers and the sender's domain<br>
 +
Another way to gen random, which is awesome:
 +
<nowiki>~$ openssl rand -hex 12
 +
~$ openssl rand -base64 12</nowiki>
 +
*BOUNDARY - automatically created for content-type boundaries
  
 
====Code====
 
====Code====
  #!/bin/bash -x
+
  <nowiki>#!/bin/bash -x
+
 
#define variables
+
#static variables
HOSTNAME=`hostname`
+
from="$HOSTNAME"'@sender domain.com'
from="$HOSTNAME"'@sender domain.com'
+
to='recipient address'
to='recipient address'
+
 
recipient_mx='recipient mail exchanger'
+
#dynamic variables
DATE=`date`
+
HOSTNAME=$(hostname)
messageid='blahblahblahblahblah@domain.com'
+
#recipient_mx='recipient mail exchanger'   ---obsolete
IP=$(curl checkip.dyndns.org|awk '/Address:/ {print$6}'|sed 's/<.*/ /')
+
DATE=$(date -R)
UPTIME=`uptime`
+
sender_domain=$(printf "$from" | sed 's/^.*@//')
USERS=`users`
+
messageidrandom=$(od -vAn -N4 -tu4 < /dev/urandom)
+
messageidrandom=$(printf "$messageidrandom" | sed 's/ //')
#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.
+
messageid="$messageidrandom"'@'"$sender_domain"
 +
IP=$(curl checkip.dyndns.org|awk '/Address:/ {print$6}'|sed 's/<.*/ /')
 +
UPTIME=$(uptime)
 +
USERS=$(users)
 +
recip_domain=$(printf "$to" | sed 's/^.*@//')
 +
recip_mx=$(dig +short $recip_domain mx | sort -n | nawk '{print $2;exit}' | sed 's/\(.*\)./\1/')
 +
boundaryrandom=$(cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 32 | head -n 1)
 +
boundary='b1_'"$boundaryrandom"
 +
 
 +
#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 {
 
  function mail_input {
        sleep 1
+
        sleep 1
        echo 'ehlo test.com\n'
+
        printf 'ehlo test.com\n'
        sleep 0.5
+
        sleep 0.5
        printf 'mail from:<'"$from"'>\n'
+
        printf 'mail from:<'"$from"'>\n'
        sleep 1
+
        sleep 1
        printf 'rcpt to:<'"$to"'>\n'
+
        printf 'rcpt to:<'"$to"'>\n'
        sleep 1
+
        sleep 1
        printf 'data\n'
+
        printf 'data\n'
        sleep 0.5
+
        sleep 0.5
        printf 'Return-Path: <'"$from"'>\n'
+
        printf 'Return-Path: <'"$from"'>\n'
        printf 'Date: '"$DATE"'\n'
+
        printf 'Date: '"$DATE"'\n'
        printf 'Message-ID: <'"$messageid"'>\n'
+
        printf 'Message-ID: <'"$messageid"'>\n'
        printf 'Subject: '"$HOSTNAME"' ip address= '"$IP"'\n'
+
        printf 'Subject: '"$HOSTNAME"' ip address= '"$IP"'\n'
        printf 'From: <'"$from"'>\n'
+
        printf 'From: <'"$from"'>\n'
        printf 'To: <'"$to"'>\n'
+
        printf 'To: <'"$to"'>\n'
        printf 'hostname='"$HOSTNAME"'\n'
+
        printf 'Content-type: multipart/alternative;\n'
        printf 'ip address= '"$IP"'\n'
+
        printf '        boundary="'"$boundary"'"\n'
        printf 'uptime= '"$UPTIME"'\n'
+
        printf '\n'
        printf 'users logged in= '"$USERS"'\n'
+
        printf '\n'
        printf '.\n'
+
        printf "%s\n" '--'"$boundary"''
        printf 'quit'
+
        printf 'Content-type: text/plain;\n'
 +
        printf '        charset="UTF-8"\n'
 +
        printf 'Content-transfer-encoding: quoted-printable\n'
 +
        printf '\n'
 +
        printf 'hostname: '"$HOSTNAME"'\n'
 +
        printf 'ip address: '"$IP"'\n'
 +
        printf 'uptime: '"$UPTIME"'\n'
 +
        printf 'users logged in: '"$USERS"'\n'
 +
        printf '\n'
 +
        printf '\n'
 +
        printf "%s\n" '--'"$boundary"''
 +
        printf 'Content-type: text/html;\n'
 +
        printf '        charset="UTF-8"\n'
 +
        printf 'Content-transfer-encoding: quoted-printable\n'
 +
        printf '\n'
 +
        printf '<html>\n'
 +
        printf '<head>\n'
 +
        printf '<meta http-equiv=3D"Content-Type" content="3Dtext/html; charset=3Dutf-8">\n'
 +
        printf '</head>\n'
 +
        printf '<body>\n'
 +
        printf 'hostname: '"$HOSTNAME"'<br>\n'
 +
        printf 'ip address: '"$IP"'<br>\n'
 +
        printf 'uptime: '"$UPTIME"'<br>\n'
 +
        printf 'users logged in: '"$USERS"'<br>\n'
 +
        printf '</body>\n'
 +
        printf '</html>\n'
 +
        printf '\n'
 +
        printf '\n'
 +
        printf "%s\n" '--'"$boundary"'--'
 +
        printf '\n'
 +
        printf '\n'
 +
        printf '\n'
 +
        printf '.\n'
 +
        sleep 3
 +
        printf 'quit'
 +
        sleep 10
 +
        kill $$
 
  }
 
  }
+
 
#sending mail with mail_input function
+
#sending mail with mail_input function
mail_input | telnet $recipient_mx 25
+
#plain text
 +
mail_input | telnet $recip_mx 25
 +
#TLS encrypted
 +
#mail_input | openssl s_client -crlf -starttls smtp -quiet -connect $recip_mx:25
 +
</nowiki>
  
 
====Learned====
 
====Learned====
Line 56: Line 116:
 
  WRONG: hostname &ne; `hostname`
 
  WRONG: hostname &ne; `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 &ne; 'hostname'
 
  WRONG: HOSTNAME &ne; '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"
Line 64: Line 125:
 
adding -x to your #!/bin/bash statement will cause everything to be printed, useful in troubleshooting
 
adding -x to your #!/bin/bash statement will cause everything to be printed, useful in troubleshooting
 
  #!/bin/bash -x
 
  #!/bin/bash -x
 +
*a wise man told me that i'm supposed to separate the headers from the body of the message with (2) linefeeds.  Added to the script.

Latest revision as of 01:17, 14 January 2022

Scripts | SSL Expire | PulseAudio

Contents

[edit] Email current IP address

[edit] Variables needing to be defined:

  • sender domain (@sender domain.com)
  • recipient (recipient address)

[edit] 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
  • RECIP_DOMAIN - defined by parsing the domain from the recipients' address
  • RECIP_MX - defined by querying the recipient's MX and picking the record with the highest priority
  • SENDER_DOMAIN - defined by parsing the domain from the senders' address, used for the messageid
  • MESSAGEID - automatically created using random numbers and the sender's domain

Another way to gen random, which is awesome:

~$ openssl rand -hex 12
~$ openssl rand -base64 12
  • BOUNDARY - automatically created for content-type boundaries

[edit] Code

#!/bin/bash -x

#static variables
from="$HOSTNAME"'@sender domain.com'
to='recipient address'

#dynamic variables
HOSTNAME=$(hostname)
#recipient_mx='recipient mail exchanger'   ---obsolete
DATE=$(date -R)
sender_domain=$(printf "$from" | sed 's/^.*@//')
messageidrandom=$(od -vAn -N4 -tu4 < /dev/urandom)
messageidrandom=$(printf "$messageidrandom" | sed 's/ //')
messageid="$messageidrandom"'@'"$sender_domain"
IP=$(curl checkip.dyndns.org|awk '/Address:/ {print$6}'|sed 's/<.*/ /')
UPTIME=$(uptime)
USERS=$(users)
recip_domain=$(printf "$to" | sed 's/^.*@//')
recip_mx=$(dig +short $recip_domain mx | sort -n | nawk '{print $2;exit}' | sed 's/\(.*\)./\1/')
boundaryrandom=$(cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 32 | head -n 1)
boundary='b1_'"$boundaryrandom"

#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
        printf '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 'Content-type: multipart/alternative;\n'
        printf '        boundary="'"$boundary"'"\n'
        printf '\n'
        printf '\n'
        printf "%s\n" '--'"$boundary"''
        printf 'Content-type: text/plain;\n'
        printf '        charset="UTF-8"\n'
        printf 'Content-transfer-encoding: quoted-printable\n'
        printf '\n'
        printf 'hostname: '"$HOSTNAME"'\n'
        printf 'ip address: '"$IP"'\n'
        printf 'uptime: '"$UPTIME"'\n'
        printf 'users logged in: '"$USERS"'\n'
        printf '\n'
        printf '\n'
        printf "%s\n" '--'"$boundary"''
        printf 'Content-type: text/html;\n'
        printf '        charset="UTF-8"\n'
        printf 'Content-transfer-encoding: quoted-printable\n'
        printf '\n'
        printf '<html>\n'
        printf '<head>\n'
        printf '<meta http-equiv=3D"Content-Type" content="3Dtext/html; charset=3Dutf-8">\n'
        printf '</head>\n'
        printf '<body>\n'
        printf 'hostname: '"$HOSTNAME"'<br>\n'
        printf 'ip address: '"$IP"'<br>\n'
        printf 'uptime: '"$UPTIME"'<br>\n'
        printf 'users logged in: '"$USERS"'<br>\n'
        printf '</body>\n'
        printf '</html>\n'
        printf '\n'
        printf '\n'
        printf "%s\n" '--'"$boundary"'--'
        printf '\n'
        printf '\n'
        printf '\n'
        printf '.\n'
        sleep 3
        printf 'quit'
        sleep 10
        kill $$
 }

#sending mail with mail_input function
#plain text
mail_input | telnet $recip_mx 25
#TLS encrypted
#mail_input | openssl s_client -crlf -starttls smtp -quiet -connect $recip_mx:25

[edit] 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
  • a wise man told me that i'm supposed to separate the headers from the body of the message with (2) linefeeds. Added to the script.
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