moneyslow.com

Linux shell mysql命令精华收藏

sed合并两行,将

1

2

3

4

合并成:

1 2

3 4

sed '$!N;s/\n/\t/' 文件名

 

按模式合并:

sed '/2/{N;s/\n/\t/}' 文件名

 

 

vi 里替换空格行 %s/^\n//g

awk and sed

去掉第一列重复的行 awk '! a[$1]++' t.lst

去掉重复行 awk 'a!=$1{a=$1;print}' yourfile

sed -n '/<title>/,/<\/title>/p' aaa.shtml |grep -v title

去掉,开头的行 sed -e '/^,/d' t1.lst > t2.lst

 

shell 比大小和自增的例子

#!/bin/bash

c=1;

 

for line in `cat downlog.lst.gb2312`

do

cc=`echo $line | grep kejian.com |wc -l`

 

if [ $cc -ge 1 ];then

echo $line >> $c.txt

fi

 

if [ "$line" == "saved" ];then

echo $c

c=`expr $c + 1`

fi

done

exit;

 

shell 读取文件

#!/bin/bash

#find test |

find kejian -type f -name \*\ \* |

while read file

do

mv "$file" `dirname "$file"`/`basename ${file// }` 2>/dev/null

#echo $file

Done

 

epel 安装:

centos 6.3:

rpm -Uvh http://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm

AS4-u8 x86_64:

rpm -Uvh http://dl.fedoraproject.org/pub/epel/4AS/x86_64/epel-release-4-10.noarch.rpm

glances 安装:

yum -y install glances

----------------------------------------------------

argument line too long:

/usr/bin/find /data/tmp/ -type f -print0 |xargs -0 -l1 -t rm -f

 

ubuntu 一键安装lamp

tasksel install lamp-server

 

ubuntu apt-get 安装:

apt-get instahe2 php5 mysql-server php5-mysql php5-gd curl libcurl3 libcurl3-dev php5-curl

 

--------------------------------------------------

 

Device is busy :

umount -l /data/hadoop

--------------------------------

闰秒:

service ntpd restart

hwclock --hctosys

 

single 模式只读

mount -o remount, rw /

 

虚机lck解决办法:

~ # lsof|grep lck

~ # kill 7026756

 

arping -U -s 10.32.25.28 -I bond0 10.32.25.248

 

 

去除空格

for loop in `ls -1 | tr ' ' '#'`

do

mv "`echo $loop | sed 's/#/ /g' `" "`echo $loop |sed 's/#//g' `" 2> /dev/null

done

 

挪文件

find 啥* -type f -name |xargs -I '{}' mv {} /data/tmp/1

 

交换机查看mac地址命令

1. sh mac add

2. sh arp

3. sh ip int bri(仅显示三层端口)

 

H3C的

1. disp mac

2. disp arp

3. disp bri int bri(仅显示三层端口)

 

 

yum php的memcached,redis 扩展

pecl install redis

 

solr 扩展安装:

先安装php5.3以上

yum -y install php55

pecl install solr

 

openssl 扩展安装:

wget www.a8z8.com/php-5.3.8.tar.gz

cd php-5.3.8/ext/openssl/

mv config0.m4 config.m4

/usr/local/php/bin/phpize

./configure --with-openssl --with-php-config=/usr/local/php/bin/php-config

make && make install

 

-----------------------------------------------------

测试磁盘性能

[root@if40 ~]# hdparm -tT /dev/sdc1

 

/dev/sdc1:

Timing cached reads: 14856 MB in 2.00 seconds = 7437.87 MB/sec

Timing buffered disk reads: 544 MB in 3.01 seconds = 181.03 MB/sec

----------------------安装iftop----------------------------

yum -y install flex byacc libpcap ncurses ncurses-devel libpcap-devel

wget http://www.ex-parrot.com/~pdw/iftop/download/iftop-0.17.tar.gz

configure & make & make install

lsof 解决磁盘空间不释放的问题

lsof |grep delete 找到delete的进程,kill掉

 

判断一个进程是否有写操作:

strace -f -p 28123 -e trace=write (注意,strace只盯着write系统调用,其他还有writev,mmap也可以写磁盘)

 

lsof 常见的用法是查找应用程序打开的文件的名称和数目

端口号运行的程序:

lsof -i :3306

 

 

恢复删除的文件:假如有人误删了/var/log/messages 文件,通过lsof|grep messages 查看文件描述符:

# lsof |grep message

abrt-dump 1946 root 4r REG 253,0 280161 278827 /var/log/messages

rsyslogd 19330 root 4w REG 253,0 280161 278827 /var/log/messages

可见第4列的文件描述符是4,而且处于w是写入的状态。通过这个描述符就可以看实际的文件了:

# more /proc/19330/fd/4 就可以看是不是删除的文件。

恢复文件:cat /proc/19330/fd/4 > /var/log/messages

 

iotop 查看使用最大的io

----------------------------------

tcpdump -nnn -i eth0 src port 12000

tcpdump -nnn -i eth0 dst port 12000

tcpdump -XXX -vvv -nnn -i eth0 dst port 12000

tcpdump -vvvv -XXX host 220.1.1.46 and port 80 -i eth1

tcpdump -nnXSs 0 'port 80'

 

抓header数据并已行格式输出:

tcpdump -s 1024 -l -i eth0 host 10.21.2.186 -A

 

抓header里的数据:

tcpdump -X -s 0 host 10.10.22.5 and tcp port 80|grep Forwarded

.抓包用来防止80端口被人攻击时可以分析数据

# tcpdump -c 10000 -i eth0 -n dst port 80 > /root/pkts

用tcpdump嗅探80端口的访问看看谁最高

# tcpdump -i eth0 -tnn dst port 80 -c 1000 | awk -F"." '{print $1"."$2"."$3"."$4}' | sort | uniq -c | sort -nr |head -20

查看数据库执行的sql

# /usr/sbin/tcpdump -i eth0 -s 0 -l -w - dst port 3306 | strings | egrep -i 'SELECT|UPDATE|DELETE|INSERT|SET|COMMIT|ROLLBACK|CREATE|DROP|ALTER|CALL'

查看是哪些蜘蛛在抓取内容。

# /usr/sbin/tcpdump -i eth0 -l -s 0 -w - dst port 80 | strings | grep -i user-agent | grep -i -E 'bot|crawler|slurp|spider'

 

----------------------下载apnic的ip地址段-------------------------

wget http://ftp.apnic.net/apnic/stats/apnic/delegated-apnic-latest

计算mask:

grep 'apnic|CN|ipv4' delegated-apnic-latest | cut -f 4,5 -d '|' | tr '|' ' ' | while read ip netmask

do

i=0;

while [ $netmask -gt 1 ]

do

let "netmask=$netmask/2";

let "i = $i + 1";

done

echo $ip/$i;

done

如何是韩国的,就CN改为KR

-----------------------------------------------------------------

调试程序时有用的命令:

gdb Linux下的调试器

pstack 查看进程的调用栈,如果是多线程的话,可以查看每个线程的调用栈

pmap 查看内存的映射情况

strace 跟踪进程的系统调用

ltrace 跟踪程序的系统调用

ldd 查看依赖库

lsof 查看进程打开的所有文件

------------------------mkfs.ext4 -----------------------

yum -y install e4fsprogs

 

df显示到一行:df –hP

查看可疑ip 看每个ip的连接数

netstat -ntu | awk '{print $5}' | cut -d: -f1 | sort | uniq -c | sort -n

netstat -alnt | awk '{print $5}' |awk -F ":" '{print $1}' | sort | uniq -c |sort -n

netstat -n | awk '/^tcp/ {++S[$NF]} END {for(a in S) print a, S[a]}'

更快的方法:

ss -ant | awk 'NR>1 {++s[$1]} END {for(k in s) print k,s[k]}'

查看php-cgi 数量:

netstat -anpo|grep php-cgi

查看占用内存的进程:

ps -A --sort -rss -o comm,pmem,pcpu |uniq -c |head -15

查看一个进程占用的内存:

pmap $(pgrep php-cgi|head -1)

#########################################################

查看系统最大文件描述符:

# cat /proc/sys/fs/file-max

查看系统当前使用的描述符:

# cat /proc/sys/fs/file-nr

[root@# /usr/local/php/bin/pear install Pager

downloading Pager-2.4.8.tgz ...

Starting to download Pager-2.4.8.tgz (36,122 bytes)

..........done: 36,122 bytes

install ok: channel://pear.php.net/Pager-2.4.8

----------------------------------------------------------------

rsyncd.conf:

 

readonly = no

hosts allow = 1.1.1.1

uid = www

gid = www

 

[xxx]

path = /xxx

windows 下路由

route add 10.11.2.0 mask 255.255.255.0 10.11.6.254 metrc 2

ip route add 10.10.0.0/16 via 10.11.2.254

 

添加永久路由加个-p参数:

route -p add 172.16.0.0 mask 255.240.0.0 10.10.26.254

-------------------------------------------------------

查看网卡是否插网线

mii-tool

网卡工作模式:

设为10M:

mii-tool -F 10baseT-FD eth1

恢复自适应模式:

mii-tool -r eth1

查看:

mii-tool -v

 

以上是老工具,新工具的办法:

设置为网卡为10M的办法:

ethtool -s eth0 speed 10 duplex full autoneg off

执行后断网,一会自己好。

 

恢复1000M:

ethtool -s eth0 speed 1000 duplex full autoneg off

------------------------svn--------------------

svnadmin create /data/svndata/xxx

cd /data/svndata/xxx/conf

cp /data/svndata/svnserve.conf /data/svndata/xxx/conf/

kill -9 svnpid

svnserve -d -r /data/svndata

 

-----------------------------------------gate way ------------------

for i in /proc/sys/net/ipv4/conf/*/rp_filter ; do echo 2 > $i; done

---------------------------------------------------

批量加用户:

#!/bin/bash

for name in aaa bbb nnnn

do

useradd -g dev $name

echo cpasswdom | passwd --stdin $name

done

nginx编译参数:

#/usr/local/nginx/sbin/nginx -V

apache编译参数:

# cat /usr/local/apache2/build/config.nice

php编译参数:

# /usr/local/php/bin/php -i |grep configure

mysql编译参数:

# cat "/usr/local/mysql/bin/mysqlbug"|grep configure

ulimit -SHn 51200

kill -9 `ps -ef | grep mysql | grep -v grep | awk '{print $2}'`

nmap -sP -n 123.1.1.0/24 | cut -d" " -f5 | awk '/123/' | sort -t . -k 4,4n

 

---------------网卡限速-----------------------------------

ethtool -s eth0 speed 1000 duplex full autoneg off

ethtool -s eth0 speed 10 duplex full autoneg off

------------------------------------------------------------------------------------------

查看可疑的访问url

tail -10000 access.log |awk '{print $7}'|sort |uniq -c|sort -rn|head -20

查看可以的访问ip:

tail -10000 access.log | awk '{print $1,$7}'|sort |uniq -c|sort -rn|head -20

查看压缩日志攻击前20名ip

zcat access_12.log.gz |awk '{print $1,$7}'|sort |uniq -c|sort -rn|head -20

Exit mobile version