`
liyuan462
  • 浏览: 49655 次
  • 性别: Icon_minigender_1
  • 来自: 武汉
社区版块
存档分类
最新评论
收藏列表
标题 标签 来源
简单的单线程http下载程序 c/c++
#include "apue.h"
#include <netdb.h>
#include <arpa/inet.h>
#include <errno.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <time.h>

/**
 *Author: Li Yuan <liyuan462@gmail.com>
 *Create Date: 2011-10-06
 *Version: 0.1
 */

unsigned long downloaded = 0;
unsigned long total = 0;

double difftimeval(const struct timeval *tv1,
		   const struct timeval *tv2)
{
  double d;
  time_t s;
  suseconds_t u;

  s = tv1->tv_sec - tv2->tv_sec;
  u = tv1->tv_usec - tv2->tv_usec;
  d = s;
  d *= 1000000.0;
  d += u;
  return d;
}

void *speed_thr(void *arg)
{
  struct timeval tv_last, tv_this;
  time_t s;
  suseconds_t u;
  int err;
  unsigned long down_last, down_this;
  double timediff;
  if ((err = gettimeofday(&tv_last, NULL)) < 0) {
    fprintf(stderr, "get time error: %s\n", strerror(errno));
    exit(1);
  }
  down_last = downloaded;
  printf("\033[s");
  for (;;) {
    if ((err = gettimeofday(&tv_this, NULL)) < 0) {
      fprintf(stderr, "get time error: %s\n", strerror(errno));
      exit(1);
    }
    timediff = difftimeval(&tv_this, &tv_last);
    if (downloaded == total) {
      printf("\033[u");
      printf("\033[K");
      printf("100%%\t%lu\t%.0fK/s\n", total,
	     (timediff/1000000.0) * (total-down_last) / 1024);
      break;
    }
    if (timediff >= 1000000.0) {
      down_this = downloaded;
      printf("\033[u");
      printf("\033[K");
      printf("%.1f%%\t%lu\t%luK/s", 100*(float)down_this/total,
	     down_this, (down_this-down_last)/1024);
      tv_last = tv_this;
      down_last = down_this;
    }
  }
  return((void *)0);
}

int resolve(char *src, char *web, char *file, char *local, int *port)
{
  char *pa, *pb, *tp;
  char *http = "http://";
  if (!src || !(*src)) {
    fprintf(stderr, "url resolve error: src is null\n");
    return -1;
  }
  pa = src;
  if (!strncmp(pa, http, strlen(http)))
    pa += strlen(http);
  else {
    fprintf(stderr, "url resolve error: src is not a valid http url\n");
    return -1;
  }
  pb = strchr(pa, '/');
  if (pb) {
    memcpy(web, pa, strlen(pa)-strlen(pb));
    if (*(pb+1)) {
      memcpy(file, pb+1, strlen(pb)-1);
      for (tp = file+strlen(file)-1; tp !=file; tp--) {
	if (*tp == '/')
	  break;
      }
      if (tp == file)
	strcpy(local, file);
      else
	strcpy(local, tp+1);
    } else {
      fprintf(stderr, "url resolve error: no file exists\n");
      return -1;
    }
  } else {
    fprintf(stderr, "url resolve error: no file exists\n");
    return -1;
  }
  pa = strchr(web, ':');
  if (pa) {
    *port = atoi(pa+1);
    *pa = '\0';
  }
  return 0;
}

int main(int argc, char *argv[])
{
  struct addrinfo *ailist, *aip;
  struct addrinfo hint;
  struct sockaddr_in *sinp;
  const char *addr;
  char server_name[256];
  char server_file[1024];
  char local_file[256];
  int port, n, i, j, toread;
  int fd, sockfd, err, speed_tid;
  char abuf[INET_ADDRSTRLEN];
  char request[1024];
  char buffer[1024];
  char line[1024];
  char *pc;

  if (argc != 2) {
    fprintf(stderr, "usage: %s url\n", argv[0]);
    exit(1);
  }

  memset(server_name, 0, sizeof(server_name));
  memset(server_file, 0, sizeof(server_file));
  memset(local_file, 0, sizeof(local_file));
  port = 80;

  setvbuf(stdout, NULL, _IONBF, 0);
  if (resolve(argv[1], server_name, server_file, local_file, &port) < 0)
    exit(1);

  hint.ai_flags = AI_CANONNAME;
  hint.ai_family = AF_INET;
  hint.ai_socktype = SOCK_STREAM;
  hint.ai_protocol = 0;
  hint.ai_addrlen = 0;
  hint.ai_canonname = NULL;
  hint.ai_addr = NULL;
  hint.ai_next = NULL;

  printf("resolving the host %s... ", server_name);
  if ((err = getaddrinfo(server_name, "www", &hint, &ailist)))
    err_quit("getaddrinfo error: %s", gai_strerror(err));
  aip = ailist;
  sinp = (struct sockaddr_in *)aip->ai_addr;
  addr = inet_ntop(AF_INET, &sinp->sin_addr, abuf, INET_ADDRSTRLEN);
  if (!addr) {
    fprintf(stderr, "fail to resolve\n");
    exit(1);
  }
  printf("%s\n", addr);

  printf("connecting %s|%s|:%d... ", server_name, addr, port);
  if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
    fprintf(stderr, "can't connect to %s: %s\n",
	    server_name, strerror(errno));
    exit(1);
  }

  if (connect(sockfd, aip->ai_addr, aip->ai_addrlen) < 0) {
    fprintf(stderr, "can't connect to %s: %s\n",
	    server_name, strerror(errno));
    exit(1);
  }
  printf("connected.\n");

  sprintf(request, "GET /%s HTTP/1.0\r\nHost:%s\r\n\r\n",
	  server_file, server_name);

  if (write(sockfd, request, strlen(request)) != strlen(request)) {
    fprintf(stderr, "send error: %s\n", strerror(errno));
    exit(1);
  }

  printf("http request sent, waiting for response... ");
  toread = 1;
  i = j = 0;
  while ((n = read(sockfd, buffer, 1)) > 0) {
    if (buffer[0] != '\r' && buffer[0] != '\n') {
      i = 0;
      line[j++] = buffer[0];
    }
    else {
      i++;
      if (i == 2 && buffer[0] == '\n') {
	line[j] = '\0';
	if (!strncmp(line, "HTTP", strlen("HTTP"))) {
	  pc = strchr(line, ' ');
	  printf("%s\n", pc+1);
	  if (strncmp(pc+1, "200", 3)) {
	    exit(1);
	  }
	} else if (!strncmp(line, "Content-Length",
			    strlen("Content-Length"))) {
	  printf("%s\n", line);
	  pc = strchr(line, ' ');
	  total = atoi(pc+1);
	} else if (!strncmp(line, "Content-Type",
			    strlen("Content-Type"))) {
	  printf("%s\n", line);
	}
	j = 0;
      }
    }
    if (i == 4)
      break;
  }

  printf("downloading %s\n", local_file);
  if ((fd = creat(local_file, FILE_MODE)) < 0)
    err_sys("creat error");

  downloaded = 0;
  err = pthread_create(&speed_tid, NULL, speed_thr, NULL);
  if (err != 0)
    err_quit("can't create speed thread: %s\n", strerror(err));
  while ((n = read(sockfd, buffer, 1024)) > 0) {
      if (write(fd, buffer, n) != n)
	err_sys("write error");
      downloaded += n;
  }
  if (n < 0)
    err_sys("read error");
  err = pthread_join(speed_tid, NULL);
  if (err != 0)
    err_quit("can't join with speed thread: %s\n", strerror(err));
  printf("complete.\n");

  exit(0);
}
python实现的简单的踩人人网主页脚本 python
#!/usr/bin/env python

import urllib2
import urllib
import cookielib
from HTMLParser import HTMLParser
import sys
import re
import json
import random
import getopt
import getpass

def usage():
    print """Function: Visit others' home pages on www.renren.com
   Usage: visitRenRen [-h] [-e email] [-p password]"""

class HomeParser(HTMLParser):
    
    def __init__(self):
        self.logoutLink = None
        HTMLParser.__init__(self)

    def handle_starttag(self, tag, attrs):
        if tag != 'a': return
        for key, value in attrs:
            if key == 'href':
                href = value
                break
        if 'Logout.do' in href:
            self.logoutLink = href

    def getLogoutLink(self):
        return self.logoutLink

class FriendListParser():
    
    def __init__(self):
        self.content = None
        self.friendList = []

    def feed(self, content):
        self.content = content

    def fillFriendList(self):
        p = re.compile('var friends=(.*);')
        for line in self.content.split('\n'):
            m = p.search(line) 
            if m:
                break
        else:
            return
        friendListJson = m.group(1)
        friendListRaw = json.loads(friendListJson)
        for friend in friendListRaw:
            friendId = friend['id']
            friendName = friend['name']
            self.friendList.append((friendId, friendName))

    def getFriendList(self):
        return self.friendList 

class VisitParser(HTMLParser):
    
    def __init__(self, id):
        self.statusLink = None
        self.name = None
        self.id = id
        self.inName = False
        self.validate = False
        HTMLParser.__init__(self)

    def handle_starttag(self, tag, attrs):
        status = False
        if tag == 'a':
            for key, value in attrs:
                if key == 'stats' and value == 'pf_tab_status':
                    status = True
                if key == 'href':
                    href = value
            if str(self.id) in href and status:
                self.statusLink = href
        if tag == 'label':
            for key, value in attrs:
                if key == 'for' and value == 'personalInfo5qValidateCode':
                    self.validate = True
        if tag == 'h1':
            for key, value in attrs:
                if key == 'class' and "username" in value:
                    self.inName = True

    def handle_data(self, data):
        if self.inName:
            self.name = data
            self.inName = False

    def getStatusLink(self):
        return self.statusLink

    def getValidate(self):
        return self.validate

    def getName(self):
        return self.name

try:
    opts, args = getopt.gnu_getopt(sys.argv[1:], "he:p:")
except getopt.GetoptError, err:
    print str(err)
    usage()
    sys.exit()

email = None 
password = None

for o, a in opts:
    if o == "-h":
        usage()
        sys.exit()
    elif o == "-e":
        email = a
    elif o == "-p":
        password = a

if email is None:
    email = raw_input('Email: ')
if password is None:
    password = getpass.getpass()
    
cj = cookielib.LWPCookieJar()
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
#urllib2.install_opener(opener)

print "logging in..."
loginData = {
'email':email,
'password':password
}
loginData = urllib.urlencode(loginData)

f = opener.open('http://www.renren.com/PLogin.do', loginData)
parser = HomeParser()
parser.feed(f.read())
logged = parser.getLogoutLink()
if logged is None:
    print "login fail"
    sys.exit()
print "login succeed"

#for ind, cookie in enumerate(cj):
#    print "%d-%s" % (ind, cookie)
#cj.save("cookies.dat")

#f = opener.open('http://friend.renren.com/myfriendlistx.do')
#parser = FriendListParser()
#parser.feed(f.read())
#parser.fillFriendList()
#friendList = parser.getFriendList()
#print "total amount of friends:", len(friendList)
#
#toVisitList = random.sample(friendList, len(friendList))
#lenSucceed = 0
#lenFail = 0
#failedOnce = False
#for id, name in toVisitList:
#    f = opener.open('http://www.renren.com/profile.do?\
#portal=homeFootprint&ref=home_footprint&id=' + str(id))
#    encoding = f.info().getparam('charset')
#    page = f.read().decode(encoding)
#    parser = VisitParser(id)
#    parser.feed(page)
#    visited = parser.getStatusLink()
#    if visited is None:
#        print "visit " + str(id) + " " + name + " fail"
#        lenFail += 1
#        if failedOnce:
#            break
#        failedOnce = True
#    else:
#        print "visit " + str(id) + " " + name + " succeed"
#        lenSucceed += 1
#        failedOnce = False
#print "visit:", str(lenFail+lenSucceed)
#print "succeed:", str(lenSucceed)
#print "fail:", str(lenFail)

baseId = 222000000
visitCount = 0
while True:
    offset = random.randint(0, 999999) 
    realId = baseId + offset 
    f = opener.open('http://www.renren.com/profile.do?\
portal=homeFootprint&ref=home_footprint&id=' + str(realId))
    encoding = f.info().getparam('charset')
    page = f.read().decode(encoding)
    parser = VisitParser(realId)
    parser.feed(page)
    visited = parser.getStatusLink()
    validate = parser.getValidate()
    name = parser.getName()
    if validate:
        print "visit enough pages. wait and try again."
        break
    if visited is None:
        continue
    print "visit", str(realId), name, "succeed"
    visitCount += 1
print "you have successfully visited", str(visitCount), "persons' home pages"
powershell版双IP配置切换 powershell
$ip_in = "1.2.3.4"
$gateway_in = "1.1.1.1"

$ip_out = "1.2.3.5"
$gateway_out = "1.1.1.2"

$netmask = "255.255.255.0"
$dns1 = "202.114.0.242"
$dns2 = "202.112.20.131"


do {
	write-host "【1】切换到内网"
	write-host "【2】切换到外网"
	$choice = read-host "请选择"
} while (($choice -ne "1") -and ($choice -ne "2"));

if ($choice -eq "1") {
	$type = "内网"
} else {
	$type = "外网"
}

write-host "正在切换到$type,请稍等..."

$adapter = Get-WmiObject Win32_NetworkAdapterConfiguration -filter "IPEnabled = true"
if ($choice -eq "1") {
	if ((Get-Process | Where-Object {$_.ProcessName -eq "MentoHUST"}) -ne $NULL) {
		Stop-Process -name MentoHUST
	}
	$temp = $adapter.EnableStatic($ip_in, $netmask)
	$temp = $adapter.SetGateways($gateway_in)
} else {
	$temp = $adapter.EnableStatic($ip_out, $netmask)
	$temp = $adapter.SetGateways($gateway_out)
}
$temp=$adapter.SetDNSServerSearchOrder(@($dns1, $dns2))

if ($choice -eq "2") {
	if ((Get-Process | Where-Object {$_.ProcessName -eq "MentoHUST"}) -eq $NULL) {
		Start-Process D:\MentoHUST\MentoHUST.exe
	}
}

write-host "你已切换到$type。按任意键退出。"
$temp = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
统计某目录下的代码行数 bash
#!/bin/bash

E_NOARGS=75

if [ -z "$1" ]
then
    echo "Usage: `basename $0` [directory] [postfix]"
    exit $E_NOARGS
fi

postfix=${2:-""}
sum=0

for file in `find "$1" -type f`
do
    if [ -n "$postfix" ] && [ ${file##*.} != "$postfix" ];then
        continue
    fi
    line=`wc -l $file | cut -d' ' -f1`
    sum=$(($sum + $line))
done

echo $sum
ssh信任建立expect脚本 python
#!/usr/bin/expect --

#author: iceblue <liyuan462@gmail.com>
#date: 2011-5-12

if {[llength $argv]!=2} {
    puts "Usage: sshtrust \[name@host\] \[password\]"
    exit
}

set nhost "[lindex $argv 0]"
set pass "[lindex $argv 1]"

set timeout 60
puts "checking whether trust is configured..."
log_user 0
#whether the file ~/.ssh/id_rsa.pub exists, 0 for exists
set norsa 0 

spawn ssh $nhost 
expect {
    "password:" {puts "trust is NOT configured"} 
    "ssh:" {puts "host NOT reachable"
            exit}
    "(yes/no)\\?" {send "yes\r"
                   expect "password:"
                   puts "trust is NOT configured"}
    -re "\\$|#" {puts "trust is ALREADY configured"
            exit}
}
close

puts "starting configuring trust..."
set rsapath "~/.ssh/id_rsa.pub"
if [catch {set file [open $rsapath]} error] {
    set    norsa 1
}

if {$norsa==1} {
    spawn ssh-keygen
    expect ":"    {send "\r"}
    expect ":"    {send "\r"}
    expect ":"    {send "\r"}
    expect eof
    set file [open $rsapath]
}

gets $file rsapub
spawn ssh $nhost
expect "password:" {send "$pass\r"}
expect {
    "Permission denied" {puts "password NOT correct"
                        exit}
    -re "\\$|#" {send "echo $rsapub >> ~/.ssh/authorized_keys\r"}
}
expect -re "\\$|#"
close
puts "configuring trust completed"

puts "testing..."
spawn ssh $nhost
expect {
    "password:" {puts "configuring trust fail"} 
    "ssh:" {puts "host NOT reachable"}
    -re "\\$|#" {puts "configuring trust succeed"}
}
ssh信任建立或解除python脚本 python
#!/usr/bin/env python

#author: iceblue <liyuan462@gmail.com>
#date: 2011-05-14

import pexpect
import os
import sys
import getopt

def usage():
    print """Function: Create or destroy the ssh trust relationship to user@host 
   Usage: sshtrust [-h|--help] [-u] [-p password] user@host
Examples: sshtrust user@host -p password   #create 
          sshtrust user@host -u            #destroy"""

def check():
    ret = None
    print "checking whether trust is configured..."
    child = pexpect.spawn('ssh ' + nhost)
    i = child.expect(['password:', '[\$#] ', 'ssh:', '\(yes/no\)\?'])
    if (i == 0):
    	print "trust is NOT configured"
        ret = 0
    elif (i == 1):
    	print "trust is ALREADY configured"
        ret = 1
    elif (i == 2):
    	print "host NOT reachable"
        ret = 2
    elif (i == 3):
    	child.sendline('yes')
    	ii = child.expect(['password:', '[\$#]'])
        if (ii == 0):
    	    print "trust is NOT configured"
            ret = 0
        elif (ii == 1):
            print "trust is ALREADY configured"
            ret = 1
    child.kill(0)
    return ret

try:
    opts, args = getopt.gnu_getopt(sys.argv[1:], "hp:u", ["help"])
except getopt.GetoptError, err:
    print str(err)
    usage()
    sys.exit()

nhost = None
passw = None
untrust = False

for o, a in opts:
    if o in ("-h", "--help"):
        usage()
        sys.exit()
    elif o == "-p":
        passw = a
    elif o == "-u":
        untrust = True

if len(args) != 1:
    usage()
    sys.exit()
nhost = args[0]

if untrust:
    checkret = check()
    if checkret != 1:
        sys.exit()
    print "untrusting..."
    file = open(os.path.expanduser('~/.ssh/id_rsa.pub'))
    rsapub = file.read().rstrip('\n').replace('/', '\/')
    file.close()
    systemret = os.system('ssh ' + nhost + ' "sed -i \'/' + rsapub + '/d\' ~/.ssh/authorized_keys"')
    if systemret == 0:
        print "untrusting succeed" 
    else:
        print "untrusting fail"
    sys.exit()

if passw == None:
    usage()
    sys.exit()

checkret = check()
if checkret == 1 or checkret == 2:
    sys.exit()

print "starting configuring trust..."
rsapath = '~/.ssh/id_rsa.pub'
hasrsa = os.path.exists(os.path.expanduser(rsapath))

if not hasrsa:
	child = pexpect.spawn('ssh-keygen')
	child.expect(':')
	child.sendline()
	child.expect(':')
	child.sendline()
	child.expect(':')
	child.sendline()
	child.expect(pexpect.EOF)

file = open(os.path.expanduser('~/.ssh/id_rsa.pub'))
rsapub = file.read().rstrip('\n')
file.close()
child = pexpect.spawn('ssh ' + nhost)
child.expect('password:')
child.sendline(passw)
i = child.expect(['Permission denied', '[\$#]'])
if (i == 0):
	print "password NOT correct"
	sys.exit()
elif (i == 1):
	child.sendline('echo ' + rsapub + ' >> ~/.ssh/authorized_keys')
	child.expect('[\$#]')
	child.kill(0)
	print "configuring trust completed"

print "testing..."
child = pexpect.spawn('ssh ' + nhost)
i = child.expect(['password:', 'ssh:', '[\$#]'])
if (i == 0):
	print "configuring trust fail"
elif (i == 1):
	print "host NOT reachable"
elif (i == 2):
	print "configuring trust succeed"
Global site tag (gtag.js) - Google Analytics