3种常用的ssh端口映射

openssh客户端除了可以作为一个ssh登陆客户端外,还能做一些简单的端口映射,非常使用的。常见的用法有三种: -D 参数 -D [bind_address:]port Specifies a local "dynamic" application-level port forwarding. This works by allocating a socket to listen to port on the local side, optionally bound to the specified bind_address. Whenever a connection is made to this port, the connection is forwarded over the secure channel, and the application protocol is then used to determine where to connect to from the remote machine. Currently the SOCKS4 and SOCKS5 protocols are supported, and ssh will act as a SOCKS server. Only root can forward privileged ports. Dynamic port forwardings can also be specified in the configuration file. -D 指定一个本地端口(如果本地有多个IP的话也可以指定监听某一个IP的端口),充当socks代理的作用的。然后每当有一个到这个端口的链接时,这个链接就被转发到通过ssh隧道转发,然后再从远程服务器上去链接目的地址。1-1024的端口只有root能转发。 ...

May 28, 2012 · 3 min · pm

linux下配置drivel

为了方便有时在linux下更新blog,就安装了一个drivel。服务器地址选为http://xxx.gnuers.org/xmlrpc.php就行了。 测试一下效果

May 28, 2012 · 1 min · pm

ext3文件恢复

其实我一直很少测试一下东西,比如RAID,LVM,因为自己一直用笔记本,从来就没有用过两块硬盘。 现在就是想试试ext3和ext4的文件恢复。所以测试一下 直接 dd if=/dev/zero of=testdisk bs=1024 count=102400 sudo mkfs.ext3 testdisk sudo mount -o loop testdisk disk 这个就自己创建的一个文件块格式化成了一个ext3分区然后挂载上了。 在下面创建了点文件,然后删掉一个目录。安装ext3grep来试试 可以使用ext3greptestdisk –ls –inode 2把分区内的文件夹列出来 然后逐级根据inode找到你想恢复的文件的inode 找到需要恢复的文件后直接使用 ext3grep testdisk –restore-file d/c/haha2 恢复d/c/haha2 这时就可以在本地文件夹下的RESTORED_FILES下找到需要的文件了,目录结构是和之前的相同的。 也直接使用–dump-names把目录里的文件都列出来。 ext3grep testdisk –dump-names 偷懒一点的人会直接使用–restore-all直接把整个目录都恢复回来,这样会造成有些很老的不需要的也被恢复了。这是可以使用–after和–before参数来解决。 ext3grep testdisk –restore-all –after 1338133162 –before 1338133251 –histogram=dtime 具体的时间值其实在–ls –inode的时候可以看到的,当前的时间可以date “+%s”查看。 对于ext4的恢复可以尝试使用extundelete,用法和ext3grep类似的。这里可以看到使用的帮助文档。 这里但是我测试了下没有成功恢复回来,不知道是不是因为这两年的ext4的一些特性变化了,extundelete是2010年release的。

May 27, 2012 · 1 min · pm

一个供联调服务器使用的nginx代理服务架构

背景: 公司需要和很多外部商户合作连调。目前是每一个项目都会同时修改很多应用系统,有的项目修改的需要被外部商户访问。目前是都会给需要被外部商户访问的系统分配一个公网的IP,并且还需要网络工程师给每一个IP地址配置一堆网络白名单。目前的工作流程是开发/测试申请向我们申请开通某个测试服务器的公网IP地址。然后我们会先向网络安全工程师提出申请,评估同意后再向网工提需求新建公网IP,并配置好商户的IP白名单。整个流程麻烦而且效率低,影响所有人的工作效率。所以想了下不如搞个代理服务器来解决整个问题。只需要使用一台或者几台nginx服务器做代理即可,而且是可以提供HTTPS服务。 基本思路: 代理服务器具有公网IP地址,其实就是上机路由器会把几个使用的端口做NAT到私网的这个代理服务器,外围的网络防火墙是全部开放此服务器的80,8080,443端口(22端口不开),对商户的IP限制由iptables+nginx做。如果某些系统由于特殊原因需要直接向公网开发的话则另外在一个单独的代理服务器上做。由于有的时候让商户绑定host可能比较困难,因此没有使用虚拟主机的方式,而是直接给URL里添加一段。比如对于APP1和APP2,如果它们发布的WS服务是这样: APP1http://APP1.net:8080/services/appser1?wsdlAPP2http://APP2.net:8080/services/appser2?wsdl那么提供给外部商户的URL可以改成是 [http://APP2.net:8080/](http://app2.net:8080/)[services/appser2?wsdl](http://acctransquery.alipay.net:8080/services/acctransSystemQueryFacade?wsdl) APP1http://APP1.net:8080/APP1_net/services/appser1?wsdlAPP2http://APP2.net:8080/APP2_net/services/appser2?wsdlnginx的配置nginx.conf:worker_processes 1; error_log logs/error.log ; pid logs/nginx.pid; events {worker_connections 1024; }http {include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; gzip off; log_format main ‘$remote_addr -> $upstream_addr $host $remote_user [$time_local] “$request”‘‘ $status $body_bytes_sent “$http_referer” ‘‘$http_user_agent “$http_x_forwarded_for” $request_time $upstream_response_time’; server {listen 8080; proxy_set_header Host “$host:$server_port”; proxy_set_header ORIG_CLIENT_IP $remote_addr; proxy_set_header X-Forwarded-By $server_addr:$server_port; proxy_set_header X-Forwarded-For $remote_addr; client_max_body_size 10m; client_body_buffer_size 128k; proxy_buffer_size 128k; proxy_buffers 8 256k; proxy_busy_buffers_size 256k; proxy_http_version 1.1; proxy_set_header Connection “keep-alive”; access_log logs/access.log main; location / {root html; index index.html index.htm; if ( $uri ~* ^/([a-z_0-9]+)/(.*) ) {set $appenv $1_$server_port; set $myurl $2; rewrite ^ /$myurl break; proxy_passhttp://$appenv; } [http://APP2.net:8080/](http://app2.net:8080/)[services/appser2?wsdl](http://acctransquery.alipay.net:8080/services/acctransSystemQueryFacade?wsdl) http://$appenv allow 10.3.3.2;deny all; ...

May 26, 2012 · 2 min · pm

c使用regex库进行正则匹配

C语言其实也可以直接使用自带的regex库进行一些基础的匹配操作,其实主要就是使用regcomp,regexec, regerror和regfree.具体的用法man一下就可以了。下面是一个范例。 $ cat mygrep.c #include #include #include #include #define BUF_SIZE 8192 int main(int argc,char *argv[]) { regex_t regex; int reti; char line_buf[BUF_SIZE]; FILE *FP; long int count=0; printf("regex is: %s\n",argv[1]); reti=regcomp(®ex,argv[1],REG_EXTENDED); if(reti){ perror("complie regex expression error!\n"); } if( (FP=fopen(argv[2],"r"))==NULL){ perror("open file failed"); } while(fgets(line_buf,BUF_SIZE,FP)){ ```bash reti=regexec(®ex,line_buf,0,NULL,0); if (reti==0){ ++count; } } regfree(®ex); printf("%ld\n",count); fclose(FP); return 0; }

May 25, 2012 · 1 min · pm

RHEL的telnet client bug

有时在线上服务器上可以看到log用户使用的telnet进程占用了大量的CPU资源,这两天又遇到这样的事情,所有就查了一下。这个场景可以在CentOS 4.8,5.8上都得到重现。 ssh 127.0.0.1 “telnet test1.me.net 80” [email protected]’s password: Trying 10.253.85.208… Connected to test1.me.net (10.23.85.208). Escape character is ‘^]’. Killed by signal 2. 这个时候top可以看到telnet经常耗CPU 100%。 后来自己在debian下测试发现没有这个问题,debian下的telnet客户端其实也是 netkit-telnet-0.17。但redhat bugzilla网站也有这样的问题,另外我也试了inetutils(1.6,1.7,1.8, 1.9)里面的telnet客户端也没有发现这样的情况。那么比较明显的是可能是debian对 netkit-telnet打了一些patch,在http://packages.debian.org/squeeze/telnet上可以看到确实有一个patch的,然后我就先把netkit-telnet_0.17.orig.tar.gz 这个包下载下来在centos上编译(很多头文件都没有include,还得自己根据make的报错一个一个添加),编译出来后的telnet确实会出现相同的问题,即占用CPU 100%,strace -p PID后可以看到是进入死循环。把debian的patch打上去后,一下顺利编译,而且占用CPU 100%的情况也消失了,看了下那个patch改动还是很多的。 [问题](https://bugzilla.redhat.com/show_bug.cgi?format=multiple&id=337821)

May 25, 2012 · 1 min · pm

ubuntu配置openvpn

在EC2的ubuntu上配置openvpn还是比较简单的,主要是参考了这篇文章。简单的说就是aptitude install openvpn lzop然后在/usr/share/doc/openvpn/examples/easy-rsa/2.0里面依次运行source vars./clean-all./build-ca 自签CA./build-key-server server 签发服务器证书./build-key client 签发客户端证书./build-dh 生成Diffie Hellman参数运行以上命令后会在key目录下生成服务端证书,客户端证书之后就是配置OpenVPN [这篇文章](http://www.vpser.net/build/linode-install-openvpn.html) port 50000 #端口50000proto udp #使用udpdev tun #使用tunca key/ca.crt #根证书cert key/server.crt #证书key key/server.key # This file should be kept secretdh key/dh1024.pemserver 10.10.0.0 255.255.255.0 #ip段ifconfig-pool-persist ipp.txtpush “dhcp-option DNS 8.8.8.8"push “dhcp-option DNS 8.8.4.4” #推送的dnskeepalive 10 30comp-lzo #开启压缩max-clients 60 #最多60个客户端user nobodygroup nogrouppersist-keypersist-tunstatus openvpn-status.logverb 3mute 20 iptables -t nat -A POSTROUTING -s 10.168.0.0/16 -o eth0 -j MASQUERADE内核参数添加net.ipv4.ip_forward = 1 windows和linux下的客户端配置基本都是一样的,以前在学校使用openvpn也是用相同的配置文件的 。clientdev tunproto udpremote xxxx.xxx.xxx 50000resolv-retry infinitenobindpersist-keypersist-tunca ca.crtcert client.crtkey client.keyns-cert-type servercomp-lzoverb 3mute 20keepalive 20 60redirect-gateway #一定注意这个,这个是修改默认路由策略。

May 25, 2012 · 1 min · pm