Linux
修复数据库中被SyntaxHighlighter替换的特殊符号
< > " & '修改自http://quartergeek.com/fix-special-char-replaced-by-syntaxhighlighter
代码增加了单引号的替换,同时增加几个变量提醒用户修改。
自从换上了Crayon Syntax Highlighter,发现原来<>&”’符号被WP替换成了< > " & ‘而Crayon Syntax Highlighter又不能转换,只好转回来,于是写了下面这个脚本,成功地替换回来了。
set_charset("utf8"); $result = $conn->query("SELECT post_content, ID FROM ".$ta); $stmt = $conn->prepare("UPDATE ".$ta." SET post_content = ? WHERE ID = ?"); $search = array("<", ">", """, "&", "'"); $replace = array("<" , ">" , "\"" , "&" ,"'"); while ($row = $result->fetch_array()) { $id = $row['ID']; $post_content = str_replace($search, $replace, $row['post_content']); $stmt->bind_param('si', $post_content, $id); if (!$stmt->execute()) { die("\n[ERROR!]".$id."\n"); } else { echo "[Success]".$id."\n"; } } $result->free(); $conn->close(); ?>
PHP 使用 GeoLiteCity 库解析 IP 为地理位置
关于把 IP 地址转换为地理位置可以使用网络上很多的 API,好处就是不用在本地存储一个 IP 数据库,而且一般网络上的 IP 库会自动更新,不利的地方就是太依赖于网络,性能表现也可能会弱些。比如像下面的 API:
http://api.hostip.info/get_html.php?ip=58.63.236.31
http://api.hostip.info/flag.php?ip=58.63.236.31
这里介绍 PHP 如何使用 GeoLiteCity.dat 库把 IP 转换为地理位置,GeoLiteCity.dat 可以在 http://geolite.maxmind.com/download/geoip/database/GeoLiteCity.dat.gz 下,解压出 GeoLiteCity.dat,即可,我们可以手动去更新新的 IP 库。
下面 PHP 解析 IP 的过程参考自 WordPress 插件 Visitor Maps and Who’s Online 的实现。可以找到该插件的两个文件 include-whos-online-geoip.php 和 visitor-maps.php 告诉了我们怎么做。你可以点击这里的链接下载到这两个文件,我这里把 include-whos-online-geoip.php 改名为 geoipcity.inc.php,然后参考 visitor-maps.php 中的 get_location_info($user_ip) 函数,那么我们可以写出自己的解析 IP 地址的程序 resolve_ip.php:
city)) ? $record->city : ''; //城市 $location_info['state_name'] = (isset($record->country_code) && isset($record->region)) //州名 ? $GEOIP_REGION_NAME[$record->country_code][$record->region] : ''; $location_info['state_code'] = (isset($record->region)) ? strtoupper($record->region) : ''; //州代号 $location_info['country_name'] = (isset($record->country_name)) ? $record->country_name : '--'; //国家名 $location_info['country_code'] = (isset($record->country_code)) ? strtoupper($record->country_code) : '--'; //国家代号 $location_info['latitude'] = (isset($record->latitude)) ? $record->latitude : '0'; //维度 $location_info['longitude'] = (isset($record->longitude)) ? $record->longitude : '0'; //经度 //php 站点设置了 utf-8 字符集必要时进行转码 $charset = 'utf-8'; // this fixes accent characters on UTF-8, only when the blog charset is set to UTF-8 if ( strtolower($charset) == 'utf-8' && function_exists('utf8_encode') ) { if ($location_info['city_name'] != '' ) { $location_info['city_name'] = utf8_encode($location_info['city_name']); } if ($location_info['state_name'] != '') { $location_info['state_name'] = utf8_encode($location_info['state_name']); } if ($location_info['country_name'] != '') { $location_info['country_name'] = utf8_encode($location_info['country_name']); } } return $location_info; } //查询一个 IP 测试下 $record = get_ip_record("206.19.49.154"); var_dump($record); $location = get_location_info("206.19.49.154"); var_dump($location); ?>
执行后输出如下(可以作为系统脚本直接用 php resolve_ip.php 来执行):
object(geoiprecord_VMWO)#2 (10) { ["country_code"]=> string(2) "US" ["country_code3"]=> string(3) "USA" ["country_name"]=> string(13) "United States" ["region"]=> string(2) "TX" ["city"]=> string(10) "Richardson" ["postal_code"]=> string(5) "75080" ["latitude"]=> float(32.9722) ["longitude"]=> float(-96.7376) ["area_code"]=> int(972) ["dma_code"]=> float(623) } array(7) { ["city_name"]=> string(10) "Richardson" ["state_name"]=> string(5) "Texas" ["state_code"]=> string(2) "TX" ["country_name"]=> string(13) "United States" ["country_code"]=> string(2) "US" ["latitude"]=> float(32.9722) ["longitude"]=> float(-96.7376) }
只要取你想要的数据就是了,里面还有诸如区号,邮编等数所,GeoLiteCity.dat 是个二进制文件,比普通文本要紧凑省空间。
不知道您有没有多留一份心,有无浏览链接:http://geolite.maxmind.com/download/geoip/api/php/,是这样的:
看到 GeoIp 给我们提供了不少的例子,那么多 sample.php,而实际上前面用到的 geoipcity.inc.php,就是 geopip.inc、geoipcity.inc 和 geoipregionvars.php 三个程序的内容合体。
再往上看:
官方提供的 API 何止 PHP 啊,几乎能全线满足您的实际需求了,c、java、perl、python、vb、ruby、tcl 等……,放其他程序里以后也不用愁了。
再进到 http://geolite.maxmind.com/download/geoip/database/ 瞧瞧:
正考虑着呢,不是说 IPv4 快用净了吗?IPv6 的数据也正为我们准备着呢?当然,天朝的 IPv9 恐怕永远不会有的。
本只是把 Visitor Maps and Who’s Online 里的解析 IP 的做法抽出来用用,可总能不断 深入再深入,不知道可喜还是可怕了。
其實很久以前就注意到,某些計數器可以很準確的判定IP來源的位置,甚至連所在城市都可以顯示出來
後來才知道是因為有GeoIP這樣的東西
MaxMind – GeoIP | IP Address Location Technology
這是MaxMind開發的東西,有不同平台的API和定期更新的”IP-位置”資料庫
如果要在網站上應用的話,可以找Apache module,perl和php的API
在Apache上面的模組叫”mod_geoip”
さすが,租用的主機空間不可能給我隨便安裝模組,所以先在自己的電腦試…
不過仔細看才發現那檔案是linux上面用的,只有個c檔要自己make install
好吧…只好跳過…不想為了這東西去裝linux
再來試試perl,它的module是”Geo::IP
這也是要安裝的,所以先在自己的電腦試..
不過不知道是不是activeperl的版本問題,根本就裝不起來…orz
在目前的主機上面雖然可以自己安裝perl module,但是卻限制CPAN上面的東西,還好搜尋了一下就發現這個模組
Apache2::Geo::IP
但是不知道為什麼,安裝到最後出現錯誤:
CPAN.pm: Going to build R/RK/RKOBES/Apache-GeoIP-1.99.tar.gz
Please install either mod_perl 1 or mod_perl 2 first at Makefile.PL line 118.
No 'Makefile' created RKOBES/Apache-GeoIP-1.99.tar.gz
/usr/bin/perl Makefile.PL PREFIX=/home/jwintaiw/perl/usr -- NOT OK
Running make install
Make had some problems, won't install
呃阿…orz
還好仔細看,發現主機裡面有預裝另外一個類似功能的”Geo::IPfree”
它會從ip或主機名尋找之後回傳三個變數的陣列:二文字國碼,國家全名,IP
小試了一下可以動作,不過速度無敵慢!
最後嘗試PHP API
網頁上面有geoip和geoipcity兩種,前者是從ip/主機名尋找之後回傳二文字國碼和國家全名;後者是回傳更詳細的位置資訊,包含國家,程式和經緯度
不過兩者資料庫差很多,geoip需要的資料庫大約1MB附近,但是geocity的資料庫快30MB…|||
這邊照著該網頁上放的範例自己弄了一個測試的程式,有興趣可以試試…
http://snow-sugar.net/lookup.php (會放多久不確定,因為database太大)
同樣的東西也在自己電腦的自架網站動作確認,感覺大概像這樣
其實最重要的應用不在這裡,而是可以利用辨識來源IP的國家進而讓server作不同的動作,例如台港的IP就自動轉入中文頁面,歐美的IP就自動轉入英文頁面,韓國的IP就block等等…因為IP對應位置的資料庫有持續更新,也讓誤動做的可能減少
Setting up awstats with apache 2 & geoip on debian-ubuntu
Awstats is a web log analyzer. It gives really useful statistics about traffic on your web sites.This log analyzer works as aCGI or from command line and shows you all possible information your log contains, in few graphical web pages.
yesterday, it took me many hour to configure it on my server, I took help from many blogs to complete it.
Hope this can help someone.
1. Installing Awstats:
ab@web1:~$ sudo apt-get install awstats
This command will install awstats.Now we have to configure Apache.
2. Configuring Apache2:
Now make the following changes inawstats.conf file.
ab@web1:~$ sudo vi /etc/apache2/awstats.conf
Alias /awstatsclasses “/usr/share/awstats/lib/”
Alias /awstats-icon/ “/usr/share/awstats/icon/”
Alias /awstatscss “/usr/share/doc/awstats/examples/css”
ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
ScriptAlias /awstats/ /usr/lib/cgi-bin/
Options ExecCGI -MultiViews +SymLinksIfOwnerMatch
After, Making these changes, we have to include this file (/etc/apache2/awstats.conf) in to our apache.conf file, so we can access it over http.
ab@web1:~$ sudo vi /etc/apache2/apache2.conf
Include /etc/apache2/awstats.conf
Now, reload the apache.
ab@web1:~$ sudo /etc/init.d/apache2 reload
Now you can try to open http://www.mysite.com/awstats/awstats.pl but you will get this error: Error: SiteDomain parameter not defined in your config/domain file. You must edit it for using this version of AWStats.
That means we have to configure Awstats.
3. Configuring Awstats:
By default awstats provides a default configuration file named /etc/awstats/awstats.conf. Copy this file to /etc/awstats/awstats.www.mysite.com.conf:
~$sudo cp /etc/awstats/awstats.conf /etc/awstats/awstats.www.mysite.com.conf
and edit the file:
~$sudo vi /etc/awstats/awstats.www.mysite.com.conf
and check for the following lines and edit those for your needs:
LogFile=”/var/log/apache2/access.log”
SiteDomain=”mysite.com”
This is all setted up, now you need to generate the first stats.
4. Generating the First Stats:
in order to generate the first stats, you need to call the script as root using the following command line:
~:$sudo -u www-data /usr/bin/perl /usr/lib/cgi-bin/awstats.pl -update -config=www.mysite.com
After finishing this you can try to open http://www.mysite.com/awstats/awstats.pl this will show you the stats.
Now for automatic update the stats you need to put the following command in your cron.
0 2 * * * www-data [ -x /usr/lib/cgi-bin/awstats.pl -a -f /etc/awstats/awstats.conf -a -r /var/log/apache/access.log ] && /usr/lib/cgi-bin/awstats.pl -config=mysite.com -update >/dev/null
GeoIP Information for AWStats
Improve country and city location information in your web analytics reports
AWStats offers three different ways to provide Country level information about visitors (“hosts”) connecting to your site. By default, the host domain extension is used if you have performed reverse DNS look-up. While this results in human readable host names as well, reverse DNS lookup is time consuming. Alternatively, GeoIP plugins can be used. They match the host IP with a database of IP allocations which is already installed locally on your computer, significantly reducing the time needed to match a host to a location. There are two country level GeoIP databases available. AGeo::IPFree Perl module is available, but it does not appear that the database is being maintained. The better solution is MaxMind’s Geo::IP GeoLite Country open source version, said to be 97% accurate.
Download the current GeoLite Country, GeoLite City and Organization (AS Numbers)databases
wget http://geolite.maxmind.com/download/geoip/database/GeoLiteCountry/GeoIP.dat.gz
wget http://geolite.maxmind.com/download/geoip/database/GeoLiteCity.dat.gz
wget http://geolite.maxmind.com/download/geoip/database/asnum/GeoIPASNum.dat.gz
Move data files to directory and unzip.
mv GeoIP.dat.gz /usr/local/share/GeoIP/
gunzip /usr/local/share/GeoIP/GeoIP.dat.gz
mv GeoLiteCity.dat.gz /usr/local/share/GeoIP/
gunzip /usr/local/share/GeoIP/GeoLiteCity.dat.gz
mv GeoIPASNum.dat.gz /usr/local/share/GeoIP/
gunzip /usr/local/share/GeoIP/GeoIPASNum.dat.gz
Install the GeoIP program.
1. wget http://geolite.maxmind.com/download/geoip/api/c/GeoIP-1.4.5.tar.gz
tar -xvzf GeoIP-1.4.5.tar.gz
cd GeoIP-1.4.5
./configure
make
make check
sudo make install
2.wget http://geolite.maxmind.com/download/geoip/api/perl/Geo-IP-1.35.tar.gz
tar -xvzf Geo-IP-1.35.tar.gz
cd Geo-IP-1.35
perl Makefile.PL LIBS=’-L/usr/local/lib’
make
make test
sudo make install
Update your awstats.mysite.conf configuration file
Look for the following geoip LoadPlugin options in your AWStats configuration file. Update them with the full path to each of the two database files.
LoadPlugin=”geoip GEOIP_STANDARD /usr/local/share/GeoIP/GeoIP.dat”
LoadPlugin=”geoip_city_maxmind GEOIP_STANDARD /usr/local/share/GeoIP/GeoLiteCity.dat”
LoadPlugin=”geoip_org_maxmind GEOIP_STANDARD /usr/local/share/GeoIP/GeoIPASNum.dat”
Well, It’s done and now you can check the stats with country info.
Cheers!!
DH的虚拟主机上是有GeoIP.dat的,可以这样找到路径:
$ locate GeoIP.dat
/usr/local/share/GeoIP/GeoIP.dat
/usr/share/GeoIP/GeoIP.dat安装库模块:
perl -MCPAN -e ‘install “Geo::IP”‘
perl -MCPAN -e ‘install “Geo::IP::PurePerl”‘
我找到两个,比了一下时间,/usr/local那个要更新一点,所以用这个。所以,像下边这样修改common.conf:
找到
# LoadPlugin=”geoip GEOIP_STANDARD /pathto/GeoIP.dat”
取消注释,改为
LoadPlugin=”geoip GEOIP_STANDARD /usr/local/share/GeoIP/GeoIP.dat”
这样做,就可以开启GeoIP功能了,也就是在IP栏的右边,显示出IP所在国家。
如果,你认为DH的GeoIP.dat数据比较老,想使用比较新的,可以直接从MaxMind下载。可以下载GeoIP.dat.gz和GeoLiteCity.dat.gz,之后解压,放在某处。假设放在/home/yourname/awstats/目录下。
LoadPlugin=”geoip GEOIP_STANDARD /pathto/GeoIP.dat”
LoadPlugin=”geoip_city_maxmind GEOIP_STANDARD /pathto/GeoIPCity.dat”
把以上两句改为:
LoadPlugin=”geoip GEOIP_STANDARD /home/awstats/awstats/GeoIP.dat”
LoadPlugin=”geoip_city_maxmind GEOIP_STANDARD /home/yourname/awstats/GeoIPCity.dat”
这样,就可以有国家和城市两栏显示了。
但是,这个城市显示比较粗糙一点,还可以利用QQ的纯真库IP信息来显示,有人做了一个这样的插件,qqhostinfo。需要以下步骤:
1. 下载纯真版IP数据库,解压缩后,只使用QQWry.dat
2. 下载qqhostinof.pm
3. 下载分析QQWry.dat的脚本
4. 把以上文件都放在cgi-bin/plugins下
5. 修改qqwry.pl,把./QQWry.dat改为${DIR}/plugins/QQWry.Dat
6. 修改common.conf,加入LoadPlugin=”qqhostinfo”
经过以上设置就可以了,会在GeoIPCity右边出现Location栏,里边有很具体的地址信息。
在使用qqhostinfo的时候,需要安装Net::XWhois模块。发现DH的主机上已经装了这个模块。
如果在没有的虚拟主机上安装,由于没有管理员权限,所以,正常的安装过程在make install的时候会报错的。所以,可以这样:
$ make Makefile.PL PREFIX=/home/yourname/awstats/lib # 这样可以把最后的安装都放在这个目录里
$ make
$ make install
另外,如果对中文搜索引擎统计有更高需求的话,可以使用车东的TOP 8 Chinese local search engines。这东西是将近一年前的,适用于AWStats-6.6,对于6.7来说不太合适。我做了一个6.7的search_engines.pm.patch,可以对search_engines.pm打patch。不管在本地,还是在DH主机的shell里,可以
patch search_engines.pm search_engines.pm.patch
1.更新若干有用的模块:
ppm install Geo::IP::PurePerl
ppm install Geography::Countries
ppm install IP::Country
ppm install Geo-IPfree
ppm install Net-Xwhois
2.GeoIP和qqhostinfo需要下载相关文件
GeoIP需要GeoIP.dat;qqhostinfo需要qqhostinfo.pm、qqwry.pl、QQWry.dat3个文件,把他们都放到cgi-bin的插件目录里面.
GeoIP.dat,qqhostinfo.pm、qqwry.pl、QQWry.dat3 打包CSDN下载:
http://download.csdn.net/detail/debugcs/4360307
3、插件的开启
# 开启 GeoIP 反查模块
LoadPlugin=”geoip GEOIP_STANDARD C:/Perl/lib/GeoIP.dat” # 开启 IP 反查的外挂
LoadPlugin=”geoipfree”
# 开启 decodeutfkeys 外挂,解决“用以搜索的关键词”乱码
LoadPlugin=”decodeutfkeys”
# 设定IIS的log时间
LoadPlugin=”timezone +8″好了,所以的设置都弄好了,awstats已经能够很好的工作了。
# Plugin: qqhostinfo,显示访问者地区,下面的语句需要手工添加。
LoadPlugin=”qqhostinfo”
# 开启HostInfo,这个是qqhostinfo插件的前提
LoadPlugin=”hostinfo”
It may not be able to give the same level of detail as a commercial package such as Urchin, but it provides more than enough detail for most purposes.
On Ubuntu you can install awstats via apt:
apt-get install awstats
This will install the base package however, if you want to lever its full power you will need to install a couple of extra Perl modules.
apt-get install libnet-ip-perl
If you would like to see which countries your visitors are based in then you should look at one of the IP to Country plugins which will give you a Geo IP lookup.
You can choose between either geoipfree or GeoIp.
Other useful plugins are:
- Plugin: HostInfo – which gives you a popup whois window
- Plugin: IPv6 – adds IPv6 support
- Plugin: Tooltips – confused by all the information? This will take away the mystery
On rpm-based system there is a handy little perl script that walks you through the initial configuration. On Ubuntu you have to do it by hand, however most of the configuration is self-explanatory.
Simply copy the provided sample config to a new config file:
cp awstats.conf awstats.yoursite.tld.conf
NB: You will need a separate configuration file for each vhost you wish to examine.
Edit the conf file to match your site’s setup. Important things to pay attention to are any domain aliases, the location of the log files and any IPs you wish to ignore (eg. your own)
You can also decide which optional plugins you want to load. Simply add a line to invoke each one:
LoadPlugin="geoipfree"
LoadPlugin="ipv6"
LoadPlugin="Tooltips"
geoipfree requires an extra perl module. On ubuntu the package is called libgeo-ipfree-perl
You are almost ready!
You now need to configure Apache to allow access to your awstats pages.
Rather than messing about with my main Apache config file I found it neater to place the awstats specific information in an external file called awstats.conf:
Options None
AllowOverride None
Order allow,deny
Allow from all
Alias /awstatsicons/ /usr/share/awstats/icon/
ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
AllowOverride None
Options ExecCGI -MultiViews +SymLinksIfOwnerMatch
Order allow,deny
Allow from all
Then use an include directive in your main Apache config (apache2.conf):
Include /etc/apache2/awstats.conf
You then need to add it in to your vhost:
ScriptAlias /whereyouwanttoaccessit/ /usr/lib/cgi-bin/
If you navigate to:
http://www.yourdomain.tld/whereyouwanttoaccessit/awstats.pl?config=yourdomain.tld
You *should* be able to access your stats.
Of course you’ll need to actually generate the stats first:
/usr/lib/cgi-bin/awstats.pl -config=yourdomain.tld -update
I would recommend you run this from the command line once to create the first set of stats and then add a cronjob:
crontab -e
0,10,20,30,40,50 * * * * /usr/lib/cgi-bin/awstats.pl -config=yourdomain.tld -update >/dev/null
wq
This will update your stats every 10 minutes, which means that you’ll only have to update a few lines at a time. If your site is busy this makes sense
The last part of the line is important:
>/dev/null
By default cron emails the user each time it runs. You really don’t want 6 emails an hour telling you that Awstats has run
How to reset awstats
Just delete the files in /var/lib/awtstats
(directory defined by DirData parameter)
sudo su - root cd /var/lib/awstats rm * logout
Notes
-
you have to be root because for some reason,
sudo rm -R awstats/*
wouldn’t work. -
if you get the message
Never updated (See 'Build/Update' on awstats_setup.html page)
whereas the awstats files are there in/var/lib/awtstats
, it’s probably a permission problem on this folder.
VIM查看文件编码文件编码格式转换文件名编码转换
如果你需要在Linux中操作windows下的文件,那么你可能会经常遇到文件编码转换的问题。Windows中默认的文件格式是GBK(gb2312),而Linux一般都是UTF-8。下面介绍一下,在Linux中如何查看文件的编码及如何进行对文件进行编码转换。
查看文件编码
在Linux中查看文件编码可以通过以下几种方式:
1.在Vim中可以直接查看文件编码
:set fileencoding
即可显示文件编码格式。
如果你只是想查看其它编码格式的文件或者想解决用Vim查看文件乱码的问题,那么你可以在
~/.vimrc 文件中添加以下内容:
set encoding=utf-8 fileencodings=ucs-bom,utf-8,cp936
这样,就可以让vim自动识别文件编码(可以自动识别UTF-8或者GBK编码的文件),其实就是依照fileencodings提供的编码列表尝试,如果没有找到合适的编码,就用latin-1(ASCII)编码打开。
文件编码转换
1.在Vim中直接进行转换文件编码,比如将一个文件转换成utf-8格式
:set fileencoding=utf-8
2. iconv 转换,iconv的命令格式如下:
iconv -f encoding -t encoding inputfile
比如将一个UTF-8 编码的文件转换成GBK编码
iconv -f GBK -t UTF-8 file1 -o file2
文件名编码转换:
从Linux往 windows拷贝文件或者从windows往Linux拷贝文件,有时会出现中文文件名乱码的情况,出现这种问题的原因是因为,windows的文件名中文编码默认为GBK,而Linux中默认文件名编码为UTF8,由于编码不一致,所以导致了文件名乱码的问题,解决这个问题需要对文件名进行转码。
在Linux中专门提供了一种工具convmv进行文件名编码的转换,可以将文件名从GBK转换成UTF-8编码,或者从UTF-8转换到GBK。
首先看一下你的系统上是否安装了convmv,如果没安装的话用:
yum -y install convmv
安装。
下面看一下convmv的具体用法:
convmv -f 源编码 -t 新编码 [选项] 文件名
常用参数:
-r 递归处理子文件夹
–notest 真正进行操作,请注意在默认情况下是不对文件进行真实操作的,而只是试验。
–list 显示所有支持的编码
–unescap 可以做一下转义,比如把%20变成空格
比如我们有一个utf8编码的文件名,转换成GBK编码,命令如下:
convmv -f UTF-8 -t GBK –notest utf8编码的文件名
这样转换以后”utf8编码的文件名”会被转换成GBK编码(只是文件名编码的转换,文件内容不会发生变化)
vim 编码方式的设置
和所有的流行文本编辑器一样,Vim 可以很好的编辑各种字符编码的文件,这当然包括UCS-2、UTF-8 等流行的 Unicode 编码方式。然而不幸的是,和很多来自 Linux 世界的软件一样,这需要你自己动手设置。
Vim 有四个跟字符编码方式有关的选项,encoding、fileencoding、fileencodings、termencoding (这些选项可能的取值请参考 Vim 在线帮助 :help encoding-names),它们的意义如下:
* encoding: Vim 内部使用的字符编码方式,包括 Vim 的 buffer (缓冲区)、菜单文本、消息文本等。默认是根据你的locale选择.用户手册上建议只在 .vimrc 中改变它的值,事实上似乎也只有在.vimrc 中改变它的值才有意义。你可以用另外一种编码来编辑和保存文件,如你的vim的encoding为utf-8,所编辑的文件采用cp936编码,vim会自动将读入的文件转成utf-8(vim的能读懂的方式),而当你写入文件时,又会自动转回成cp936(文件的保存编码).
* fileencoding: Vim 中当前编辑的文件的字符编码方式,Vim 保存文件时也会将文件保存为这种字符编码方式 (不管是否新文件都如此)。
* fileencodings: Vim自动探测fileencoding的顺序列表, 启动时会按照它所列出的字符编码方式逐一探测即将打开的文件的字符编码方式,并且将 fileencoding 设置为最终探测到的字符编码方式。因此最好将Unicode 编码方式放到这个列表的最前面,将拉丁语系编码方式 latin1 放到最后面。
* termencoding: Vim 所工作的终端 (或者 Windows 的 Console 窗口) 的字符编码方式。如果vim所在的term与vim编码相同,则无需设置。如其不然,你可以用vim的termencoding选项将自动转换成term的编码.这个选项在 Windows 下对我们常用的 GUI 模式的 gVim 无效,而对 Console 模式的Vim 而言就是 Windows 控制台的代码页,并且通常我们不需要改变它。
好了,解释完了这一堆容易让新手犯糊涂的参数,我们来看看 Vim 的多字符编码方式支持是如何工作的。
1. Vim 启动,根据 .vimrc 中设置的 encoding 的值来设置 buffer、菜单文本、消息文的字符编码方式。
2. 读取需要编辑的文件,根据 fileencodings 中列出的字符编码方式逐一探测该文件编码方式。并设置 fileencoding 为探测到的,看起来是正确的 (注1) 字符编码方式。
3. 对比 fileencoding 和 encoding 的值,若不同则调用 iconv 将文件内容转换为encoding 所描述的字符编码方式,并且把转换后的内容放到为此文件开辟的 buffer 里,此时我们就可以开始编辑这个文件了。注意,完成这一步动作需要调用外部的 iconv.dll(注2),你需要保证这个文件存在于 $VIMRUNTIME 或者其他列在 PATH 环境变量中的目录里。
4. 编辑完成后保存文件时,再次对比 fileencoding 和 encoding 的值。若不同,再次调用 iconv 将即将保存的 buffer 中的文本转换为 fileencoding 所描述的字符编码方式,并保存到指定的文件中。同样,这需要调用 iconv.dll由于 Unicode 能够包含几乎所有的语言的字符,而且 Unicode 的 UTF-8 编码方式又是非常具有性价比的编码方式 (空间消耗比 UCS-2 小),因此建议 encoding 的值设置为utf-8。这么做的另一个理由是 encoding 设置为 utf-8 时,Vim 自动探测文件的编码方式会更准确 (或许这个理由才是主要的 ;)。我们在中文 Windows 里编辑的文件,为了兼顾与其他软件的兼容性,文件编码还是设置为 GB2312/GBK 比较合适,因此 fileencoding 建议设置为 chinese (chinese 是个别名,在 Unix 里表示 gb2312,在 Windows 里表示cp936,也就是 GBK 的代码页)。
Mac/Linux下的批量文件编码转换
用了很多次的东西,放到自己的blog记录一下。
1. 新建一个文件夹,这里取根目录下的文件夹 encoding ,然后将要转换的文本拉入这个文件夹。
2. 打开终端,输入以下代码:
cd /encoding find *.txt -exec sh -c "iconv -f GB18030 -t UTF8 {} > {}.txt" \;
上面的命令是针对GB18030,如果你转换前的编码为GB2312,将 GB18030 代替为 GB2312 即可。同理,若文件名后缀不是.txt,则做相应的修改即可。
10大最适合编程的字体推荐下载,让代码看起来更美更舒服!
现在有事没事就喜欢写写代码的人越来越多了,对于成天盯着屏幕工作的开发人员来说,编程代码可能是每天见得最多的东西了。可是绝大部分人都一直使用编辑器默认的字体,其实,换一套适合自己的编程字体不仅能让代码看得更舒服,甚至还能提高工作效率的!
如果你有想过换一种编程字体,却不知道哪里找合适的,那么看看异次元软件世界为您推荐的10款最适合编程的字体吧,这些字体能让你的代码瞬间“优雅”起来!换一种字体,换一番心情嘛。当然,除了编程之外,经常需要编辑英文文档的朋友同样适用……
前言:
下面字体的排序是作者的主观感受,每个人的喜好都不一样。建议您都试试,按照自己的喜好来选择。另外,还在使用 Windows XP 的同学,如果您希望显示到如截图中清晰的字体效果,则必须安装微软的ClearType设置程序对清晰度进行设置。
10. Courier
也叫Courier New,这大概是我们最熟悉的字体了,基本上所有系统都有。很不幸,很多终端和编辑器都默认使用此种字体,虽然不会影响使用,但它太无趣了。如果你正在使用这种字体,建议调大一点,并打开系统的 anti-aliasing (抗锯齿) 设置。
9. Andale Mono
比Courier稍好,也是常见的默认字体。我感觉字母太宽了,字符间距也比较蠢。
8. Monaco
Mac的默认字体,好像也只有Mac上有。小字号的时候表现不错,而且再大些也不寒碜。
7. Profont
与Monaco类似的位图字体,你能够在Mac, Windows和Linux上面使用。小字号的时候表现好。非Mac平台上Monaco的最佳替代。喜欢小字号且不怕眼睛疲劳的同学可以考虑。
6. Monofur
独特的等宽字体,各种字号下都表现不错,但是需要设置anti-aliasing。怀旧而且喜欢与众不同的人推荐。
5. Proggy
干净的等宽字体,好像很受Windows用户欢迎,但在Mac上也不错。使用时,字号可以小一点,无需anti-aliasing。
4. Droid Sans Mono
开源字体,可以在这里下载,适合手机屏幕。是等宽字体中最突出的一个。可惜0和O区别不大。
3. Deja Vu Sans Mono
我最喜欢的免费字体系列,以Vera为基础,但是比后者提供更多字符了。适于任何字号,需要anti-aliasing。
2. Consolas + 中文雅黑混合版
Consolas 是商业字体,专门为微软设计,微软不少产品上都有,所以很可能你的系统上已经有了。需要anti-aliasing。如果不是商业的,我可能就把它放第一了。Consolas仅含英文字母部分,这里提供的是雅黑中文+Consolas英文的混合体,即使代码里有中文注释显示效果也能很好的!
1. Inconsolata
我最喜欢的等宽字体,免费。我遇到它之后,很快就把原来的默认字体Deja Vu Sans Mono抛弃了。真正适合任何字号的好字体。感谢它的创造者Raph Levien!
1分钟内保护你的Linux服务器—Server Shield v1.0.2
Server Shield是一个轻量级的linux服务器安全加固软件。它安装简单、稳定,能够直接有效的使你的服务器抵御黑客攻击。
特性
Slowloris Protection
Firewall Hardening
TCP Hardening
ICMP/Ping Flood Protection
DoS Protection
Spoof Protection
FTP/SSH Bruteforce Protection
Automatic Security Updates
Disables Bash History
DNS Amplification Protection
依赖
yum-security
iptables
net-tools
sed
gawk
git
gcc
安装
git clone https://github.com/Brian-Holt/server-shield cd server-shield;chmod +x sshield;mv sshield /etc/init.d /etc/init.d/sshield start
How to Install Java on Linux
Java allows you run cross-platform applications that can run on Mac OS-X, Linux, and Windows (among other OSs) without modification. Here’s how to install it on a GNU/Linux machine.
Steps
Manual Non-RPM Method
This is the generic method that also works with GNU/Linux clones that do not support RPM. It does not require administrator rights and allows installing multiple Java versions on the same computer.
-
1.Download the JDK.[1].
- Click on the “Download” link under Java Platform (JDK) 7u4.
- Accept the license and continue.
-
2.Select the right download. Under the “Product/File Description” you should pick your corresponding Linux option. For example, if you’re running Linux x86 (32-bit), you need to choose the corresponding “*.tar.gz” version.
-
3.Download the file as a .gz. Save it to your GNU/Linux machine.
-
4.Switch to the directory where you saved the file. You do not need to be a root and only must have the write access to the folder where you wish to install Java. If your administrator is not supportive, you may need to place Java into your home folder or (even better) on some shared network location
-
5.Uncompress the file. For example, you could do this in your home folder. Uncompressing will create a folder called “jdk1.7.0_04”. Java is now installed.
- The installed Java jre is rather independent and can be easily moved into another place just by copying all its files.
- You can install multiple different jre’s this way: they coexist together and can be used if some software requires the older version to run.
-
6.Launch Java. The Java executable you need to launch is located in a subfolder, called “bin.” This way of installation will not configure a default Java command for you — you must do this manually or always include the full path in your startup script.
Manual RPM Method
This seems like a “more civilized” way to install Java because it allows the installer to check the dependencies on some system libraries that may be missing. However, it does not support versioning easily and may fail even in some systems that do support RPMs. (Though the current Java installations are rather self-dependent and the required minimal requirements are usually satisfied anyway.)
-
1.Download the JDK.[2].
- Click on the “Download” link under Java Platform (JDK) 7u4.
- Accept the license and continue.
-
2.Select the right download. Under the “Product/File Description” you should pick your corresponding Linux option. For example, if you’re running Linux x86 (32-bit), you need to choose the corresponding “*.rpm” version, for example “jdk-7u4-linux-i586.rpm”.
-
3.Download the .rpm file. Save it to your GNU/Linux machine.
-
4.Log in as root and switch to the directory where you saved the file. Or, become root by running su and entering the super-user password.
-
5.Install the rpm file by executing ‘rpm -ivh filename.rpm’, where filename is the name of your .rpm file. (Such as jdk-7u4-linux-i586.rpm).
- You may receive a message telling that program “rpm” is not installed.
- If that is the case the program “rpm” is not installed. You need to install it writing: sudo apt-get install rpm. Enter your password, and you’re finished.
-
6.Create symbolic links. If you want to be able to execute this version of Java interpretor or compiler from any directory on your GNU/Linux system, you will have to create a few symbolic links:
- sudo ln -s -v jdk1.7.0_04/bin/java /usr/bin/java
- sudo ln -s -v jdk1.7.0_04/bin/javac /usr/bin/javac
- Note that by installing using this method there could be dependencies that fail. It is better to use the package installer that handles all dependencies for you, since you will not be able to use Java correctly until the dependencies are resolved.
Ubuntu Method Using a GUI Package Manager
-
1.Open a package manager. You can use Synaptic or Adept Manager.
-
2.Install Open JDK. Java JDK and JRE are not available for installation through GUI Package Manager. You’ll need to install Open JDK instead.
- Perform a search for openjdk-7-jdk.
- Select openjdk-7-jdk for installation. Depending on the package manager, you may be asked if you wish to install the required dependencies or it will automatically select them without confirmation. For example, the openjdk-7-jre was already selected by Synaptic Package Manager. If you want to use Java as a plug-in in your browser, then also select to install icedtea-7-plugin.
-
3.Apply the changes. Click the button to apply the changes. Depending on the package manager, a pop-up window may appear asking for you to confirm the changes.
-
4.Wait for Java to install.
Ubuntu Method Using a Console
-
1.Enter one of the following commands into your console program:
- sudo apt-get install openjdk-7-jdk openjdk-7-jre (if you don’t want the browser plug-in)
- sudo apt-get install openjdk-7-jdk openjdk-7-jre icedtea-7-plugin (if you do need the plug-in)
- apt-get automatically takes care of the dependencies and lists the changes for confirmation.
-
2.Enter y to confirm the installation.
Ubuntu OpenJDK Method Using a Console
-
1.Ubuntu no longer supports the Sun Java package in favor of OpenJDK.
-
2.Enter one of the following into your console program:
- If you do not want the browser plugin, sudo apt-get install openjdk-6-jre.
- If you do want the browser plugin, sudo apt-get install openjdk-6-jre icedtea6-plugin
- If you’re working on a server without graphics, and want an environment just for running server applications (e.e. Tomcat or Glassfish), sudo apt-get install –no-install-recommends openjdk-6-jre-headless
- If you need the full JDK (for writing Java programs): sudo apt-get install openjdk-6-jdk
- apt-get automatically takes care of the dependencies and lists the changes for confirmation.
-
3.Enter y to confirm the installation.
Tips
- It’s much easier to install from the repositories than from the download on Oracle’s site.
- Even though it might be unfamiliar to you, using the console method is actually easier than the GUI method.
- If you find how-to specific for your distribution we recommend using it because in some distributions, Java can be included in the software repositories (which you can benefit from).
- On Red Hat 8.0 Linux, one could start a Nautilus file manager, desending to the directory, and click on the name of the rpm filename and activate the installation process. This does not work any more in Fedora Core 4 (other FC versions not tested). In reality, if you do this you will get a (warning) message saying that the file name indicates the file is not executable while the content of the file is of the type “executable”.
- Another tip to follow progress of the rpm install is to use ‘rpm -ivv … ‘ which turns on theverbose output and gives you more info on the install as it progresses. You can do the same thing with other rpm actions to get rpm to be more verbose about what is going on.
学习Web应用漏洞最好的教程—-WebGoat
WebGoat是一个用于讲解典型web漏洞的基于J2EE架构的web应用,他由著名的WEB应用安全研究组织OWASP精心设计并不断更新,目前的版本已经到了5.0。WebGoat本身是一系列教程,其中设计了大量的web缺陷,一步步的指导用户如何去利用这些漏洞进行攻击,同时也指出了如何在程序设计和编码时避免这些漏洞。Web应用程序的设计者和测试者都可以在WebGoat中找到自己感兴趣的部分。
虽然WebGoat中对于如何利用漏洞给出了大量的解释,但是还是比较有限,尤其是对于初学者来说,但觉得这正是其特色之处:WebGoat的每个教程都明确告诉你存在什么漏洞,但是如何去攻破要你自己去查阅资料,了解该漏洞的原理、特征和攻击方法,甚至要自己去找攻击辅助工具,当你成功时,WebGoat会给出一个红色的Congratulation,让你很有成就感!(咱号称搞技术的不就需要这个吗?)
WebGoat甚至支持在其中加入自己的教程,具体方法可以查看其说明文档;
WebGoat中包括的漏洞教程主要有
Cross-site Scripting (XSS)
Access Control
Thread Safety
Hidden Form Field Manipulation
Parameter Manipulation
Weak Session Cookies
Blind SQL Injection
Numeric SQL Injection
String SQL Injection
Web Services
Fail Open Authentication
Dangers of HTML Comments
建议在使用WebGoat时对照OWASP的文档来看,比较有用的是下面三个:
OWASP Testing Guide 3.0
OWASP Code Review Guide 1.1
OWASP Development Guide 2.0
如果你以前没有接触过OWASP,当你看到这些文档时,应该会有一种感觉叫做兴奋。
下面简单说一下安装方法(windows下):
先下载webscarab-current.zip(这个自带tomcat,还有一个下载方式是war文件,需要自己安装tomcat,建议使用第一个),地址为http://www.owasp.org/index.php/Category:OWASP_WebGoat_Project,解压到一个文件夹,运行webgoat.bat即可启动其自带的tomcat,通过访问http://localhost/WebGoat/attack,输入用户名guest,密码guest即可进入。如下图所示:
有一点值得注意的是默认Tomcat只开放在127.0.0.1的80端口,其他的机器没有办法访问,这也是为了安全考虑,因为WebGoat中的漏洞实在太多了。如果是拿来学习,建议还是将其开在0.0.0.0上,修改tomcat下conf中server-80.xml,将其中的所有127.0.0.1改为0.0.0.0并重新运行webgoat.bat即可。server-8080.xml也可以改,但是要运行webgoat_8080.bat才有效果,
知道有WebGoat这个好东东得益于一次和Fortify厂商的接触,Fortify也是OWASP的主要资助者,他们的程序跟踪分析器(PTA)和静态代码分析器(SCA)常常拿WebGoat来做演示。但是很可惜,没有保存Fortify的分析结果,Fortify也不提供个demo版,只好先拿IBM(收购自WatchFire)的AppScan来看看WebGoat中是否有那么多漏洞,下面是扫描结果:
的确漏洞无数!好了,开始学习吧!