顯示具有 hostname 標籤的文章。 顯示所有文章
顯示具有 hostname 標籤的文章。 顯示所有文章

2014年11月12日 星期三

[Linux] 使用 awk, sort, uniq, grep, dig 批次驗證 email address @ Ubuntu 14.04

工作時中,偶爾會對 UNIX 系統感動! 雖然有時很簡單的任務會龜毛到硬要連續指令來處理,明明寫一小段 code 就好了啊 Orz

取處已知清單中的 email domain:

$ mysql -e "select email from db.user;" > email.log;
$ sort email.log | uniq | awk -F'@' '{print $2}' | sort | uniq | grep "." > email.domain.log


最後的 grep "." 則是要避開 hostname 不合法的項目(如 localhost 等)。

驗證 mx:

$ dig -t mx test.com +short | wc -l
$ test `dig -t mx test.com +short | wc -l` -ne 0 && echo "ok"


批次驗證:

$ awk '{ system("test `dig -t mx "$1" +short | wc -l` -ne 0 && echo "$1)}' email.domain.log > email.validated.log

2014年6月5日 星期四

iOS 開發筆記 - DNS Lookup、Host name to IP Address

最近想把玩一下 socket ,過程中需要把 domain name 轉成 ip ,筆記一下:

#include <netdb.h>
#include <arpa/inet.h>

- (NSString*)hostnameToIPAddress:(NSString*)hostname
{
    struct hostent *remoteHostEnt = gethostbyname([hostname UTF8String]);
    struct in_addr *remoteInAddr = (struct in_addr *) remoteHostEnt->h_addr_list[0];
    return [NSString stringWithUTF8String:inet_ntoa(*remoteInAddr)];
}

NSLog(@"IP: %@", [self hostnameToIPAddress:@"tw.yahoo.com"]);
NSLog(@"IP: %@", [self hostnameToIPAddress:@"tw.yahoo.com"]);
NSLog(@"IP: %@", [self hostnameToIPAddress:@"tw.yahoo.com"]);


若看到查到結果不一樣,主因是 DNS 提供的分流服務。

2014年5月19日 星期一

[Linux] 透過 AWK 和 HOSTNAME 資訊執行 Sleep 指令 @ Ubuntu 14.04

最近開始埋一些 crontab 任務到各台機器,但是,有些指令是對 DB 動作的,而通常擺在 crontab 的 DB 指令是為了建出一些 snapshot 且多為 slow query 的,那如果多台機器全部都同時發送 DB query 時,反而又累到了 DB 系統。

這時候想到的方式就是搭配 sleep 的方式,並透過每檯機器的資訊避開一同發送要求,用法就想到 hostname 了:

$ awk '{s=0;for(i=1;i<=length($0);i++){c=sprintf("%c",substr($0,i,1));s+=c ;}; system( "sleep " s); }' /etc/hostname 

最後,在 crontab 上,就可以在待執行指令前面,先安插上述的 script 來跑,如:
  # m h dom mon dow command
*/10 * * * * sh /path/sleep.sh ; /path/job_exec