Q:

Create a shell script to logon to a server and email disk usage

I would like to create a shell script that it will login to a server, get the disk usage from the server, email to me, and then exit from server. Also, if possible to do it daily. This is what I have:
#!/usr/bin/bash

ssh -p 1111 server@server.example.com

CURRENT=$(df / | grep / | awk '{ print $5}' | sed 's/%//g')

mail -s 'Disk Space Alert' username@example.com << EOF
Disk usage is at:  $CURRENT%
EOF
fi

the name of the shell script is example.sh
When I run it, it keeps asking me for password and once I am logged in nothing happens.

A:

First, generate a public key for use to log-in to the remote system without a password:
ssh-keygen -t rsa -b 4096 -C "your_username"

This will generate a 4096-bit RSA key in your ~/.ssh/ directory with your user-name as the label. Then, add this new key to your ssh-agent so that it is used when you connect via SSH.
ssh-agent -s
ssh-add ~/.ssh/id_rsa

If you set a custom name during key generation, replace id_rsa with that name.
Now, copy your public key contents to your clipboard.
clip < ~/.ssh/id_rsa.pub

Next, log-in to the remote server with ssh -p YOURPORT user@remote.host and edit the remote user's authorized_keys file.
vim .ssh/autorized_keys

Paste in the contents you just copied to your clipboard.  This allows connections from your computer to the remote host without password, instead using certificates (usually much more secure, anyway).
Now, you need to make a script, example-script.sh, in your remote host's home directory(make sure to chmod a+x example-script.sh afterward to set it as executable).
#!/usr/bin/env bash
CURRENT=$(df / | awk '/\// { print $5}' | sed 's/%//g')
mail -s 'Disk Space Alert' username@example.com << EOF
Disk usage is at:  $CURRENT%
EOF

And now, create a script in your local home directory, local-script.sh to run the command later easily(again, make sure to make it executable after).
#!/usr/bin/env bash
ssh -P YOURPORT user@remote.host ./example-script.sh

This script allows you to run ./local-script.sh from your home directory to run your remote script in just one line. You can then add this as an alias to your .bashrc file so that you can just run local-script or whatever other alias you may want. You can also use the absolute path(/home/your_username/local-script.sh) in crontab to make it run at a specific time every day.

A:

Firstly to explain why nothing happens, most likely because of the syntax. Your ssh command is of the form: ssh server, which is when you only want to login, and then manually type some commands interactively. But if you want some command to be run automatically after login, you should be doing : ssh server somecommand, so just having somecommand on the next line will not work.
Well, if you want a system to both email disk space info whenever you want, and at the same time support scheduled emails, I would recommend a three part system:

one script on the server that obtains the disk info and email logic, for example at /home/yourusername/script.sh
crontab to run the script.sh
for your arbitrary, on-a-whim requests you can ssh and execute the script

script.sh
This is the script you put on the server, for example at /home/yourusername/script.sh.
#!/bin/bash

s="Disk Space Alert: $(df / | grep / | awk '{ print $5}')"

mail -s $s username@example.com

# Feedback
echo $s

I removed the sed because it simply removed % sign but then you were adding it back later, this seems a redundant use of sed so I removed it.
Secondly, I have condensed the disk usage info to fit just in the subject line, if this doesn't work then of course you can revise it to your original form, but let everyone know either way so we can learn if there were any errors etc
crontab
Edit crontab on your server, for example ssh into your server and then proceed with these commands
$ crontab -e

You know the format is
# m h dom mon dow  command

so for daily every day 8 am
0 8 * * * /home/yourusername/script.sh

So as long as you have tested the script already and know it works to send emails, then crontab will run the script and cause the email to be sent at 8 am every day
arbitrary request to get disk info emailed
You just ssh but put the script command on the end
ssh -p 1111 server@server.example.com /home/yourusername/script.sh

This uses ssh syntax ssh server somecommand so it logs in, executes whatever command and returns results to your local standard out.
Because it has the feedback line, not only should it email, but you also see the Disk Space Alert: ... message so that you at least get immediate feedback that your script was run or not.

