Posts Tagged “Study”
举几个例子说一下ssh port forward的应用吧,假设:
A:192.168.1.123,linux,有sshd,httpd服务,没有路由,能通过网关访问外网
B:218.104.xxx.xxx,linux,有sshd,有路由
C:172.16.1.123,linux,有sshd,没有路由,能通过网关访问外网
D:192.168.1.111,windows,没有sshd,没有路由,能通过网关访问外网络
其中:A,D通网断,可以互相访问,与C不在同一网段,不能直接访问。
例一:让B访问A的服务
1) 从A上建立远程端口转发:
A#ssh -f -N -g -R9999:localhost:22 -l root 218.104.xxx.xxx
说明:需要认证root@218.104.xxx.xxx的密码,且9999端口在218.104.xxx.xxx未被占用,一旦命令执行成功,在B上运行netstat -an |grep 9999可以看到该端口正在监听。如果不用root,只能创建1024以上的端口监听。
2) 从B上连接A的sshd端口:
B#ssh -p 9999 -l user localhost
说明:B–ssh–>B:9999–>B–ssh–>A:22此时用户从B上即可连接到A的sshd端口,实现了vpn的功能。但是如果用户从B以外仍然无法访问A,即使在B上用ssh -p 9999 -l user 218.104.xxx.xxx都无法登陆。
问题:-g参数本来是允许远程机器访问转发端口的,但是实际使用中似乎不起作用。当不使用-g时,用netstat -na看到的端口监听是ServerName:listen_port的形式,使用-g参数之后看到的情况是*:listen_port的形式(-L,-D时如此,-R无效)。如果要让B以外的机器也能访问A,即能访问B的9999端口,可以在/etc/ssh/sshd_config中加入下面一行:
GatewayPorts Yes
或者用我们后面介绍的方法。
例二:让C访问A的服务,理论上说有两个途径
方法一:在B上建立本地端口转发到自身,并且使用-g参数,允许远程主机访问
B#ssh -f -N -g -L7777:loaclhost:9999 -l root localhost
如果-g起作用(实际尝试中,在有些机器中成功,有些失败,但是端口确实是在监听所有地址,即*:listen_port),则:
C#ssh -p 7777 218.104.xxx.xxx -l user
将能连接到A的sshd服务
C–ssh–>B:7777–ssh–>B->localhost(B):9999–>B–ssh–>A:22
方法二:在C上通过B建立本地端口转发到localhost(B),因为B上可以访问自身9999端口,即可访问到A
C#ssh -f -N -g -L6666:localhost:9999 -l root 218.104.xxx.xxx
C#ssh -p 6666 -l user localhost
C–>localhost(C):6666–ssh–>B–>localhost(B):9999–>B–ssh–>A:22
先到这里,下次介绍-D的作用吧。实践的OS是redhat enterprise 3,由于家里没有试验环境,所以麻烦比较多。
No Comments »
ssh的三个强大的端口转发命令:
ssh -C -f -N -g -L listen_port:DST_Host:DST_port -l user Tunnel_Host
ssh -C -f -N -g -R listen_port:DST_Host:DST_port -l user Tunnel_Host
ssh -C -f -N -g -D listen_port -l user Tunnel_Host
I) 先看看linux下面ssh命令的相关帮助:
# ssh -help
……
-l user Log in using this user name.
用来登录sshd服务器的用户名,相当于user@remote-host。
-f Fork into background after authentication.
后台认证用户/密码,通常和-N连用,不用登录到远程主机。
-p port Connect to this port. Server must be on the same port.
被登录的ssd服务器的sshd服务端口。
-L listen-port:host:port Forward local port to remote address
在本地创建监听端口,一旦有连接建立到该端口,就转发到host:port。
-R listen-port:host:port Forward remote port to local address
在远程创建监听端口,一旦有连接建立到该端口,就转发到host:port。
These cause ssh to listen for connections on a port, and
forward them to the other side by connecting to host:port.
-D port Enable dynamic application-level port forwarding.
在本地建立监听端口,一旦有连接建立到该端口,动态转发,相当于创建了socks代理服务器。
-C Enable compression.
压缩数据传输。
-N Do not execute a shell or command.
不执行脚本或命令,通常与-f连用。
-g Allow remote hosts to connect to forwarded ports.
在-L/-R/-D参数中,允许远程主机连接到建立的转发的端口,如果不加这个参数,只允许本地主机建立连接。注:这个参数我在实践中似乎始终不起作用,参见III)
……
II) 参见:http://cmpp.linuxforum.net/cman-html/man1/ssh.1.html
-L port:host:hostport
将本地机(客户机)的某个端口转发到远端指定机器的指定端口. 工作原理是这样的, 本地机器上分配了一个 socket 侦听 port 端口, 一旦这个端口上有了连接, 该连接就经过安全通道转发出去, 同时远程主机和 host 的 hostport 端口建立连接. 可以在配置文件中指定端口的转发. 只有 root 才能转发特权端口. IPv6 地址用另一种格式说明: port/host/hostport
-R port:host:hostport
将远程主机(服务器)的某个端口转发到本地端指定机器的指定端口. 工作原理是这样的, 远程主机上分配了一个 socket 侦听 port 端口, 一旦这个端口上有了连接, 该连接就经过安全通道转向出去, 同时本地主机和 host 的 hostport 端口建立连接. 可以在配置文件中指定端口的转发. 只有用 root 登录远程主机才能转发特权端口. IPv6 地址用另一种格式说明: port/host/hostport
-D port
指定一个本地机器 “动态的” 应用程序端口转发. 工作原理是这样的, 本地机器上分配了一个 socket 侦听 port 端口, 一旦这个端口上有了连接, 该连接就经过安全通道转发出去, 根据应用程序的协议可以判断出远程主机将和哪里连接. 目前支持 SOCKS4 协议, 将充当 SOCKS4 服务器. 只有 root 才能转发特权端口. 可以在配置文件中指定动态端口的转发.
III) ssh port forward相关配置
一般sshd配置文件路径为/etc/ssh/sshd_config
添加一行:
GatewayPorts Yes
则允许远程主机连接到转发端口,相当于-g
最后是我的理解:
Localhost–>Localhost:listen_port–ssh(encrypt)–>Tunnel_Host–(unencrypt)–>DST_Host:DST_port
Tunnel_Host–>Tunnel_Host:listen_port–ssh(encrypt)–>DST_Host:DST_port
Localhost–>Localhost:listen_port–ssh(encrypt)–>Tunnel_Host–(unencrypt)–>AnyHost:Any_port
No Comments »
A very good article about ssh port forward, the original url is:http://staff.washington.edu/corey/fw/ssh-port-forwarding.html
I’ll give detail explainations in Chinese later.
And the article I upload to my own spalce, the link url is:http://ttrek.net/download/tech/ssh-port-forwarding.html
2 Comments »
一直没有学会emacs:(所以坚持用vim:P
贴几个我常用的plugin,也就是vim的script是:
taglist:可以用来浏览代码,支持c,cpp,java;需要事先安装ctags到可执行路径。
jad.vim:反编译class,需要先拷贝jad到可执行路径。
cscope:是一个更强大的浏览代码的工具,不过在windows下面使用cscope_win可能更方便些。
还有个jcommenter,刚从网上看来的,可以用来自动插入javadoc格式的comment,暂时我还用不上。
刚学来的命令,用于在行首插入行号:
:g/^/exec “s/^/”.strpart(line(“.”).” “, 0, 4)
g:grep全文搜索
/^:匹配行首
/exec:执行后面字符串(”"之间,或者加字符串函数)中的命令
“s/^/”:字符串的前一半,标准的vim的替换命令,s=substitute
.:用来连接两个字符串
strpart(line(“.”).” “, 0, 4):strpart函数,需要三个参数,后两个指明截取字符串的范围,即从0开始取4个字符长;line(“.”).” “:为第一个参数,取行号加上(用.连接)空格
vim6中可以用下面命令替代:
:%s/^/\=strpart(line(‘.’).” “,0,&ts)
猜测:\=应该是取后面函数运算的结果与前面连接,类似bash中`;&ts不详。
这个例子参考http://www.vim.org/tips/tip.php?tip_id=65
最后关注一下vim tips的主页:http://www.vim.org/tips/index.php
1 Comment »
一直想学regular expressions,今天正好要写个url的js验证,网上找来的都不太好用,于是边学边改了一个。验证domain和ip的写的太繁琐,希望以后有空能改进。
function check(){
url=form1.url.value;
var temp = /^[\d^\w]+$/
var special = /^[\\\/\+\*\.\-'"!()?_%=@&~#$:^ ]+$/;
//特殊字符规则:只允许“\/+*.-’”!()?_%=@&~^#$: ”中的一个或多个
/* + means {1,}
* * means {0,}
* ? means {0,1}, not supported everywhere.
* – means from one character to another character
* . means one character
* \w [a-zA-Z0-9]
* \d [0-9]
* \W [^a-zA-Z0-9]
* \D [^0-9]
* \( \) any thing betwen \( and \) is a temp group, and can be replaced by from \1 to \9
* ^ when and only when character ‘^’ is at the beginning of [] that means not includes…
*/
var domain = /^(\w(\-*\w)*\.)+[a-zA-Z]{2,4}$/;
var email = /^\w([\-_.]*\w)*@(\w(\-*\w)*\.)+[a-zA-Z]{2,4}$/;
//email帐号规则:以字母或者数字开头并结尾,中间可以有’-'或’_'或’.’
//@后面的domain规则见url_reg
var ip = /^((1\d\d|2([0-4]\d|5[0-5])|[1-9]\d|\d).){3}(1\d\d|2([0-4]\d|5[0-5])|[1-9]\d|\d).?$/;
var url_reg = /^((http|ftp|https):\/\/)?(((\w(\-*\w)*\.)+[a-zA-Z]{2,4})|(((1\d\d|2([0-4]\d|5[0-5])|[1-9]\d|\d).){3}(1\d\d|2([0-4]\d|5[0-5])|[1-9]\d|\d).?))(\/+.*)*$/;
//url规则:
//协议类型:http,https,ftp中的一种
//domain:以字母或者数字开头并结尾,中间可以有-,根域名必须是2-4位长的字符。
//ip:0.0.0.0.-255.255.255.255. 结尾的’.'可有可无,数字不可以以0开头(0本身除外)
//domain或者ip后面可以有/,也可以没有,如果有/可以添加任何字符(不作判断)
//alert(“temp:” + temp.test(url));
//alert(“special:” + special.test(url));
alert(“domain:” + domain.test(url));
//alert(“email:” + email.test(url));
//alert(“ip:” + ip.test(url));
alert(“url:” + url_reg.test(url));
}
这里有篇文章不错:http://sitescooper.org/tao_regexps.html
No Comments »
例:听网卡eth0是否有TCP数据
tcpdump –i eth0 tcp
例:听网卡eth0是否有IP地址为 192.168.0.1 端口为80的数据
tcpdump –i eth0 host 192.168.0.1 port 80
例:听网卡eth0是否有源IP地址为192.168.0.1且源端口为80的数据
tcpdump –i eth0 src 192.168.0.1 and src port 80
例:听网卡eth0是否有目的IP地址为192.168.0.1且目的端口为80的数据
tcpdump –i eth0 dst 192.168.0.1 and dst port 80
例:将网卡eth0的数据存入到文件aaaa.dmp中
tcpdump –i eth0 –s 1600 –w aaaa.dmp
-s 1600是指设置每个packet的大小为1600字节,默认是68。
No Comments »
1. cvs update/merge
sample1:
cvs update -j1.1.1.1.2.2 -r TEST — arglist.c
1.1.1.1.2.2 is changed version; TEST is parent branch
sample2:
cvs update -jTEST3 -r 1.2 — arglist.c
cvs commit -f -m “no message” — arglist.c
force commit
2. wget download webpage and linked elements
wget -rHcpk -t 5 -l 5 -D Domain url
3. jad decompile all package:
jad -o -r -sjava -dsrc tree/**/*.class
1 Comment »
import module test2:
cd test2
cvs import -I ! -I CVS — test2 TEST V10
cvs import [-options] repository vendortag releasetag
No Comments »
Posted by Tintin in IT, tags: IT, MySQL, Study, Tech Tip
mysql -p pass -u user database < sript.sql;
use mysql;
insert into user (host, user, password) values (‘root’,'%’,'passwd’);
INSERT INTO user (Host,User,Password) VALUES(‘localhost’,'custom’,PASSWORD(‘obscure’));
SET PASSWORD FOR ”@’host_name’ = PASSWORD(‘newpwd’);
FLUSH PRIVILEGES;
show databases;
show tables;
describe TableName;
mysql> GRANT ALL PRIVILEGES ON *.* TO ‘monty’@'localhost’
-> IDENTIFIED BY ‘some_pass’ WITH GRANT OPTION;
mysql> GRANT ALL PRIVILEGES ON *.* TO ‘monty’@'%’
-> IDENTIFIED BY ‘some_pass’ WITH GRANT OPTION;
mysql> GRANT RELOAD,PROCESS ON *.* TO ‘admin’@'localhost’;
mysql> GRANT USAGE ON *.* TO ‘dummy’@'localhost’;
mysql> FLUSH PRIVILEGES;
2 Comments »
/etc/ppp/ppp.conf
//**************************//
default:
set log Phase tun command
set ifaddr 10.0.0.1/0 10.0.0.2/0
njadsl:
set device PPPoE:fxp0 # replace fxp0 with your Ethernet device
set authname b00265276@adsl # replace with your adsl account
set authkey 86667024 # replace with your password
set dial
set login
add default HISADDR
//**************************//
use ifconfig to check the Ethernet device
dial command:
# ppp -ddial njadsl
succeeded, but got a warning, ignore it:P
# ifconfig
……
tun0: ……
inet 221.226.190.204 –> 218.2.135.62 netmask 0xffffffff
Opened by PID 930
use netstat -r to check the route info
#route delete default
#route add default 218.2.135.62
succeeded
/etc/rc.conf
//**************//
ppp_enable=”YES”
ppp_mode=”ddial”
ppp_nat=”YES”
ppp_profile=”njadsl”
//**************//
defaul browser mozilla
no chinese input now
but mpd is the recommended way, I’ll try later
and wanna try nat support:P
3 Comments »
|