UNIX: Send Mail using SMTP Server - unix

I would like to send an email through a remote SMTP server. How can this be done? I will be sending the email from a bash shell script.
I'm using a unix machine. uname -a returns:
Linux linux 2.4.21 BrandZ fake linux i686 athlon i386 GNU/Linux

Contacting a remote SMTP server directly is not generally the way this is done. What, for example, if the server is temporarily unavailable?
The easier route is to run a local mailserver such as postfix, exim or qmail, and set it up to send mail through a remote server. Then you can just use command-line sendmail to send your e-mail.
In postfix on Ubuntu, I put the following in master.cf:
relayhost = [smtp.my-isp.com]
smtp_generic_maps = hash:/etc/postfix/generic
You need the last line in case your ISP's mail server requires that all outgoing mail originates from you#your-isp.com. Then you'll also need /etc/postfix/generic like this:
youruser#localhost you#your-isp.com
Add other variants (e.g. youruser#yourbox.yourdomain) as necessary.

The mailx command can be used to send email non-interactively,
but the classical implementation talks to a local mail transport agent.
There are simple MTAs which will just send email through a remote SMTP server,
and won't accept any local email.
Another solution is to use heirloom-mailx,
(formerly known as nail I think)
which supports talking to an SMTP server directly instead of using a local MTA.
For instance, you may invoke it as follows:
heirloom-mailx -S smtp=smtp.your-isp.com \
-S from=you#your-isp.com \
-s "subject" <<EOM
Hello, $name,
This is an automatic reminder, sent out once a month, ...
[the rest of your message]
EOM

i'm not sure if it still works.. but you might wanne use telnet. at least you should be able to write a bashscript using it

Related

How can I control mouse by sending unix domain packets to x.org server using echo and netcat

I know that tools like xdotool can do the job, but it's not pre-installed.
I want to use the low-level API (i.e. the X Window client and server protocol directly).
I connect the server using nc -U /tmp/.X11-unix/X0 successfully in Ubuntu 20.04, but I don't know which command to send.

[asyncssh]: Reverse ssh tunnels with python asyncssh

I am looking to use asyncssh with python3.7 (asyncio)
Here is what I want to build:
A remote device would be running a client that does a call-home to a centralized server. I want the server to be able to execute commands on client using reverse ssh tunnels on the incoming connection. I cannot use forward ssh (regular ssh) because the client could be behind NAT and server might not know the address of the client. I prefer client doing a call-home and then server managing the client.
The program for a POC should use python3 + an async implementation of ssh. I see asyncssh as the only viable choice (please suggest if you have an alternate):
Client: Connect to server and accepts reverse ssh tunnels to be opened on same outbound connection
Server: Accepts connection from client and keeps the session open. The server then opens reverse ssh tunnel to the client. For e.g. the server program should open 3 reverse ssh tunnnels on the incoming connection. Each of these tunnels would run one command ['ls', 'sleep 30 && date', 'sleep 5 && cat /proc/cpuinfo']
Server program should print the received response for each of these commands (one should come back amost immediately, other after 5 and other after 30).
I looked at the documentation, and I could not see examples of using multiple reverse ssh tunnels.
Anyone has experience using this? Can you point me to examples?
Developer of asyncssh has provided an example:
As of now, this is in develop branch. I have tested it and it does the job perfectly!
https://asyncssh.readthedocs.io/en/develop/#reverse-direction-example
[If you are checking this after a while, you might find it in master documentation.]

Stream content form pipe with Apache2

Is it possible to write an Apache2 service that can pipe content to the client at is being generated?
I would like to setup a simple http service that triggers a build and immediately starts sending stdout (gcc stuff) to the client while the compiling is going on. The goal is that a client can use e.g. curl to test a build:
curl http://myserver.com/testbuild -F "file=#mypkg.tar.gz"
And immediately get to see stdout from the build process on the server.
I think it would be possible somehow using a cgi script, but the trick is to immediately get the stdout, bypassing the buffering. If you do not really need http as a transport protocol, why not use direct tcp streaming via netcat.
On the build server you run a script like:
#!/bin/bash
while true ; do
nc -l -p 8080 -e /path/to/buildscript
done
and when any client connects via
nc <buildservername or ip> 8080
it gets the build stout immediately.
My recommendation would be something different (using jenkins as ci server, I do this even on a cubietruck), but for a quick and small solution, it should be enough. If you need http, you can even get this adding the http header to your build script.

The server rejected SFTP connection, but it listens for FTP connections

When I use WinSCP in Windows to connect to VMware with Ubuntu, it prompted this:
The server rejected SFTP connection, but it listens for FTP connections.
Did you want to use FTP protocol instead of SFTP? Prefer using encryption.
What's the matter?
I can succeed to ping Ubuntu in Windows.
The fact that you can ping the server has nothing to do with what protocols it supports.
The message says that the server does not listen on port 22 (SSH, SFTP), but listens on port 21 (FTP). The point of the message is that WinSCP defaults to SFTP protocol, what is not common. So it tries to help users who expect FTP to be a default. But that's not relevant to you apparently.
As #ps2goat suggested, make sure you setup SSH/SFTP server.
For more details, see the documentation for the error message The server rejected SFTP connection, but it listens for FTP connections.
If you see this error all of a sudden (when SFTP has always worked for you for this particular server), and if you are using CSF (ConfigServer Security & Firewall), then it might be that your IP was blocked for SSH access. Try flushing all blocks. Also, try restarting the SSH server.
Old question but still responding so others might get benefited.
I stumbled upon this error and the first thing I checked was if my ubuntu machine had ssh installed. It was there and the latest version and I still would get this error.
As long as you have ssh access to the target, check the ssh service status and most certainly it'd be found inactive. Turn it on using
sudo service ssh restart
and you should be back in the game.
Do check the status of the SFTP by using
sudo service ssh status
and take any corrective action.

R SSH Tunnel MySQL

I'm looking for a way that will allow me to use an SSH Tunnel to connect to a MySQL Server (as opposed to a file) within R; I'm assuming it'll require a combination of RCurl and RODBC, but I can't seem to get it to work properly.
I came across this post and this post that talk about utilizing SSH to connect to specific files or tables, but I'm hoping to use it as part of a Shiny app that will execute different SQL queries based on input from the user, which would require connecting into the server as opposed to specific files.
I'm assuming the code would look something along these lines x = scp("remote.ssh.host.com", "/home/dir/file.txt", "My.SCP.Passphrase", user="username"), but would I replace the "/home/dir/file.txt" piece with an odbcConnect() statement or replace it with the port number for the specific database I want to access?
Edit: The line I use for a regular odbcConnect() is odbcConnect(dsn, uid = "userid", pwd = "password"). Part of the problem is, I am developing it on Windows, but it will be deployed to a Linux server (handled be someone else) so I'm struggling to figure out what exactly will need to be used in my server.R code for connecting to the database.
Okay, so to test this on Windows, either grab Cygwin, or install OpenSSH so you can run ssh from the command line in Windows, like you would do in Linux.
Once you have ssh running on your Windows box, then try first making a tunnel through SSH. Run this from the command line:
ssh -f <server_user>#<server_ip> -L <unused_local_port>:localhost:<database_remote_port> -N
Obviously, replace everything in '<>' with the appropriate information. It will ask for the password, and remember that this isn't the database password, but the password to the server itself. Notably, the server_ip doesn't have to be the server with the database on it, just any server that is inside the proper subnet and that runs an SSH server, which is pretty much all Linux machines.
Now, setup an ODBC connection, except make the IP localhost, and the port unused_local_port. Now, try connecting to your new ODBC connection in R. If this works you're halfway there.
The next problem is the password, because you will have to enter a password to connect via SSH, but in R you won't be able to input it after a simple system command. So you have to setup some a public/private rsa key pair. Notably, this will make it so that anyone with access to your user/pass on your Windows box will now have automatic access to your server, so be careful. First, generate a SSH key:
ssh-keygen -t rsa
Don't make a passphrase, and save it in the default location. Now, create the directory for your public key on the remote host, and drop your public key in there.
# This creates a directory on the other machine if it wasn't already there. (Type in your password on the remote machine)
ssh <server_user>#<server_ip> mkdir -p .ssh
# This adds your public key to the list of accepted ones:
cat ~/.ssh/id_rsa.pub | ssh <server_user>#<server_ip> 'cat >> .ssh/authorized_keys'
Now try creating your tunnel again from the command line:
ssh -f <server_user>#<server_ip> -L <unused_local_port>:localhost:<database_remote_port> -N
If it doesn't ask you for the password, you have succeeded in creating your keypair. Now you are ready to run your ssh command from the command line. But before you do that, try and kill your ssh command, so you can make sure that R is actually creating the tunnel, and you aren't just reusing an old one. You can do it through Windows Task Manager (Ctrl+Alt+Esc), and just right click and End Process the ssh.exe.
So, just run:
system('ssh -f <server_user>#<server_ip> -L <unused_local_port>:localhost:<database_remote_port> -N')
And then connect to your new tunneled ODBC connection.

Resources