Showing posts with label Linux. Show all posts
Showing posts with label Linux. Show all posts

还记得多年以前怎么分享A图的么?

藏在图片里的rar压缩包,嘿嘿...图片下载下来,改名成.rar再解压缩。

Windows里是这么做得:
copy picture.png + archive.rar share_rar_in_pic.png

Linux里也可以哦:
cat picture.png archive.rar > share_rar_in_pic.png

原来bt官方客户端是py的...

一直都不知道...无脑bt:
alias dltorrent='screen /path/to/btdownloadheadless.py --display_interval 5 --spew 1'
此外还有ncurse的btdownloadcurses.py 和wx的btdownloadgui.py... 不过还是没有rtorrent强大,没有aria2c无脑...

搞来个的解压缩函数,哈哈,够懒的

dotfiles.org 搞来的解压缩,哈哈
###   Handy Extract Program

extract () {
    if [ -f $1 ] ; then
        case $1 in
            *.tar.bz2)   tar xvjf $1   ;;
            *.tar.gz)    tar xvzf $1   ;;
            *.bz2)       bunzip2 $1    ;;
            *.rar)       unrar x $1    ;;
            *.gz)        gunzip $1     ;;
            *.tar)       tar xvf $1    ;;
            *.tbz2)      tar xvjf $1   ;;
            *.tgz)       tar xvzf $1   ;;
            *.zip)       unzip $1      ;;
            *.Z)         uncompress $1 ;;
            *.7z)        7z x $1       ;;
            *)           echo "'$1' cannot be extracted via >extract<" ;;
        esac
    else
        echo "'$1' is not a valid file"
    fi
}

List dir tree in bash

This is a simple "tree" command:
find . -type d | sed -e "s/[^-][^\/]*\//  |/g" -e "s/|\([^ ]\)/|-\1/"
Alias it and have fun!!

recode tips

So why bother iconv, dos2unix, unix2dos when we have recode...
#显示所有有效的字符集及其别名
recode -l | less

#转换Windows下的ansi文件到当前的字符集(自动进行回车换行符的转换)
recode windows-1252.. file_to_change.txt

#转换Windows下的ansi文件到当前的字符集
recode utf-8/CRLF.. file_to_change.txt

#转换Latin9(西欧)字符集文件到utf8
recode iso-8859-15..utf8 file_to_change.txt

#Base64编码
recode ../b64 < file.txt > file.b64

#Quoted-printable格式解码
recode /qp.. < file.txt > file.qp

#将文本文件转换成HTML
recode ..HTML < file.txt > file.html

#在字符表中查找某符号(e.g.欧元)
recode -lf windows-1252 | grep euro

#显示字符在latin-9中的字符映射
echo -n 0x80 | recode latin-9/x1..dump

#显示latin-9编码
echo -n 0x20AC | recode ucs-2/x2..latin-9/x

#显示utf-8编码
echo -n 0x20AC | recode ucs-2/x2..utf-8/x

Toying with cal and date

#显示日历
$cal -3

#显示指定月,年的日历
$cal 9 1752

#这个星期五是几号?
$date -d fri

#今年圣诞礼拜几?
$date --date='25 Dec' +%A

#若干秒后的未来时刻时间计算
$date --date '2009-09-27 13:00:00 UTC 360000 seconds'

#显示当前某地区时间时间(可用tzselect寻找时区)
$TZ=':America/Los_Angeles' date

#定时弹对话框
$echo "DISPLAY=$DISPLAY xmessage ALARM!!" | at "NOW + 30 minutes"

Some wget tips

#网页及附件下载
wget -nd -pHEKk [url]

#断点续传
wget -c [url]

#批量下载某类别文件
wget -r -nd -np -l1 -A '*.jpg' [url]

#定时下载
echo 'wget url' | at 01:00

#限制下载速度(32KB/s)
wget --limit-rate=32k [url]

#检查链接有效性
wget -nv --spider --force-html -i bookmarks.html

Some CLI tips note

# 去掉配置文件里的注释
egrep -v '^[[:space:]]*(#|$)'
.or.
sed '/ *#/d; /^ *$/d'

#做http url链接
echo $1 | sed -e h -e 's@/@\\@g' -e G -e 's@\([^\n]*\)\n\(.*\)@\1@'

#Python 自带当前目录的Http Server
python -m SimpleHTTPServer

#寻找所有不可读的文件
find -type f ! -perm -444

#寻找不可访问的目录
find -type d ! -perm -111

#Tar拷贝目录下的所有文件到目录/where/to/并保持文件属性
( cd /copy/from && tar -c . ) | ( cd /copy/to/ && tar -x -p )

#Tar拷贝目录到远程目录并保持文件属性
( tar -c /copy/from ) | ssh -C user@remote 'cd /copy/to/ && tar -x -p'

从urandom取随机数

/*
  Read a random number from /dev/urandom
*/
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>

#include <string.h>  /* for strerror(int errno) */
#include <errno.h>
extern int errno;

#define BUFS 4

/* Assumes ssize_t is 4 bytes on the system */

union U {
  ssize_t mlong;
  char c[4];
};

int main (void)
{
  int fp,n;
  char buf[BUFS+1];
  union U u;
  
  /*  /dev/random only returns bytes when enough noise has 
      been generated from the entropy pool. Hense you may 
      be blocked waiting. In contrast, /dev/urandom will always 
      return bytes. */
  if ((fp = open ("/dev/urandom", O_RDONLY)) == -1)
    {
      fprintf (stderr, "Can't open data: %s\n", strerror (errno));
      return 1;
    }

 if ( ( n=read(fp, buf, BUFS )) >=  4)
   {
     buf[n]='\0';
     u.c[0]=buf[0];
     u.c[1]=buf[1];
     u.c[2]=buf[2];
     u.c[3]=buf[3];

     // check sizes
     fprintf(stderr,"%u %d  sizeof(%d)  sizeof(%d)\n",u.mlong,n,sizeof(u.mlong),sizeof(u)); 
     printf("%u\n",u.mlong);
   }
 close(fp);

  return 0;
}

bti: 好用的linux CLI twitter/identica 客户端

找到个好用的CLI T/I 客户端:bti,直接输出结果,十分合适bash里pipe使用。
配合Privoxy 或者 GAPPProxy使用:

$bti --proxy 127.0.0.1:8118 --host twitter --account methuselar --password XXX --action friends
src在这里:

也适合武装上bot... 虽说python模块很成熟了, 有个C的现成玩意儿也不错...

Finds all devices with quotas

/* 
Finds all devices with quotas.
*/

#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <mntent.h>
#include <string.h>

FILE *setdev()
{
 FILE *fp;
 if ((fp = setmntent(MOUNTED, "r")) == NULL) {
  fprintf(stderr, "Error calling setmntent\n");
 }
 return fp;
}

int getdev(FILE * fp,char *device)
{
 struct mntent *mnt;
        char *p;
        char *h;

 while ((mnt = getmntent(fp)) != NULL) {
  if (hasmntopt(mnt, "usrquota")) {
    if ((h = hasmntopt(mnt,"loop=/dev/loop")) != NULL) {
                    snprintf(device,255,"%s",h+5);
      p=device;
                    while(*p++) {
        if ( *p == ',') {
   *p='\0';
   break;
        }}
    return strlen(device);
   } else {
     snprintf(device,255,"%s",mnt->mnt_fsname);
    return strlen(device);
   }
  }

 }
    
 return 0;
}

int main(void)
{
 FILE *fp;
 char device[256];

 if ((fp = setdev()) == NULL) {
  fprintf(stderr, "setdev returned NULL\n");
  exit(1);
 }

 while ( getdev(fp,device)) {
  printf("%s\n", device);

 }

 endmntent(fp);
 exit(0);
}

getpwnam 结构

#include <stdio.h>
#include  <stdlib.h>
#include  <sys/types.h>
#include  <pwd.h>

int main(int argc, char **argv)
{
  struct passwd *pw;

  if(argc <= 1) {
      printf("./getpwuid  \n");
      return 0;
  }

  pw = getpwnam(argv[1]);
  if( pw != NULL ) {
    printf("User name %s\n",pw->pw_name);
    printf("user password %s\n",pw->pw_passwd);
    printf("user id %d\n",pw->pw_uid);
    printf("group id %d\n",pw->pw_gid);
    printf("real name %s\n",pw->pw_gecos);
    printf("home directory %s\n",pw->pw_dir);
    printf("shell program %s\n",pw->pw_shell);
 
  } else {
    printf("User not found!\n");
  }
  return 0;

}

Linux signal 目录小监控

/*
   notify if anything is modified in the current directory
*/

#define _GNU_SOURCE
#include 
#include 
#include 
#include 

static volatile int event_fd;
static volatile int event_sig;
static volatile void *event_data;

static void handler(int sig, siginfo_t *si, void *data)
{
  event_fd = si->si_fd;
  event_sig = sig;
  event_data = data;
}

int main(void)
{
  struct sigaction act;
  int fd;

  act.sa_sigaction = handler;
  sigemptyset(&act.sa_mask);
  act.sa_flags = SA_SIGINFO;
  sigaction(SIGRTMIN + 1, &act, NULL);

  fd = open(".", O_RDONLY);
  fcntl(fd, F_SETSIG, SIGRTMIN + 1);
  fcntl(fd, F_NOTIFY, DN_ACCESS|DN_MODIFY|DN_CREATE|DN_RENAME|DN_DELETE|DN_ATTRIB|DN_MULTISHOT);
  
  while (1) {
    pause();
    printf("Sth happened on fd=%d\n", event_fd);
  }
}

Sabayon Linux CoreCD!

 http://www.sabayonlinux.org/pages/show/id/88
    Nice for those who don't wanna read Gentoo Handbook or who don't like compiling from the right start.
    I personally use Sabyon as a Gentoo Installer. Gets rid of entropy ASAP but... 
    Sabayon IS a great work. The installer, the config, the scripts, a nice kernel sourceset.
    Gentooers should have Sabyaon overlay!

超小巧的socks代理

今天装了个包包:
net-proxy/nylon
用起来比Dante简单多了
默认配置改好IP规则就上了,
让我想起了windows的Antinat...

Great Microsoft Outlook .MSG files converter

Just get these goodies in CPAN (Comprehensive Perl Archive Network):
perl -MCPAN -e 'install("Email::Outlook::Message")'
perl -MCPAN -e 'install("Email::LocalDelivery")'
perl -MCPAN -e 'install("Getopt::Long")'
perl -MCPAN -e 'install("Pod::Usage")'
perl -MCPAN -e 'install("File::Basename")'
And then, go to http://www.matijs.net/software/msgconv/ and download the Perl script msgconvert.pl (see the link "script itself" near the bottom of the above-mentioned Web page).
Use it like this:
perl -w ~/msgconvert.pl ~/your_message.msg
And your_messagemsg..mime is generated. Now open it with your favorate mail client and we are set!

Setting up PPTP VPN connection

make pptpvpn file:
#!/bin/bash
function routeadd {
route add -host 61.xxx.xxx.xxx dev ppp0
route add -host 62.xxx.xxx.xxx dev ppp0
route add -host 63.xxx.xxx.xxx dev ppp0
}
function makepptp {
echo pty \"pptp VPN.SERVER.COM --nolaunchpppd\" >> /etc/ppp/peers/pptpvpn;
echo remotename PPTP >> /etc/ppp/peers/pptpvpn;
echo require-mppe-128 >> /etc/ppp/peers/pptpvpn;
echo file /etc/ppp/options.pptp >> /etc/ppp/peers/pptpvpn;
echo ipparam pptpvpn >> /etc/ppp/peers/pptpvpn;
pppd call pptpvpn &
}
if [ -a /etc/ppp/chap-secrets ];
then
rm /etc/ppp/chap-secrets
echo $1 PPTP $2 '*' >> /etc/ppp/chap-secrets;
else
echo $1 PPTP $2 '*' >> /etc/ppp/chap-secrets;
fi

if [ -e /etc/ppp/peers/pptpvpn ];
then
rm /etc/ppp/peers/pptpvpn;
echo name $1 >> /etc/ppp/peers/pptpvpn;
makepptp;
sleep 8;
routeadd;
else
echo name $1 >> /etc/ppp/peers/pptpvpn;
makepptp;
sleep 8;
routeadd;
fi

Where,
pty \"pptp VPN.SERVER.COM --nolaunchpppd\"
Specifies that the command script is to be used to communicate rather than a specific terminal device. In this case we are using pptp client to establishes the client side of a Virtual Private Network (VPN) using the Point-to-Point Tunneling Protocol (PPTP).
VPN.SERVER.COM is host name (or IP address) for the VPN server
This will tell which hosts are on the other side of tunnel. (host name or IP address)
route add -host 63.xxx.xxx.xxx dev ppp0
Close and save the file.
chmod +x pptpvpn
sudo ./pptpvpn username password
If everything is went correctly you should be online and ppp0 should be up. Remote server will assign IP address and other routing information
Disconnect PPTP server vpn connection
Simply kill pppd service, enter:
killall pppd
it will work on any distribution, its just a matter ofpptp package you will have to install..

--Taken from: Sabayon wiki

Gentoo:回归到portage稳定分支

个人觉得~挺好,没这个必要,但是实在想的话:
改make.conf(amd64,x86之类):
#sed "s/\(ACCEPT_KEYWORDS=\"\)~/\1/g" /etc/make.conf > /etc/make.conf

备份package.keywords:
#cp /etc/portage/package.keywords /etc/portage/package.keywords.back

合法化现存unstable分支的包包:
#equery -i -N list | grep \~ | sed 's/.* \(.*\) (.*/<=\1 ~amd64/' >> /etc/portage/package.keywords

这样一来,系统将会在一次次的emerge -uDN world 中慢慢趋于稳定分支...

用Sabayon live 恢复gcc

有猛人这么干过:
emerge -C gcc
借助Sabayon live CD/DVD,可以利用quickpkg方便的恢复gcc。
假设/dev/[gentoo_root]是你的根目录盘:
mount /dev/[gentoo_root] /mnt/gentoo
mount --bind /mnt/gentoo/usr/portage/packages/ /usr/portage/packages/
quickpkg gcc
chroot /mnt/gentoo
emerge -K gcc
当然,如果分开挂了portage,那么portage和/也要分别挂上...

Create a service in /etc/init.d

For example, a rp-pppoe service
#!/sbin/runscript

depend() {
need net
}

start() {
ebegin "Starting ADSL"
/usr/sbin/pppoe-start
eend $? "Connection Failed"
}

stop() {
ebegin "Stopping ADSL"
/usr/sbin/pppoe-stop
eend $? "Failed to stop ADSL"
}
Place this in /etc/init.d and use it as other Services!Many apps can be used like this!
| More

Twitter Updates