Linux利用重定向三步搞定请求百度主页源代码

Posted 小鹏说

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Linux利用重定向三步搞定请求百度主页源代码相关的知识,希望对你有一定的参考价值。

第一步:

新建文件描述符,建立与百度通信的socket通道

exec 8<> /dev/tcp/www.baidu.com/80
 
命令解释:
8:新建的文件描述符
<>:既要发送请求,又要接收响应数据
/dev/tcp:这个目录看不到,但内核确实有
 
 
一旦执行该命令,就会新建一个socket连接
 
删除文件描述符:
exec 8<& -

第二步:

发送请求头:

echo -e "GET / HTTP/1.0\\n" 1>& 8
 
命令解释:
-e:使其识别\\n为换行符
GET:请求方式
/:请求内容为主页
HTTP/1.0:HTTP协议1>& 8将标准输出重定向到 8(上面的文件描述符代表的socket)
 
执行该命令则立马向百度发送请求,百度响应的数据存在文件描述符8中

第三步:

读取响应信息

cat 0<& 8

命令输出:

 1 HTTP/1.0 200 OK
 2 Accept-Ranges: bytes
 3 Cache-Control: no-cache
 4 Content-Length: 14615
 5 Content-Type: text/html
 6 Date: Sat, 19 Jun 2021 07:46:24 GMT
 7 P3p: CP=" OTI DSP COR IVA OUR IND COM "
 8 P3p: CP=" OTI DSP COR IVA OUR IND COM "
 9 Pragma: no-cache
10 Server: BWS/1.1
11 Set-Cookie: BAIDUID=52DAE4825F1D4579953CBB941D796B2F:FG=1; expires=Thu, 31-Dec-37 23:55:55 GMT; max-age=2147483647; path=/; domain=.baidu.com
12 Set-Cookie: BIDUPSID=52DAE4825F1D4579953CBB941D796B2F; expires=Thu, 31-Dec-37 23:55:55 GMT; max-age=2147483647; path=/; domain=.baidu.com
13 Set-Cookie: PSTM=1624088784; expires=Thu, 31-Dec-37 23:55:55 GMT; max-age=2147483647; path=/; domain=.baidu.com
14 Set-Cookie: BAIDUID=52DAE4825F1D4579C616C67938B0A43C:FG=1; max-age=31536000; expires=Sun, 19-Jun-22 07:46:24 GMT; domain=.baidu.com; path=/; version=1; comment=bd
15 Traceid: 1624088784087069850611639006944731897298
16 Vary: Accept-Encoding
17 X-Ua-Compatible: IE=Edge,chrome=1
18 
19 <!DOCTYPE html><!--STATUS OK-->
20 <html>
21 <head>
22     <meta http-equiv="content-type" content="text/html;charset=utf-8">
23     <meta http-equiv="X-UA-Compatible" content="IE=Edge">
24     <link rel="dns-prefetch" href="//s1.bdstatic.com"/>
25     <link rel="dns-prefetch" href="//t1.baidu.com"/>
26     <link rel="dns-prefetch" href="//t2.baidu.com"/>
27     <link rel="dns-prefetch" href="//t3.baidu.com"/>
28     <link rel="dns-prefetch" href="//t10.baidu.com"/>
29     <link rel="dns-prefetch" href="//t11.baidu.com"/>
30     <link rel="dns-prefetch" href="//t12.baidu.com"/>
31     <link rel="dns-prefetch" href="//b1.bdstatic.com"/>
32     <title>百度一下,你就知道</title>
33     <link href="http://s1.bdstatic.com/r/www/cache/static/home/css/index.css" rel="stylesheet" type="text/css" />
34     <!--[if lte IE 8]><style index="index" >#content{height:480px\\9}#m{top:260px\\9}</style><![endif]-->
35     <!--[if IE 8]><style index="index" >#u1 a.mnav,#u1 a.mnav:visited{font-family:simsun}</style><![endif]-->
36     <script>var hashMatch = document.location.href.match(/#+(.*wd=[^&].+)/);if (hashMatch && hashMatch[0] && hashMatch[1]) {document.location.replace("http://"+location.host+"/s?"+hashMatch[1]);}var ns_c = function(){};</script>
37     <script>function h(obj){obj.style.behavior=\'url(#default#homepage)\';var a = obj.setHomePage(\'//www.baidu.com/\');}</script>
38     <noscript><meta http-equiv="refresh" content="0; url=/baidu.html?from=noscript"/></noscript>
39     <script>window._ASYNC_START=new Date().getTime();</script>
40 </head>
41 <body link="#0000cc"><div id="wrapper" style="display:none;"><div id="u"><a href="//www.baidu.com/gaoji/preferences.html"  onmousedown="return user_c({\'fm\':\'set\',\'tab\':\'setting\',\'login\':\'0\'})">搜索设置</a>|<a id="btop" href="/"  onmousedown="return user_c({\'fm\':\'set\',\'tab\':\'index\',\'login\':\'0\'})">百度首页</a>|<a id="lb" href="https://passport.baidu.com/v2/?login&tpl=mn&u=http%3A%2F%2Fwww.baidu.com%2F" onclick="return false;"  onmousedown="return user_c({\'fm\':\'set\',\'tab\':\'login\'})">登录</a><a href="https://passport.baidu.com/v2/?reg&regType=1&tpl=mn&u=http%3A%2F%2Fwww.baidu.com%2F"  onmousedown="return user_c({\'fm\':\'set\',\'tab\':\'reg\'})" target="_blank" class="reg">注册</a></div><div id="head"><div class="s_nav"><a href="/" class="s_logo" onmousedown="return c({\'fm\':\'tab\',\'tab\':\'logo\'})"><img src="//www.baidu.com/img/baidu_jgylogo3.gif" width="117" height="38" border="0" alt="到百度首页" title="到百度首页"></a><div class="s_tab" id="s_tab"><a href="http://news.baidu.com/ns?cl=2&rn=20&tn=news&word=" wdfield="word"  onmousedown="return c({\'fm\':\'tab\',\'tab\':\'news\'})">新闻</a>&#12288;<b>网页</b>&#12288;<a href="http://tieba.baidu.com/f?kw=&fr=wwwt" wdfield="kw"  onmousedown="return c({\'fm\':\'tab\',\'tab\':\'tieba\'})">贴吧</a>&#12288;<a href="http://zhidao.baidu.com/q?ct=17&pn=0&tn=ikaslist&rn=10&word=&fr=wwwt" wdfield="word"  onmousedown="return c({\'fm\':\'tab\',\'tab\':\'zhidao\'})">知道</a>&#12288;<a href="http://music.baidu.com/search?fr=ps&key=" wdfield="key"  onmousedown="return c({\'fm\':\'tab\',\'tab\':\'music\'})">音乐</a>&#12288;<a href="http://image.baidu.com/i?tn=baiduimage&ps=1&ct=201326592&lm=-1&cl=2&nc=1&word=" wdfield="word"  onmousedown="return c({\'fm\':\'tab\',\'tab\':\'pic\'})">图片</a>&#12288;<a href="http://v.baidu.com/v?ct=301989888&rn=20&pn=0&db=0&s=25&word=" wdfield="word"   onmousedown="return c({\'fm\':\'tab\',\'tab\':\'video\'})">视频</a>&#12288;<a href="http://map.baidu.com/m?word=&fr=ps01000" wdfield="word"  onmousedown="return c({\'fm\':\'tab\',\'tab\':\'map\'})">地图</a>&#12288;<a href="http://wenku.baidu.com/search?word=&lm=0&od=0" wdfield="word"  onmousedown="return c({\'fm\':\'tab\',\'tab\':\'wenku\'})">文库</a>&#12288;<a href="//www.baidu.com/more/"  onmousedown="return c({\'fm\':\'tab\',\'tab\':\'more\'})">更多»</a></div></div><form id="form" name="f" action="/s" class="fm" ><input type="hidden" name="ie" value="utf-8"><input type="hidden" name="f" value="8"><input type="hidden" name="rsv_bp" value="1"><span class="bg s_ipt_wr"><input name="wd" id="kw" class="s_ipt" value="" maxlength="100"></span><span class="bg s_btn_wr"><input type="submit" id="su" value="百度一下" class="bg s_btn" onmousedown="this.className=\'bg s_btn s_btn_h\'" onmouseout="this.className=\'bg s_btn\'"></span><span class="tools"><span id="mHolder"><div id="mCon"><span>输入法</span></div><ul id="mMenu"><li><a href="javascript:;" name="ime_hw">手写</a></li><li><a href="javascript:;" name="ime_py">拼音</a></li><li class="ln"></li><li><a href="javascript:;" name="ime_cl">关闭</a></li></ul></span><span class="shouji"><strong>推荐&nbsp;:&nbsp;</strong><a href="http://w.x.baidu.com/go/mini/8/10000020" onmousedown="return ns_c({\'fm\':\'behs\',\'tab\':\'bdbrowser\'})">百度浏览器,打开网页快2秒!</a></span></span></form></div><div id="content"><div id="u1"><a href="http://news.baidu.com" name="tj_trnews" class="mnav">新闻</a><a href="http://www.hao123.com" name="tj_trhao123" class="mnav">hao123</a><a href="http://map.baidu.com" name="tj_trmap" class="mnav">地图</a><a href="http://v.baidu.com" name="tj_trvideo" class="mnav">视频</a><a href="http://tieba.baidu.com" name="tj_trtieba" class="mnav">贴吧</a><a href="https://passport.baidu.com/v2/?login&tpl=mn&u=http%3A%2F%2Fwww.baidu.com%2F" name="tj_login" id="lb" onclick="return false;">登录</a><a href="//www.baidu.com/gaoji/preferences.html" name="tj_settingicon" id="pf">设置</a><a href="//www.baidu.com/more/" name="tj_briicon" id="bri">更多产品</a></div><div id="m"><p id="lg"><img src="//www.baidu.com/img/bd_logo.png" width="270" height="129"></p><p id="nv"><a href="http://news.baidu.com">&nbsp;</a> <b>&nbsp;</b> <a href="http://tieba.baidu.com">&nbsp;</a> <a href="http://zhidao.baidu.com">&nbsp;</a> <a href="http://music.baidu.com">&nbsp;</a> <a href="http://image.baidu.com">&nbsp;</a> <a href="http://v.baidu.com">&nbsp;</a> <a href="http://map.baidu.com">&nbsp;</a></p><div id="fm"><form id="form1" name="f1" action="/s" class="fm"><span class="bg s_ipt_wr"><input type="text" name="wd" id="kw1" maxlength="100" class="s_ipt"></span><input type="hidden" name="rsv_bp" value="0"><input type=hidden name=ch value=""><input type=hidden name=tn value="baidu"><input type=hidden name=bar value=""><input type="hidden" name="rsv_spt" value="3"><input type="hidden" name="ie" value="utf-8"><span class="bg s_btn_wr"><input type="submit" value="百度一下" id="su1" class="bg s_btn" onmousedown="this.className=\'bg s_btn s_btn_h\'" onmouseout="this.className=\'bg s_btn\'"></span></form><span class="tools"><span id="mHolder1"><div id="mCon1"><span>输入法</span></div></span></span><ul id="mMenu1"><div class="mMenu1-tip-arrow"><em></em><ins></ins></div><li><a href="javascript:;" name="ime_hw">手写</a></li><li><a href="javascript:;" name="ime_py">拼音</a></li><li class="ln"></li><li><a href="javascript:;" name="ime_cl">关闭</a></li></ul></div><p id="lk"><a href="http://baike.baidu.com">百科</a> <a href="http://wenku.baidu.com">文库</a> <a href="http://www.hao123.com">hao123</a><span>&nbsp;|&nbsp;<a href="//www.baidu.com/more/">更多&gt;&gt;</a></span></p><p id="lm"></p></div></div><div id="ftCon"><div id="ftConw"><p id="lh"><a id="seth" onClick="h(this)" href="/" onmousedown="return ns_c({\'fm\':\'behs\',\'tab\':\'homepage\',\'pos\':0})">把百度设为主页</a><a Nginx中处理重定向端口丢失问题

iFrame 在手机上重定向和重新加载主页问题

text 将所有请求重定向到不存在的文件和目录到主页

HAproxy指南之haproxy重定向配置(案例篇)

中毒了,百度域名被重定向

Node.js 应用程序登录重定向