Ubuntu HowTo: Postfix SMTP mail server External smtp relay

Original Source Link

How can I configure postfix smtp?

  • External smtp relay
  • Smtp authentication
  • Email send rate limit
  • Schedule emails

If you go to https://help.ubuntu.com and enter “postfix” into the search box, you’ll get a number of helpful articles, including this one https://help.ubuntu.com/community/Postfix to help install and configure postfix.

I’ve installed postfix on PC without static IP address and without buying anything. I’ve used SendGrid’s free plan account and my organization’s microsoft mailbox.

External SMTP Server configuration

  1. Go to sendgrid and register a profile
  2. Go to Sender Authentication and create a new sender (Picture)
    Specify some not-free email (I used office 365 Online account), so Sender could be verified by SendGrid.
  3. Check your mailbox and click Verify Single Sender button in the letter (Picture)
  4. Go to SMTP Relay integration page and create an apikey, you will put in postfix’s sasl_passwd file (Picture). Do not close a verification page till you insert it there and verify.

Postfix configuration:

Install some packages and configure postfix:

sudo apt install postfix mailutils

Configure /etc/postfix/main.cf file the next way, adding or modifying the next lines:

smtpd_banner = $myhostname ESMTP $mail_name (Ubuntu)
biff = no
append_dot_mydomain = no
compatibility_level = 2
smtpd_tls_cert_file=/etc/ssl/certs/ssl-cert-snakeoil.pem
smtpd_tls_key_file=/etc/ssl/private/ssl-cert-snakeoil.key
smtpd_tls_security_level=may

smtp_tls_CApath=/etc/ssl/certs
smtp_tls_security_level=encrypt
smtp_tls_session_cache_database = btree:${data_directory}/smtp_scache

smtpd_relay_restrictions = permit_mynetworks permit_sasl_authenticated defer_unauth_destination
myhostname = localhost.localdomain

alias_maps = hash:/etc/aliases
alias_database = hash:/etc/aliases
myorigin = /etc/mailname
mydestination = $myhostname, localhost
relayhost = [smtp.sendgrid.net]:587
smtp_sasl_auth_enable = yes
smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd
smtp_sasl_tls_security_options = noanonymous

Create /etc/postfix/sasl_passwd file (api secret key would be gained further):

[smtp.sendgrid.net]:587 apikey:SG.kjaksdjkfajskdk_ASDk.lkjaoIO_Kjkoaofs3i99asfd_kkjasdof99882348IKII

Apply settings:

sudo postmap /etc/postfix/sasl_passwd
sudo systemctl restart postfix

Test email sending during SendGrid’s verification

  1. Click Next Verify Integration button. You’ll be on Let’s test your integration page.
  2. Click the Verify Integration button
  3. Run a different terminal window to see logs, using journalctl -f command
  4. Run a command in a terminal to test mail sending, using Single Sender after -r parameter created in External SMTP Server configuration section.

    echo "Test Email message body" | mail -r [email protected] -s "Email test subject" [email protected]
    

As a result, my gmail.com mailbox received a test letter. I’ve also sent it to my yahoo.com account and it also have been received into a Spam folder after a some time of deferring.

Tagged : / / / /

Ubuntu HowTo: Showing ID of GPUs that are currently being used

Original Source Link

I have 4 GPU cards in my server, I can see which ones are running by nvidia-smi and check if memory is full, but I want to automate this process, is there any commands that can return e.g. GPU device ID = 0,1,2 is being used, instead of eye-ball checking the nvidia-smi? Thank you!

Tagged : /

Linux HowTo: Cat file to terminal at particular speed of lines per second

Original Source Link

I’m lazy and I could write a script to do this, but I’m even too lazy to think of how to do it.

I often do things like :

cris$ python runexperiment.py > output.txt
cris$ cat output.txt

Sometimes when looking at the long output of an experiment I like to let the page just scroll and watch the successive patterns form and disperse. But using cat on a file with 1 million lines finishes in maybe 5 seconds. This is too quick even for me.

Is there any way that I can slow down the speed of viewing the file, something like a ‘scroll utility’? I want fast, but not 200k lines a second (all of which presumably the display would never even register anyway).

Something like

cris$ scroll -lps=300 output.txt

And then sitting back and watching 300 lines per second roll past would be ideal, I imagine.

Short and readable:

perl -pe "system 'sleep .003'" log.txt

I post this solutions because they are small and readable, as comments of DMas’s answer seem promote this kind of solution!

But I hate this because: For this run, perl will fork to /bin/sleep 300x / seconds!

This is a big ressource consumer! Also a wrong good solutions!!

Using builtin sleep in

Unfortunely, builtin sleep is limited to integers. So we have to use select instead:

perl -e 'print && select undef,undef,undef,.00333 while <>;'

Under perl, print while <> could be replaced by the -p switch:

perl -pe 'select undef,undef,undef,.00333'

Let’s try:

time /bin/ls -l /usr/bin | perl -pe 'select undef,undef,undef,.00333' | wc
   2667   24902  171131

real    0m9.173s
user    0m0.056s
sys     0m0.048s

bc -l < <(echo 2667/9.173)
290.74457647443584432573

Explanation:

  • 300 lines / sec means 1 line by 0.0033333333 secs.

  • print without argument prints $_ which is default input space.

  • called as ... | perl -e, ... | perl -ne or ... | perl -pe, standard input would be automaticaly assigned to *STDIN which is default file descriptor, so <> would do the same as <STDIN> which will read from standard input until $/ (input record separator which is by default a newline) will be reached. In English, by default <> will read one line from standard input and assign content to $_ variable.

  • && is an and condition, but is used there as a chain command separator so after (successfully) print one line, doing next command.

  • select is a programmer’s trick to not use sleep. This command is designed to trap events on file descriptors (inputs and/or outputs, files, socket and/or net sockets). With this command, a program could wait for 3 kind of events, feed ready to read, feed ready to write and some event happened on feed. The fourth argument is a timeout in seconds, so syntax is select <feeds where wait for input>, <feeds where having to write>, <feed where something could happen>, <timeout>.

For more precision, you could use Time::Hires perl module:

perl -MTime::HiRes -pe 'BEGIN{$start=Time::HiRes::time;$sleepPerLine=1/300};select undef,undef,undef,($start + $sleepPerLine*$. - Time::HiRes::time)'

Note: $. is current input line number.

Better written as cat >catLps.pl

#!/usr/bin/perl -w

use strict;
use Time::HiRes qw|time|;

my $start=time;
my $lps=300;

$lps=shift @ARGV if @ARGV && $ARGV[0]=~/^(d+)$/;
my $sleepPerLine=1/$lps;

print &&
    select undef,undef,undef,($start + $sleepPerLine*$. - Time::HiRes::time)
    while <>

Usage:

catLps.pl [lps] [file] [file]...

First argument lps is optional line per seconds numeric argument (default: 300)

Note: if filename is only numeric, you may have to specifiy them with path: ./3.

Like cat this could pass files given as argument and/or standard input

So we could:

TIMEFORMAT='%R' 
time seq 1 100 | ./catLps.pl 100 >/dev/null 
1.040

time seq 1 10000 | ./catLps.pl 10000 >/dev/null  
1.042

For fun:

export TIMEFORMAT='%R' ;clear ;time seq 1 $((LINES-2)) | ./catLps.pl $((LINES-2))

just use awk with sleep:

awk '{print $0; system("sleep .1");}' log.txt

I’m late to the party, but I found this would be a useful learning exercise to try in python, so I’ll put up what i got:

#!/usr/bin/env python3

import argparse
from time import sleep

parser = argparse.ArgumentParser(description='Echo a file slowly')
parser.add_argument('-i',
                    '--input-file',
                    type=argparse.FileType('r'),
                    default='-')
parser.add_argument('-d',
                    '--delay-in-ms',
                    type=int,
                    default='100')
args = parser.parse_args()

for line in args.input_file:
    print(line.rstrip())
    sleep(args.delay_in_ms/1000.0)

It accepts input from stdin or as an argument (-i) and by default writes one line per 1/10th of a second, but that can be changed with another argument (-d).

Ruby’s sleep function supports floating point values, so here’s an efficient and short solution based on @F.Hauri’s answer:

ruby -pe 'sleep 0.00333' log.txt

or

ruby -pe 'sleep (1.0/300)' log.txt

I like to mention two great tools:

  1. tee
  2. cstream

tee is good for capturing stdout to file as well as stream it. i.e. we can rewrite:

$ python runexperiment.py > output.txt
$ cat output.txt

as

$ python runexperiment.py | tee output.txt

The advantage of the latter version is the output.txt is being built at the same time output is displayed on stdout. So, you don’t have to wait till the end of the experment.

The other tool I mention, cstream, can be used to slow down output, e.g.

$ python runexperiment.py | cstream -t 700k | tee output.txt

Tagged : / / / /

Making Game: Cat file to terminal at particular speed of lines per second

Original Source Link

I’m lazy and I could write a script to do this, but I’m even too lazy to think of how to do it.

I often do things like :

cris$ python runexperiment.py > output.txt
cris$ cat output.txt

Sometimes when looking at the long output of an experiment I like to let the page just scroll and watch the successive patterns form and disperse. But using cat on a file with 1 million lines finishes in maybe 5 seconds. This is too quick even for me.

Is there any way that I can slow down the speed of viewing the file, something like a ‘scroll utility’? I want fast, but not 200k lines a second (all of which presumably the display would never even register anyway).

Something like

cris$ scroll -lps=300 output.txt

And then sitting back and watching 300 lines per second roll past would be ideal, I imagine.

Short and readable:

perl -pe "system 'sleep .003'" log.txt

I post this solutions because they are small and readable, as comments of DMas’s answer seem promote this kind of solution!

But I hate this because: For this run, perl will fork to /bin/sleep 300x / seconds!

This is a big ressource consumer! Also a wrong good solutions!!

Using builtin sleep in

Unfortunely, builtin sleep is limited to integers. So we have to use select instead:

perl -e 'print && select undef,undef,undef,.00333 while <>;'

Under perl, print while <> could be replaced by the -p switch:

perl -pe 'select undef,undef,undef,.00333'

Let’s try:

time /bin/ls -l /usr/bin | perl -pe 'select undef,undef,undef,.00333' | wc
   2667   24902  171131

real    0m9.173s
user    0m0.056s
sys     0m0.048s

bc -l < <(echo 2667/9.173)
290.74457647443584432573

Explanation:

  • 300 lines / sec means 1 line by 0.0033333333 secs.

  • print without argument prints $_ which is default input space.

  • called as ... | perl -e, ... | perl -ne or ... | perl -pe, standard input would be automaticaly assigned to *STDIN which is default file descriptor, so <> would do the same as <STDIN> which will read from standard input until $/ (input record separator which is by default a newline) will be reached. In English, by default <> will read one line from standard input and assign content to $_ variable.

  • && is an and condition, but is used there as a chain command separator so after (successfully) print one line, doing next command.

  • select is a programmer’s trick to not use sleep. This command is designed to trap events on file descriptors (inputs and/or outputs, files, socket and/or net sockets). With this command, a program could wait for 3 kind of events, feed ready to read, feed ready to write and some event happened on feed. The fourth argument is a timeout in seconds, so syntax is select <feeds where wait for input>, <feeds where having to write>, <feed where something could happen>, <timeout>.

For more precision, you could use Time::Hires perl module:

perl -MTime::HiRes -pe 'BEGIN{$start=Time::HiRes::time;$sleepPerLine=1/300};select undef,undef,undef,($start + $sleepPerLine*$. - Time::HiRes::time)'

Note: $. is current input line number.

Better written as cat >catLps.pl

#!/usr/bin/perl -w

use strict;
use Time::HiRes qw|time|;

my $start=time;
my $lps=300;

$lps=shift @ARGV if @ARGV && $ARGV[0]=~/^(d+)$/;
my $sleepPerLine=1/$lps;

print &&
    select undef,undef,undef,($start + $sleepPerLine*$. - Time::HiRes::time)
    while <>

Usage:

catLps.pl [lps] [file] [file]...

First argument lps is optional line per seconds numeric argument (default: 300)

Note: if filename is only numeric, you may have to specifiy them with path: ./3.

Like cat this could pass files given as argument and/or standard input

So we could:

TIMEFORMAT='%R' 
time seq 1 100 | ./catLps.pl 100 >/dev/null 
1.040

time seq 1 10000 | ./catLps.pl 10000 >/dev/null  
1.042

For fun:

export TIMEFORMAT='%R' ;clear ;time seq 1 $((LINES-2)) | ./catLps.pl $((LINES-2))

just use awk with sleep:

awk '{print $0; system("sleep .1");}' log.txt

I’m late to the party, but I found this would be a useful learning exercise to try in python, so I’ll put up what i got:

#!/usr/bin/env python3

import argparse
from time import sleep

parser = argparse.ArgumentParser(description='Echo a file slowly')
parser.add_argument('-i',
                    '--input-file',
                    type=argparse.FileType('r'),
                    default='-')
parser.add_argument('-d',
                    '--delay-in-ms',
                    type=int,
                    default='100')
args = parser.parse_args()

for line in args.input_file:
    print(line.rstrip())
    sleep(args.delay_in_ms/1000.0)

It accepts input from stdin or as an argument (-i) and by default writes one line per 1/10th of a second, but that can be changed with another argument (-d).

Ruby’s sleep function supports floating point values, so here’s an efficient and short solution based on @F.Hauri’s answer:

ruby -pe 'sleep 0.00333' log.txt

or

ruby -pe 'sleep (1.0/300)' log.txt

I like to mention two great tools:

  1. tee
  2. cstream

tee is good for capturing stdout to file as well as stream it. i.e. we can rewrite:

$ python runexperiment.py > output.txt
$ cat output.txt

as

$ python runexperiment.py | tee output.txt

The advantage of the latter version is the output.txt is being built at the same time output is displayed on stdout. So, you don’t have to wait till the end of the experment.

The other tool I mention, cstream, can be used to slow down output, e.g.

$ python runexperiment.py | cstream -t 700k | tee output.txt

Tagged : / / / /

Linux HowTo: Why doesn’t my batch script run all lines?

Original Source Link

I want to create a batch script that will run axis wsdl2java.bat for multiple WSDLs. When I create a batch script with just this code:

wsdl2java.bat
wsdl2java.bat

it would run wsdl2java.bat only once. I guess there is something with wsdl2java.bat batch script itself.

Can someone help me figure out what is the root cause of the problem?

For compatibility with Microsoft’s COMMAND, Microsoft’s CMD has the bizarre semantic that invoking a command script within another command script terminates the invoking command script at that point.

The correct workaround for this is not the START command. Nor is it invoking a subsidiary command interpreter process with CMD (especially erroneously using /K for /C as well). It is — and has been for a couple of decades — the CALL command.

call wsdl2java.bat
call wsdl2java.bat

Use cmd.exe /k filename.bat to launch each batchfile.

If I remember correctly, Windows switches completely to the new batch file when you start it from within another batch file and does not return.

The workaround would be to execute

start wsdl2java.bat

— again, just from the top of my head. I could not find a reliable source in my short google research.

Update: as JdeBP pointed out, mine is the old wrong way. It may have been too obvious that I have not touched the innards of MSDOS or Windows CMD-shell or whatever it’s called these days for some decades.

Update2: jeez, I said “off the top of my head”.

Tagged : / /

Making Game: Why doesn’t my batch script run all lines?

Original Source Link

I want to create a batch script that will run axis wsdl2java.bat for multiple WSDLs. When I create a batch script with just this code:

wsdl2java.bat
wsdl2java.bat

it would run wsdl2java.bat only once. I guess there is something with wsdl2java.bat batch script itself.

Can someone help me figure out what is the root cause of the problem?

For compatibility with Microsoft’s COMMAND, Microsoft’s CMD has the bizarre semantic that invoking a command script within another command script terminates the invoking command script at that point.

The correct workaround for this is not the START command. Nor is it invoking a subsidiary command interpreter process with CMD (especially erroneously using /K for /C as well). It is — and has been for a couple of decades — the CALL command.

call wsdl2java.bat
call wsdl2java.bat

Use cmd.exe /k filename.bat to launch each batchfile.

If I remember correctly, Windows switches completely to the new batch file when you start it from within another batch file and does not return.

The workaround would be to execute

start wsdl2java.bat

— again, just from the top of my head. I could not find a reliable source in my short google research.

Update: as JdeBP pointed out, mine is the old wrong way. It may have been too obvious that I have not touched the innards of MSDOS or Windows CMD-shell or whatever it’s called these days for some decades.

Update2: jeez, I said “off the top of my head”.

Tagged : / /

Ubuntu HowTo: rbash does not work when set as the default shell for a user

Original Source Link

Even though /etc/passwd and $SHELL confirm that the default shell is rbash, the restrictions as described by rbash manual does not apply.
For example, I can change directory with cd, I can change the PATH, etc.

However, when I manually type bash -r, all restrictions are in place.
I’m using Ubuntu 18.04.4 LTS

Any ideas what is causing this behaviour?
Is there any workaround for Ubuntu as it seems to be working on CentOS?

You need to restart the user session, either by logging out and back in or by rebooting the machine, to make the changes take place.

Tagged : /

Linux HowTo: Get default printer name from command line?

Original Source Link

How can I get the name of the default printer on my machine through the Windows command line?

Microsoft Windows XP comes with a VBS script to manage local and network printers from the command line:

  • Get the default printer details from command line:

    cscript C:windowssystem32prnmngr.vbs -g

  • Get the list of printers added to the system from Windows command line:

    cscript C:windowssystem32prnmngr.vbs -l

  • Set default printer from windows command line:

    cscript C:windowssystem32prnmngr.vbs -t -p “Servernameprintername”


Official documentation and more usage examples can be found at:

MS WinXP Product Documentation for the Prnmngr.vbs File

Using a batch file:

@echo off

::Get printer CLSID
for /f %%a in ('reg query HKCUPrintersDefaults') do (set regkey=%%a)
::Get printer name from the previous CLSID

for /f "tokens=3*" %%a in ('reg query %regkey%') do (set printername=%%a %%b)
echo Printer name is: %printername%

I wish that could help you

Tagged : /

Ubuntu HowTo: How can I plot data from sc in the terminal?

Original Source Link

Using the sc (Spreadsheet Calculator) or (Spreadsheet Calculator Improvised) sc-im (more) I can manage spreadsheets in the terminal.

Is there a way to plot this data in the terminal, too?

I’ve found termgraph, which allows to plot in the terminal, but it doesn’t work with data from sc.

Thanks to K7AAYs hint about the fact that sc-im does have plotting integrated, I figured out how to do this:

Press ‘v’ to enter visual mode and highlight the data, then enter “:plot bar” to plot the data as a bar graph. Using “:help” and searching for plot reveals this information. Other than ‘bar’, there’s also ‘line’, ‘scatter’ and ‘pie’.

Tagged : / /

Ubuntu HowTo: L2TP Connection Client on ubuntu 18.04 Server

Original Source Link

I have google arround for the answer of my thread. Most of the result suggesting to use Network Manager, which i believe is not the solution for me as i’m running Ubuntu Server without UI.

Is there anyone who experienced on connecting VPN Tunnel on L2TP with command line ?
Any link or leads very much appreciated.

Thank you

I was looking for that and then I found a started solution here:
https://gist.github.com/pastleo/aa3a9524664864c505d637b771d079c9
but still needs GUI, then I decide to test and I got the complete solution, here it is:

Note: I use Centos 7 but I think it works with Ubuntu

1. Install NetworkManager-l2tp
Centos 7:

yum -y install NetworkManager-l2tp

Ubuntu:
(Will need to add repository before install)

sudo add-apt-repository ppa:nm-l2tp/network-manager-l2tp

apt-get install network-manager-l2tp

2. Run this command as root (make your own changes before running, see below):

nmcli connection add connection.id [vpnName] con-name [vpnName] type VPN vpn-type l2tp ifname -- connection.autoconnect no ipv4.method auto vpn.data "gateway = [ipv4], ipsec-enabled = yes, ipsec-psk = 0s"$(base64 <<<'[PSK]' | rev | cut -c2- | rev)"=, mru = 1400, mtu = 1400, password-flags = 0, refuse-chap = yes, refuse-mschap = yes, refuse-pap = yes, require-mppe = yes, user = [user]" vpn.secrets password=[user-password]

You have to change:
– [vpnName] = The name of your connection
– [ipv4] = ip of the l2tp/ipsec server
– [PSK] = pre shared key from the l2tp/ipsec server
– [user] = user name to connect to
– [user-password] = password of the user to connect

I don’t know if generated PSK will work for you, it works for me. If don’t just connect with another PC with GUI, then copy PSK from file /etc/NetworkManager/system-connections/{vpn_name} and paste on

Thats it!

To show generated file: nmcli c show id [vpnName]
To start the VPN from cli: nmcli c up [vpnName]
To stop the VPN from cli: nmcli c down [vpnName]

Hope it can help because there is no other information how to do that on all Internet!

Enjoy your l2tp/ipsec VPN!

Tagged : / /