Report comment

Thank you very much for this tutorial. It is exactly what I needed.
I have a pi 3 with stretch. I followed every step and it works. But there are some problems:
When the pi finds my wifi network and goes into client mode, I have problems connecting to it via ssh. The connection will eventually be established, but it takes ages. Also some other things do not work properly, e.g. youtube-dl, apt-get update, ...
When the pi runs in ap mode, ssh works just fine.

I reversed everything as you described and all is back to normal. However, I would really love to use your script. Do you have any idea what the problem might be?

Here is some output from ssh when the pi runs in client mode, maybe it helps:

ssh -v pi@raspberrypi.local
OpenSSH_7.5p1, LibreSSL 2.5.4
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: /etc/ssh/ssh_config line 52: Applying options for *
debug1: Connecting to raspberrypi.local [fe80::4123:a746:4449:c48b%en1] port 22.
debug1: connect to address fe80::4123:a746:4449:c48b%en1 port 22: Operation timed out
debug1: Connecting to raspberrypi.local [169.254.87.255] port 22.
debug1: Connection established.