There's several ways you could automate this out.
The fastest way would probably be to use the API to log into each device, and set the host name.
If you're not comfortable around the API, but can create one config file per device named after, for example, the only IP address on an interface named 'outside', you could paste something like this:
:local server [:resolve "your.webserver.com"]; :local url ([:pick [/ip address get [/ip address find interface=outside] address] 0 [:find [/ip address get [/ip address find interface=outside] address] "/"]] . ".txt"); :local fileName "hostnamescript"; /tool fetch mode=http address=$server src-path=$url dst-path=$fileName; /import hostnamescript; /file remove hostnamescript
into each device. On a router with 126.96.36.199/24 on interface 'outside', that would attempt to download http://your.webserver.com/188.8.131.52.txt
to "hostnamescript", execute that file and delete it.
184.108.40.206.txt would then be a script file along the lines of
/system identity set "whatever"
Again, the API would be faster and more resilient to errors.
Specific answers require specific questions. When in doubt, post the output of "/ip address print detail", "/ip route print detail", "/interface print detail", "/ip firewall export", and an accurate network diagram.