The Book of Legends

Note: This legend is originally found in the Talmud, but the excerpt is from The Book of Legends.

Solomon - King and Commoner (page 129)

"I got me male and female demons" (Eccles 2:8) [Male and female demons?] for what purpose did King Solomon get them? Because it is said, "The house, when it is in buildnig, is to be built of stone made ready before it is brought thither: so that neither hammer nor ax nor any tool of iron shall be heard in the house, while it is in building." (1 Kings 6:7) Solomon asked the sages, "Then how can it be built?" They said to him, "you could build it if you had the shamir, which moses brought to cut the stones in the ephod (High Priest's breastplate)." Solomon asked, "Where is the shamir to be found?" They replied, "Get a male and a female demon, and press them hard one against the other. Perhaps they will tell you." So Solomon went and got a male and female demon, and pressed them one against the other. But the demons said to him, "We do not know. Perhaps Ashmedai, king of the demons, knows." Solomon asked them, "Where is he?" They answered, "He is in such-and-such a mountani, where he has dug himself a pit, filled it with water, covered it with stone, and sealed it with his signet. Every day Ashmedai goes up to heaven and studies in the academy on high. Then he comes down to the earth and studies in the academy below. After that, he examines his seal, opens te pit, and drinks from it; then he covers it again, seals it, and goes on his way."

At once Solomon sent for Benaiah the son of Jehoiada and gave him a chain and a signet ring with the Ineffable Name engraved on each. He also gave him wool fleece and jugs of wine.

Benaiah went and dug a ditch from below to Ashmedai's pit and drained the water into it. Then he stopped up the ditch with the wool fleece. Above the pit he dug another ditch, through which he poured the wine into Ashmedai's pit. He covered both ditches with earth, climbed to the tp of a tree, and seated himself there.

When Ashmedai came, he examined the seal, opened the pit, and found wine. He said, "It is written, 'Wine is a mocker, strong drink is riotous, and whosoever is deceived thereby is not wise.' " (Proverbs 20:1) And so he did not drink. But when he grew very thirsty, he could no longer restrain himself; he drank, became drunk, and fell asleep. Then Benaiah came down and threw over [Ashmedai] the chain [on which the Ineffable Name was engraved] and bound him.When Ashmodai woke up and began to struggle madly, Benaiah said, "The Name of your Master is upon the chain," and grasped him firmly, so that Ashmedai had to go with him. Yet, when Ashmedai came to a palm tree, he rubbed himself against it and threw it down, and when he came to a house, he overturned it. When he reached the hut of a widow, she came out and begged mercy of him. So he jerked his bulk away from the hut so abruptly that he broke a bone, at which he said, "This bears out the verse 'A soft tongue breaketh a bone.' " (Prov. 25:15) When he saw a blind man wandering off the path, he brought him back. When he saw a drunken man staggering off the path, he brought him back. When he saw a wedding procession on its merry way, he wept. When he heard a man say to a shoemaker, "Make me a pair of shoes that will last seven years,' he laughed. When he saw a diviner practicing divination upon a loaf of bread, he laughed again.

After they arrived in Jerusalem, Ashmedai was not taken before Solomon for three days. On the first day he asked, "Why am I not summoned before the king?" and when he was told "Drink has overcome the king," he picked up a brick and placed it upon another. When this was reported to Solomon, he said, "By this Ashmedai means that you are to give me more wine to drink." The next day Ashmedai again asked, "Why am I not summoned before the king?" and when he was told, "Too much food constrains the king," Ashmedai took the brick and set it back on the ground. When they came and reported this to Solomon, he said, "Ashmedai meant that you are to deny me food."

At the end of the third day, when Ashmedai was brought before Solomon, he measured off four cubits on a reed and, throwing it before Solomon, said, "When you are dead, you will own nothing in this world but four cubits [of earth]. Yet now, having sundued the whole world, you were not satisfied until you also subdued me." Solomon replied, "I want nothing at all that is yours. But because I desire to build the Temple, I need the shamir." Ashmedai said to him, "The shamir was not placed in my charge but given to the prince of the sea, and he gave sole charge of it to the wild cock (literally "splitter of mountains"), who is entrusted with it on his oath. Do you nkow what he does with it? He takes it to an uninhabited mountain and sets it down upon the peak, and the mountain splits asunder. Then the wid cock gathers seeds of trees and scatters them in the split, which consequently attracts settlers.

They sought out the nest of the wild cock and found fledgelings in it, so they covered it with a white glass. The wild cock came and tried to get into the nest, but could not. He went and fetched the shamir, and was about to set it down upon the glass that covered the nest.But at that moment they threw a pebble at the wild cock, causing him to drop the shamir, and thus they were able to take it away. When the wild cock realized that he had failed to keep his oath, he strangled himself.

After the shamir was brought in, Benaiah asked Ashmedai, "When you saw the blind man wander off the path, why did you bring him back?" Ashmedai replied, 'Because in heaven it was proclaimed of the blind man that he is a completely righteous man, and that whoever did him a kindness would merit life in the world-to-come.'

'And when you saw the drunken man stagger off the path, why did you bring him back?" "Because in heaven it was proclaimed that he was a completely wicken man, and so I helped him enjoy life in this world (thereby denying him life in the next)."

"And when you saw the wedding procession, why did you weep?" "Because the husband was to die within thirty days, and the wife would have to wait thirteen years before the husband's [infant] brother would be of an age to marry her."

'And when you saw the man saw to the shoemaker 'Make me shoes that will last seven years, why did you laugh?" "The man did not have seven days to live, and he wanted shoes for seven years."

"And when you saw the diviner divining, why did you laugh?" "He was sitting over a king's treasure and should have divined and found out what was underneath him!"

The story continues, but everything else is really irrelevent.