Category: Linux

  • ssh登录自动发邮件提醒

    大前提:

    因涉及到发邮件,debian下需安装exim4并可对外发邮件,centos则需安装mailx,sendmail。

    方案1,对所有用户生效,创建文件

    vim /etc/ssh/sshrc

    加入以下内容

    #!/bin/bash
    [email protected]
    mail -s "notification:user $USER login from ${SSH_CLIENT%% *}" $yourmail <<EOT
     the ip information : http://www.123cha.com/ip/?q=${SSH_CLIENT%% *}
     login date: `date +%Y/%m/%d" "%H:%M:%S`
     if this is not you , please contact xxx for help.
    EOT

    方案2,不影响他人,只针对自己的账户

    vim ~/.bashrc

    添加

    echo 'ALERT Shell Access on:' `date` `who` | mail -s "Alert: Root Access from `who | cut -d"(" -f2 | cut -d")" -f1`" [email protected]
  • linux beep蜂鸣声

    公司搬家得到一台老IBM笔记本,各种折腾,今天折腾一下蜂鸣声。

    有时候按tab补全,vim输入错误等都会听到这样的报警声,很难听,如果想关掉它,

    临时关闭

    debian:

     $sudo rmmod pcspkr

    centos以root执行

     #rmmod pcspkr

    永久关闭

    debian:

     $sudo echo "blacklist pcspkr" >> /etc/modprobe.d/blacklist

    centos以root执行:

     #echo "alias pcspkr off" >> /etc/modprobe.conf

    不关闭就可以用beep来做一些有意思的警告了,甚至把警告变为music

    apt-get install beep

    加载驱动:

     modprobe pcspkr

    通过beep来操作(会听到嘀一声):

     beep -f 1000

    以下“音乐”摘自
    http://kirrus.co.uk/2010/09/linux-beep-music/

    beep -f 1000 -n -f 1500 -n -f 600 -n -f 500 -n -f 100 -r 2 -l 10 -n -f 50 -r 2 -l 200 -n -f 40 -r 2 -l 300 -n -f 60 -r 3 -n -f 50 -r 3

    星球大战的经典铃声:

    beep -l 350 -f 392 -D 100 -n -l 350 -f 392 -D 100 -n -l 350 -f 392 -D 100 -n -l 250 -f 311.1 -D 100 -n -l 25 -f 466.2 -D 100 -n -l 350 -f 392 -D 100 -n -l 250 -f 311.1 -D 100 -n -l 25 -f 466.2 -D 100 -n -l 700 -f 392 -D 100 -n -l 350 -f 587.32 -D 100 -n -l 350 -f 587.32 -D 100 -n -l 350 -f 587.32 -D 100 -n -l 250 -f 622.26 -D 100 -n -l 25 -f 466.2 -D 100 -n -l 350 -f 369.99 -D 100 -n -l 250 -f 311.1 -D 100 -n -l 25 -f 466.2 -D 100 -n -l 700 -f 392 -D 100 -n -l 350 -f 784 -D 100 -n -l 250 -f 392 -D 100 -n -l 25 -f 392 -D 100 -n -l 350 -f 784 -D 100 -n -l 250 -f 739.98 -D 100 -n -l 25 -f 698.46 -D 100 -n -l 25 -f 659.26 -D 100 -n -l 25 -f 622.26 -D 100 -n -l 50 -f 659.26 -D 400 -n -l 25 -f 415.3 -D 200 -n -l 350 -f 554.36 -D 100 -n -l 250 -f 523.25 -D 100 -n -l 25 -f 493.88 -D 100 -n -l 25 -f 466.16 -D 100 -n -l 25 -f 440 -D 100 -n -l 50 -f 466.16 -D 400 -n -l 25 -f 311.13 -D 200 -n -l 350 -f 369.99 -D 100 -n -l 250 -f 311.13 -D 100 -n -l 25 -f 392 -D 100 -n -l 350 -f 466.16 -D 100 -n -l 250 -f 392 -D 100 -n -l 25 -f 466.16 -D 100 -n -l 700 -f 587.32 -D 100 -n -l 350 -f 784 -D 100 -n -l 250 -f 392 -D 100 -n -l 25 -f 392 -D 100 -n -l 350 -f 784 -D 100 -n -l 250 -f 739.98 -D 100 -n -l 25 -f 698.46 -D 100 -n -l 25 -f 659.26 -D 100 -n -l 25 -f 622.26 -D 100 -n -l 50 -f 659.26 -D 400 -n -l 25 -f 415.3 -D 200 -n -l 350 -f 554.36 -D 100 -n -l 250 -f 523.25 -D 100 -n -l 25 -f 493.88 -D 100 -n -l 25 -f 466.16 -D 100 -n -l 25 -f 440 -D 100 -n -l 50 -f 466.16 -D 400 -n -l 25 -f 311.13 -D 200 -n -l 350 -f 392 -D 100 -n -l 250 -f 311.13 -D 100 -n -l 25 -f 466.16 -D 100 -n -l 300 -f 392.00 -D 150 -n -l 250 -f 311.13 -D 100 -n -l 25 -f 466.16 -D 100 -n -l 700 -f 392

    –add by jpuyy@2013年03月20日

    解决linux播放音乐不出声的问题

    想用这台老本放音乐,但是每次都不出声,安装好了alsa,使用lsmod查看能看到声卡。困扰很久,后来查到一篇文章,http://xghe110.blog.51cto.com/337884/65713 查到原来alsamixer默认是不出声的。

    解决方法:

    1. root用户输入 #alsamixer,在界面中,左右方向键选择声道,上下方向选择大小。(关键)按m打开对应的声道。

    2. 输入# alsactl store 保存当前的配置。

    3. 在/etc/rc.local中加入 alsactl restore,下次重启后自动载入当前的alsa配置。

    这时,通过mpg321,mplayer,mocp来播放音乐。

  • linux挂载ntfs提示read-only

    公司搬家,挖出一台很老的IBM A23,256MB内存,10GB硬盘。费了不小力气装了一个debian,这么老的机器当然要用光盘装了,期间网卡不能识别,安装了firmware-linux-nonfree_0.28+squeeze1_all.deb。

    装好之后,越发感觉linux的强大。

    接上网线,打算用rtorrent下PT,并挂载一个320GB的移动硬盘,挂载有问题,只能用read-only挂载。

    网上搜到linux挂载须使用ntfs-3g的软件,安装ntfs-3g

    apt-get install ntfs-3g

    之后使用如下命令挂载

    mkdir /mnt/usb
    mount -t ntfs-3g /dev/sdb1  /mnt/usb -o defaults,umask=0
  • 自动更新花生壳的ip

    参考:

    http://open.oray.com/wiki/doku.php?id=文档:花生壳:http协议说明

    只能使用GET方式提交信息,这里使用curl,需提前安装好curl。
    加一个判断,如果当前dns中的ip和检测的ip不一致时,就自动提交;一致则退出。

    #!/bin/bash
    orayuser=your oray name
    oraypass=your oray passoword
    oraydomain=your domain
    realip=`curl http://ddns.oray.com/checkip | cut -d' ' -f 6 | cut -d'<' -f 1`
    theurl="http://$orayuser:[email protected]/ph/update?hostname=$oraydomain&myip=$realip"
    currentip=`nslookup $oraydomain | tail -2 | head -1 | cut -d' ' -f 2`
    if [ "$realip" = "$currentip" ]; then
     exit 0
    else
     curl "$theurl"
    fi

    在crontab中加入以下内容,每十五分钟检测一次

    */15 * * * * /bin/bash /root/pushiporay.sh

    –添加于2013年3月3日开始–

    后来申请了noip.com的二级域名,才发现花生壳和人家的api是如此的像。同理可更新noip里的记录

    #!/bin/bash
    noipuser=your noip username
    noippass=your noip password
    noipdomain=your noip domain
    realip=`curl http://ddns.oray.com/checkip | cut -d' ' -f 6 | cut -d'<' -f 1`
    noipurl="http://$noipuser:[email protected]/nic/update?hostname=$noipdomain&myip=$realip"
    noipcurrentip=`nslookup $noipdomain | tail -2 | head -1 | cut -d' ' -f 2`
    if [ "$realip" = "$noipcurrentip" ]; then
     echo "no need to update"
     exit 0
    else
     curl "$noipurl"
    fi

     

  • discuzx2.5放在目录中的nginx rewrite规则

    如果论坛放在/club目录时,nginx中对应server{}的discuz rewrite可使用如下规则实现伪静态。

     

    location /club/ {
        index index.php;
        try_files $uri $uri/ @discuz;
    }
    
    location @discuz {
        rewrite ^([^\.]*)/topic-(.+)\.html$ $1/portal.php?mod=topic&topic=$2 last;
        rewrite ^([^\.]*)/article-([0-9]+)-([0-9]+)\.html$ $1/portal.php?mod=view&aid=$2&page=$3 last;
        rewrite ^([^\.]*)/forum-(\w+)-([0-9]+)\.html$ $1/forum.php?mod=forumdisplay&fid=$2&page=$3 last;
        rewrite ^([^\.]*)/thread-([0-9]+)-([0-9]+)-([0-9]+)\.html$ $1/forum.php?mod=viewthread&tid=$2&extra=page%3D$4&page=$3 last;
        rewrite ^([^\.]*)/group-([0-9]+)-([0-9]+)\.html$ $1/forum.php?mod=group&fid=$2&page=$3 last;
        rewrite ^([^\.]*)/space-(username|uid)-(.+)\.html$ $1/home.php?mod=space&$2=$3 last;
        rewrite ^([^\.]*)/blog-([0-9]+)-([0-9]+)\.html$ $1/home.php?mod=space&uid=$2&do=blog&id=$3 last;
        rewrite ^([^\.]*)/(fid|tid)-([0-9]+)\.html$ $1/index.php?action=$2&value=$3 last;
        rewrite ^([^\.]*)/([a-z]+[a-z0-9_]*)-([a-z0-9_\-]+)\.html$ $1/plugin.php?id=$2:$3 last;
        if (!-e $request_filename) {
        return 404;
        }
    }
  • bash创建mysql数据库脚本

    需要批量创建数据库及数据库对应用户,写了一个对于mysql的执行脚本。

    #!/bin/bash
     #用法:将数据库名/用户名用:分隔开,每个一行,写入一个文件,执行脚本+文件名,可创建数据库,并为其分配同名用户
     IFS=:
     while read dbname dbpassword
     do
     sql_user='root'
     sql_pass='root'
     mysql -u $sql_user -p$sql_pass -s <<EOT
     create database if not exists $dbname default charset utf8 collate utf8_general_ci;
     QUIT
     EOT
     mysql -u $sql_user -p$sql_pass -s << EOF
     insert into mysql.user (Host,User,Password) values ("localhost","$dbname",PASSWORD("$dbpassword"));
     flush privileges;
     grant all on $dbname.* to $dbname@localhost;
     flush privileges;
     QUIT
     EOF
    done < $1