[chengmo@centos5 shell]$ cat</dev/tcp/127.0.0.1/22 SSH-2.0-OpenSSH_5.1 #我的机器shell端口是:22 #实际:/dev/tcp根本没有这个目录,这是属于特殊设备 [chengmo@centos5 shell]$ cat</dev/tcp/127.0.0.1/223 -bash: connect: 拒绝连接 -bash: /dev/tcp/127.0.0.1/223: 拒绝连接 #223接口不存在,打开失败 [chengmo@centos5 shell]$ exec 8<>/dev/tcp/127.0.0.1/22 [chengmo@centos5 shell]$ ls -l /proc/self/fd/ 总计 0 lrwx------ 1 chengmo chengmo 64 10-21 23:05 0 -> /dev/pts/0 lrwx------ 1 chengmo chengmo 64 10-21 23:05 1 -> /dev/pts/0 lrwx------ 1 chengmo chengmo 64 10-21 23:05 2 -> /dev/pts/0 lr-x------ 1 chengmo chengmo 64 10-21 23:05 3 -> /proc/22185/fd lrwx------ 1 chengmo chengmo 64 10-21 23:05 8 -> socket:[15067661] #文件描述符8,已经打开一个socket通讯通道,这个是一个可以读写socket通道,因为用:"<>"打开 [chengmo@centos5 shell]$ exec 8>&- #关闭通道 [chengmo@centos5 shell]$ ls -l /proc/self/fd/ 总计 0 lrwx------ 1 chengmo chengmo 64 10-21 23:08 0 -> /dev/pts/0 lrwx------ 1 chengmo chengmo 64 10-21 23:08 1 -> /dev/pts/0 lrwx------ 1 chengmo chengmo 64 10-21 23:08 2 -> /dev/pts/0 lr-x------ 1 chengmo chengmo 64 10-21 23:08 3 -> /proc/22234/fd
#!/bin/sh #testhttphead.sh #实现通过主机名,端口读取web 服务器header信息 #copyright chengmo,qq:8292669 if(($#<2));then echo "usage:$0 host port"; exit 1; fi #如果参数缺失,退出程序,返回状态1 exec 6<>/dev/tcp/$1/$2 2>/dev/null; #打开host的port 可读写的socket连接,与文件描述符6连接 if(($?!=0));then echo "open $1 $2 error!"; exit 1; fi #如果打开失败,$?返回不为0,终止程序 echo -e "HEAD / HTTP/1.1\n\n\n\n\n">&6; #将HEAD 信息,发送给socket连接 cat<&6; #从socket读取返回信息,显示为标准输出 exec 6<&-; exec 6>&-; #关闭socket的输入,输出 exit 0;
[chengmo@centos5 ~/shell]$ sh testhttphead.sh www.baidu.com 80 HTTP/1.1 200 OK Date: Thu, 21 Oct 2010 15:17:23 GMT Server: BWS/1.0 Content-Length: 6218 Content-Type: text/html;charset=gb2312 Cache-Control: private Expires: Thu, 21 Oct 2010 15:17:23 GMT Set-Cookie: BAIDUID=1C40B2F8C676180FD887379A6E286DC1:FG=1; expires=Thu, 21-Oct-40 15:17:23 GMT; path=/; domain=.baidu.com P3P: CP=" OTI DSP COR IVA OUR IND COM " Connection: Keep-Alive [chengmo@centos5 ~/shell]$ sh testhttphead.sh 127.0.0.1 8080 open 127.0.0.1 8080 error!
[chengmo@centos5 ~/shell]$ echo -e "HEAD / HTTP/1.1\n\n\n\n\n"|telnet www.baidu.com 80 Trying 220.181.6.175... Connected to www.baidu.com. Escape character is '^]'. Connection closed by foreign host. #直接给发送,失败 [chengmo@centos5 ~/shell]$ (telnet www.baidu.com 80)<<EOF HEAD / HTTP/1.1 EOF Trying 220.181.6.175... Connected to www.baidu.com. Escape character is '^]'. Connection closed by foreign host. #重定向输入,还是失败?
[chengmo@centos5 shell]$ (echo -e "HEAD / HTTP/1.1\n\n\n\n\n";sleep 2)|telnet www.baidu.com 80 Trying 220.181.6.175... Connected to www.baidu.com. Escape character is '^]'. HTTP/1.1 200 OK Date: Thu, 21 Oct 2010 15:51:58 GMT Server: BWS/1.0 Content-Length: 6218 Content-Type: text/html;charset=gb2312 Cache-Control: private Expires: Thu, 21 Oct 2010 15:51:58 GMT Set-Cookie: BAIDUID=0B6A01ACECD5353E4247E088A8CB345A:FG=1; expires=Thu, 21-Oct-40 15:51:58 GMT; path=/; domain=.baidu.com P3P: CP=" OTI DSP COR IVA OUR IND COM " Connection: Keep-Alive #成功了!加入sleep 居然可以了,sleep 改成1秒也可以
#!/bin/sh
#通过传入ip 以及端口,发送指令获得返回数据
#copyright chengmo qq:8292669
#函数往往放到最上面
function sendmsg()
{
msg=$1;
echo "$1">&8;
getout;
}
#向socket通道发送指令,并且调用获得返回参数
function getout()
{
#read 命令 -u 从打开文件描述符 8 读取数据,-d读取数据忽略掉:\r换行符
while read -u 8 -d $'\r' name;
do
if [ "${name}" == "END" -o "${name}" == "ERROR" ];then
break;
fi;
echo $name;
done
}
#由于:memcached每次通讯完毕,会返回:END或者ERROR(出错),通过判断是否是"END"觉得读取是不是结束,否则循环不会停止
if [ $# -lt 2 ];then
echo "usage:$0 host port [command]";
exit 1;
fi;
[[ $# -gt 2 ]]&&command=$3;
#设置默认值 如果command为定义则为:stats
command="${command=stats}";
host="$1";
port="$2";
exec 8<>/dev/tcp/${host}/${port};
#打开通向通道是8
if [ "$?" != "0" ];then
echo "open $host $port fail!";
exit 1;
fi
sendmsg "$command";
#发送指定命令
sendmsg "quit";
#发送退出通向命令
exec 8<&-;
exec 8>&-;
#关闭socket通道
exit 0;
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有