[分享]新新新手写的ubuntu下载镜像测速命令
发表于 : 2007-07-30 15:33
在某些地方,下载速度最快的可能不是国内的服务器,比如我这里连接北美的就比较快。刚学shell,也不知道网址速度怎么测才算准确,所以就下载了一个简单的c程序来用了(版权信息在源码里有)。
调用的命令是:
bestsite `wget -q -O - "http://www.ubuntu.com/getubuntu/downloadmirrors" | awk -F "\"" '/BEGIN MIRROR LIST/,/END MIRROR LIST/ { if ( $0 ~ /http/ ) { print $2 } }' `
bestsite.c代码
调用的命令是:
bestsite `wget -q -O - "http://www.ubuntu.com/getubuntu/downloadmirrors" | awk -F "\"" '/BEGIN MIRROR LIST/,/END MIRROR LIST/ { if ( $0 ~ /http/ ) { print $2 } }' `
bestsite.c代码
代码: 全选
/*
Copyright 2004,2005,2006 Luigi Auriemma
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
http://www.gnu.org/licenses/gpl.txt
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <sys/times.h>
#include <unistd.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <arpa/inet.h>
#include <netdb.h>
#define VER "0.1.1"
#define TIMEOUT 3
#define TRIES 3
int conn_timeout(int sock);
char *get_host(char *buff, u_short *port);
void std_err(void);
u_int resolv(char *host);
int main(int argc, char *argv[]) {
char *host;
int sd,
err,
i,
j,
tmt;
clock_t start,
end,
new,
best;
u_short port;
struct sockaddr_in peer;
setbuf(stdout, NULL);
fputs("\n"
"Bestsite (tcp-pinger) "VER"\n"
"by Luigi Auriemma\n"
"e-mail: [email protected]\n"
"web: aluigi.org\n"
"\n", stdout);
if(argc < 2) {
printf("\nUsage: %s [URL1] [HOST2] ... [URLN]\n", argv[0]);
exit(1);
}
printf("\n"
"default timeout = %d seconds\n"
"default number of tries = %d\n"
"\nConnection delay time in seconds/100 (less delay = fastest server)\n"
"\n", TIMEOUT, TRIES);
peer.sin_family = AF_INET;
for(i = 1; i < argc; i++) {
host = get_host(argv[i], &port);
peer.sin_addr.s_addr = resolv(host);
if(!peer.sin_addr.s_addr) continue;
peer.sin_port = htons(port);
best = 0x7fffffff; /* = timeout */
tmt = 0; /* timeout tries */
for(j = 0; j < TRIES; j++) {
sd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if(!sd) std_err();
fputc(j + 0x31, stdout);
err = fcntl(sd, F_SETFL, O_NONBLOCK);
if(err < 0) std_err();
start = times(0);
if(start < 0) std_err();
connect(sd, (struct sockaddr *)&peer, sizeof(peer));
err = conn_timeout(sd);
end = times(0);
if(end < 0) std_err();
if(!err) {
best = 0x7fffffff;
close(sd);
if(tmt == 1) break; /* if 2 timeout, stop */
tmt++;
continue;
}
new = end - start;
if(new < best) best = new;
close(sd);
sleep(0);
}
if(best != 0x7fffffff) printf("\r%10lu = Host: %s:%hu\n", best, host, port);
else printf("\r timeout X Host: %s:%hu\n", host, port);
}
fputc('\n', stdout);
return(0);
}
int conn_timeout(int sock) {
struct timeval timeout;
fd_set fd1,
fd2,
fd3;
int err;
timeout.tv_sec = TIMEOUT;
timeout.tv_usec = 0;
FD_ZERO(&fd1);
FD_SET(sock, &fd1);
FD_ZERO(&fd2);
FD_SET(sock, &fd2);
FD_ZERO(&fd3);
FD_SET(sock, &fd3);
err = select(sock + 1, &fd1, &fd2, &fd3, &timeout);
if(err == 1) return(1);
else return(0);
}
char *get_host(char *buff, u_short *port) {
char *tmp,
*uri;
if(!strncasecmp(buff, "ftp://", 6)) {
tmp = buff + 6;
*port = 21;
} else if(!strncasecmp(buff, "http://", 7)) {
tmp = buff + 7;
*port = 80;
} else {
tmp = buff;
*port = 80;
}
uri = strchr(tmp, '/');
if(uri) *uri = 0;
uri = strchr(tmp, ':');
if(uri) {
*port = atoi(uri + 1);
*uri = 0;
}
return(tmp);
}
u_int resolv(char *hostchar) {
struct hostent *hp;
u_int host_ip;
host_ip = inet_addr(hostchar);
if(host_ip == INADDR_NONE) {
hp = gethostbyname(hostchar);
if(!hp) {
printf(" Error Unable to resolve hostname (%s)\n", hostchar);
return(0);
} else host_ip = *(u_int *)(hp->h_addr);
}
return(host_ip);
}
void std_err(void) {
perror("\nError");
exit(1);
}