Passing AT commands from ESP32 to GSM via UART2

Welcome

Try print instead of write

Also remove delay(6000); but don't spam Serial2.print("AT");, only send when necessary

Edit: and more importantly you didn't initialize Serial2 correctly
Serial2.begin( 115200, SERIAL_8N1, RXD2, TXD2 );