ssalam o elaikum,
I am very confused regarding an issue. On one hand the ahlus sunnah wal jamah believe that a prophet of ALLAH can never have a disease or flaw that will make people shun him and stay away from him. But then we have the story of Prophet Ayub alayhissalam and there is a prophetic tradition It was narrated from Anas ibn Maalik (may Allah be pleased with him) that the Messenger of Allah (blessings and peace of Allah be upon him) said:
“Verily Ayyoob, the Prophet of Allah, suffered his trials for eighteen years, when he was shunned by relatives and strangers alike, except for two of his brothers who were among the closest of his brothers.” It continues further……..
Narrated by Abu Ya‘la in al-Musnad, 6/299; Ibn Hibbaan in his Saheeh, 7/159; al-Haakim in al-Mustadrak, 2/635
So on one side we have our core belief that a prophet never have a disease which will cause people to Shun him while on the other side we have a Hadith which many scholars believe that is sahih. As a muslim what is the middle path to take in such situations?
In the Name of Allah, the Most Gracious, the Most Merciful.
The verses of Surah Al-Anbiya (17): 85-86 as well as verses 41 to 44 of Surah Saad (34) make mention of the severe illness that befell The Prophet Ayyoob Alayhis Salaam. However, no verse mentions exactly what disease affected him and for what period he suffered. These facts are not even mentioned in any (sound) Ahadeeth from Rasulullaah ﷺ. However, it is established that he suffered more than the average Nabi and that he lost all his children in the process.
All the narrations that have been mentioned about him find their source from the Bani Israa’eel.
The verses make it clear that he prayed to Allaah for relief from his difficulty and Allaah accepted this du’aa by granting him a cure from his illness. This was intended as a “remembrance for the worshippers.”
“We then granted him his family and the like thereof in addition as a special mercy from Us…” Commentators have mentioned that this could mean that Allaah returned his lost family and children to him, or that Allaah granted him a new family after the first had died.
The numbers of his family were doubled afterwards.
However, it should be borne in mind that the narrations stating that Hadhrat Ayyoob Alayhis Salaam had to lie in the dumps for a long period does not seem authentic because it contradicts the high standards of purity and cleanliness that the Ambiya Alahimus Salaam maintained.
The narrations stating that worms fell off from his body also does not seem correct because this would cause people to shun him, thereby hindering propagation.  Anwaar-ul-Bayaan (Vol – III) pg 357- 358
This claim as you have mentioned regarding the illness of Ayyoub Alayhis salaam and the people shunning him due to scorn is unfortunately quite widespread, but incorrect. It is not found in the noble Hadith of Rasulullah ﷺ. We know and believe is that Ayyoub Alayhis salaam was greatly tested by Allah Ta’ala through illness and loss of family.
According to the Aqeedah of Ahlus Sunnah, Allah Ta’ala protects his Prophets Alayhimus Salaam from any such sickness or quality that will be regarded as something detestable or scorned by the people. Therefore, those unreliable narrations that mention he was shunned and removed from the town and lived by a dump, should be disregarded in the light of the principles of our Aqeedah.
Such claims have been refuted in various reliable commentaries of the Quran (Tafaseer), and one must refrain from such incorrect claims. 
And Allah Ta’āla Knows Best
Mufti Arshad Ali
Darul Iftaa, Jaamia Madinatul Uloom (Trinidad)
 Anwaar-ul-Bayaan (Vol – III) pg 357- 358
 تفسير القرطبي (11/ 323)
السَّابِعُ- أَنَّ دُودَةً سَقَطَتْ «2» مِنْ لَحْمِهِ فَأَخَذَهَا وَرَدَّهَا فِي مَوْضِعِهَا فعقرته فصاح” مَسَّنِيَ الضُّرُّ” فقيل: أعلينا نتصبر. قال ابن العربي: وهذا بعيد جدا مَعَ أَنَّهُ يَفْتَقِرُ إِلَى نَقْلٍ صَحِيحٍ، وَلَا سَبِيلَ إِلَى وُجُودِهِ. الثَّامِنُ- أَنَّ الدُّودَ كَانَ يَتَنَاوَلُ بَدَنَهُ فَصَبَرَ حَتَّى تَنَاوَلَتْ دُودَةٌ قَلْبَهُ وَأُخْرَى لِسَانَهُ، فَقَالَ:” مَسَّنِيَ الضُّرُّ” لِاشْتِغَالِهِ عَنْ ذِكْرِ اللَّهِ. قَالَ ابْنُ الْعَرَبِيِّ: وَمَا أَحْسَنَ هَذَا لَوْ كَانَ لَهُ سَنَدٌ وَلَمْ تَكُنْ دَعْوَى عَرِيضَةً. التَّاسِعُ- أَنَّهُ أَبْهَمَ عَلَيْهِ جِهَةَ أَخْذِ الْبَلَاءِ لَهُ هَلْ هُوَ تَأْدِيبٌ، أَوْ تَعْذِيبٌ، أَوْ تَخْصِيصٌ، أَوْ تَمْحِيصٌ، أَوْ ذُخْرٌ أَوْ طُهْرٌ، فَقَالَ:” مَسَّنِيَ الضُّرُّ” أَيْ ضُرُّ الْإِشْكَالِ فِي جِهَةِ أَخْذِ الْبَلَاءِ. قَالَ ابْنُ الْعَرَبِيِّ: وَهَذَا غُلُوٌّ لَا يُحْتَاجُ إِلَيْهِ
التفسير الواضح (2/ 548)
أما ضر أيوب الذي لحقه فالمفسرون جالوا في تحديده وصالوا، وذكر القرطبي في ذلك خمسة عشر قولا. الأول: أنه وثب ليصلى فلم يقدر على النهوض فقال: مسنى الضر: إخبارا عن حاله لا شكوى لبلائه، وهذا لا ينافي الصبر إلى آخر الأقوال التي ذكرت في تفسير الآية.
والناس يروون في بلاء أيوب أقوالا يوردونها تدل على أنه مرض مرضا مشوها ومنفرا للناس.
وهذا يتنافى مع منصب النبوة، إذ الأنبياء منزهون عن الأمراض المنفرة، ويمكن أن نفهم أن الابتلاء بهذا الشكل كان قبل النبوة فلما صبر وصابر اجتباه الله واختاره نبيا، على أن المبالغين في تصوير ضر أيوب ومرضه إنما اعتمدوا فيما يقولون على ما جاء عند أهل الكتاب في السفر المسمى «سفر أيوب
حاشية الصاوي علي الجلالين 145\3