透明 发表于 2015-1-16 13:10:09

来一发Linux下远程叫醒盘算机 源码[C]

如果你只是想应付一下操作系统的课程,劝你最好别学,或者说不要指望能用的怎么样。
C的,Win和Linux都能够用,Linux下gcc编译经由过程复制内容到剪贴板代码:
/*ether-wake.c:Sendamagicpackettowakeupsleepingmachines.*/
staticcharversion_msg[]=
"ether-wake.c:v1.0911/12/2003DonaldBecker,http://www.scyld.com/";
staticcharbrief_usage_msg[]=
"usage:ether-wake[-i<ifname>][-paa:bb:cc:dd[:ee:ff]]00:11:22:33:44:55
"
"Use-utoseethecompletesetofoptions.
";
staticcharusage_msg[]=
"usage:ether-wake[-i<ifname>][-paa:bb:cc:dd[:ee:ff]]00:11:22:33:44:55
"
"
"
"ThisprogramgeneratesandtransmitsaWake-On-LAN(WOL)
"
""MagicPacket",usedforrestartingmachinesthathavebeen
"
"soft-powered-down(ACPID3-warmstate).
"
"ItcurrentlygeneratesthestandardAMDMagicPacketformat,with
"
"anoptionalpasswordappended.
"
"
"
"ThesinglerequiredparameteristheEthernetMAC(station)address
"
"ofthemachinetowakeorahostIDwithknownNSSethersentry.
"
"TheMACaddressmaybefoundwiththearpprogramwhilethetarget
"
"machineisawake.
"
"
"
"Options:
"
"-bSendwake-uppackettothebroadcastaddress.
"
"-DIncreasethedebuglevel.
"
"-iifnameUseinterfaceIFNAMEinsteadofthedefaulteth0.
"
"-p<pw>AppendthefourorsixbytepasswordPWtothepacket.
"
"Apasswordisonlyrequiredforafewadaptertypes.
"
"Thepasswordmaybespecifiedinethernethexformat
"
"ordotteddecimal(Internetaddress)
"
"-p00:22:44:66:88:aa
"
"-p192.168.1.1
";
/*
ThisprogramgeneratesandtransmitsaWake-On-LAN(WOL)"MagicPacket",
usedforrestartingmachinesthathavebeensoft-powered-down
(ACPID3-warmstate).ItcurrentlygeneratesthestandardAMDMagicPacket
format,withanoptionalpasswordappended.
Thissoftwaremaybeusedanddistributedaccordingtotheterms
oftheGNUPublicLicense,incorporatedhereinbyreference.
Contacttheauthorforuseunderotherterms.
Thissourcefilewasoriginallypartofthenetworktrickspackage,and
isnowdistributedtosupporttheScyldBeowulfsystem.
Copyright1999-2003DonaldBeckerandScyldComputingCorporation.
Theauthormaybereachedasbecker@scyld,orC/O
ScyldComputingCorporation
914BayRidgeRoad,Suite220
AnnapolisMD21403
Notes:
Onsomesystemsdroppingrootcapabilityallowstheprocesstobe
dumped,tracedordebugged.
Ifsomeonetracesthisprogram,theygetcontrolofarawsocket.
Linuxhandlesthissafely,butbewarewhenportingthisprogram.
AnalternativetoneedingrootisusingaUDPbroadcastsocket,however
doingsoonlyworkswithadaptersconfiguredforunicast+broadcastRx
filter.Thatconfigurationconsumesmorepower.
*/
#include<unistd.h>
#include<stdlib.h>
#include<stdio.h>
#include<errno.h>
#include<ctype.h>
#include<string.h>
#if0/*Onlyexistsonsomeversions.*/
#include<ioctls.h>
#endif
#include<sys/socket.h>
#include<sys/types.h>
#include<sys/ioctl.h>
#include<linux/if.h>
#include<features.h>
#if__GLIBC__>=2&&__GLIBC_MINOR>=1
#include<netpacket/packet.h>
#include<net/ethernet.h>
#else
#include<asm/types.h>
#include<linux/if_packet.h>
#include<linux/if_ether.h>
#endif
#include<netdb.h>
#include<netinet/ether.h>
/*Grrr,noconsistencybetweenincludeversions.
Enablethisifsetsockopt()isntdeclaredwithyourlibrary.*/
#if0
externintsetsockopt__P((int__fd,int__level,int__optname,
__ptr_t__optval,int__optlen));
#else/*New,correctheadfiles.*/
#include<sys/socket.h>
#endif
u_charoutpack;
intoutpack_sz=0;
intdebug=0;
u_charwol_passwd;
intwol_passwd_sz=0;
staticintopt_no_src_addr=0,opt_broadcast=0;
staticintget_dest_addr(constchar*arg,structether_addr*eaddr);
staticintget_fill(unsignedchar*pkt,structether_addr*eaddr);
staticintget_wol_pw(constchar*optarg);
intmain(intargc,char*argv[])
{
char*ifname="eth0";
intone=1;&nbsp
12345下一页


RedHatCentOS等等.学习linux不是逛自由市场,选定版本就要静下心来学习.不要今天换版本明天要升级.这样对你没有好处。

透明 发表于 2015-1-16 14:12:54

来一发Linux下远程叫醒盘算机 源码[C]

常用的linux命令,尤其是一些能帮你提高开发效率的命令,(eg:grep,awk,sed,split等);
;/*True,forsocketoptions.*/
ints;/*Rawsocket*/
interrflag=0,verbose=0,do_version=0;
intperm_failure=0;
inti,c,pktsize;
#ifdefined(PF_PACKET)
structsockaddr_llwhereto;
#else
structsockaddrwhereto;/*whotowakeup*/
#endif
structether_addreaddr;</P>while((c=getopt(argc,argv,"bDi:p:uvV"))!=-1)
switch(c){
caseb:opt_broadcast++;break;
caseD:debug++;break;
casei:ifname=optarg;break;
casep:get_wol_pw(optarg);break;
caseu:printf(usage_msg);return0;
casev:verbose++;break;
caseV:do_version++;break;
case?:
errflag++;
}
if(verbose||do_version)
printf("%s\n",version_msg);
if(errflag){
fprintf(stderr,brief_usage_msg);
return3;
}
if(optind==argc){
fprintf(stderr,"SpecifytheEthernetaddressas00:11:22:33:44:55.\n");
return3;
}
/*Note:PF_INET,SOCK_DGRAM,IPPROTO_UDPwouldallowSIOCGIFHWADDRto
workasnon-root,butweneedSOCK_PACKETtospecifytheEthernet
destinationaddress.*/
#ifdefined(PF_PACKET)
s=socket(PF_PACKET,SOCK_RAW,0);
#else
s=socket(AF_INET,SOCK_PACKET,SOCK_PACKET);
#endif
if(s<0){
if(errno==EPERM)
fprintf(stderr,"ether-wake:Thisprogrammustberunasroot.\n");
else
perror("ether-wake:socket");
perm_failure++;
}
/*Dontrevertifdebuggingallowsanormalusertogettherawsocket.*/
setuid(getuid());
/*Welookupthestationaddressbeforereportingfailuresothat
errorsmaybereportedevenwhenrunasanormaluser.
*/
if(get_dest_addr(argv,&eaddr)!=0)
return3;
if(perm_failure&&!debug)
return2;
pktsize=get_fill(outpack,&eaddr);
/*Fillinthesourceaddress,ifpossible.
ThecodetoretrievethelocalstationaddressisLinuxspecific.*/
if(!opt_no_src_addr){
structifreqif_hwaddr;
unsignedchar*hwaddr=if_hwaddr.ifr_hwaddr.sa_data;
strcpy(if_hwaddr.ifr_name,ifname);
if(ioctl(s,SIOCGIFHWADDR,&if_hwaddr)<0){
fprintf(stderr,"SIOCGIFHWADDRon%sfailed:%s\n",ifname,
strerror(errno));
/*Magicpacketsstillworkifoursourceaddressisbogus,but
wefailjusttobeanal.*/
return1;
}
memcpy(outpack+6,if_hwaddr.ifr_hwaddr.sa_data,6);
&n
上一页12345下一页


系统管理相关命令:df、top、free、quota、at、lp、adduser、groupaddkill、crontab、tar、unzip、gunzip、last

透明 发表于 2015-1-16 14:24:54

来一发Linux下远程叫醒盘算机 源码[C]

如果你学不好的话,你在linux中开发的机会就很少,或者说几乎没有,它的优势就消失了,然后随着时间的流逝,你就会全部忘记她;
bsp;if(verbose){
printf("Thehardwareaddress(SIOCGIFHWADDR)of%sistype%d"
"%2.2x:%2.2x:%2.2x:%2.2x:%2.2x:%2.2x.\n",ifname,
if_hwaddr.ifr_hwaddr.sa_family,hwaddr,hwaddr,
hwaddr,hwaddr,hwaddr,hwaddr);
}
}</P>if(wol_passwd_sz>0){
memcpy(outpack+pktsize,wol_passwd,wol_passwd_sz);
pktsize+=wol_passwd_sz;
}
if(verbose>1){
printf("Thefinalpacketis:");
for(i=0;i<pktsize;i++)
printf("%2.2x",outpack);
printf(".\n");
}
/*Thisisnecessaryforbroadcaststowork*/
if(setsockopt(s,SOL_SOCKET,SO_BROADCAST,(char*)&one,sizeof(one))<0)
perror("setsockopt:SO_BROADCAST");
#ifdefined(PF_PACKET)
{
structifreqifr;
strncpy(ifr.ifr_name,ifname,sizeof(ifr.ifr_name));
if(ioctl(s,SIOCGIFINDEX,&ifr)==-1){
fprintf(stderr,"SIOCGIFINDEXon%sfailed:%s\n",ifname,
strerror(errno));
return1;
}
memset(&whereto,0,sizeof(whereto));
whereto.sll_family=AF_PACKET;
whereto.sll_ifindex=ifr.ifr_ifindex;
/*Themanualpageincorrectlyclaimstheaddressmustbefilled.
Wedosobecausethecodemaychangetomatchthedocs.*/
whereto.sll_halen=ETH_ALEN;
memcpy(whereto.sll_addr,outpack,ETH_ALEN);
}
#else
whereto.sa_family=0;
strcpy(whereto.sa_data,ifname);
#endif
if((i=sendto(s,outpack,pktsize,0,(structsockaddr*)&whereto,
sizeof(whereto)))<0)
perror("sendto");
elseif(debug)
printf("Sendtoworked!%d.\n",i);
#ifdefUSE_SEND
if(bind(s,(structsockaddr*)&whereto,sizeof(whereto))<0)
perror("bind");
elseif(send(s,outpack,100,0)<0)
perror("send");
#endif
#ifdefUSE_SENDMSG
{
structmsghdrmsghdr={0,};
structioveciovector;
msghdr.msg_name=&whereto;
msghdr.msg_namelen=sizeof(whereto);
msghdr.msg_iov=iovector;
msghdr.msg_iovlen=1;
iovector.iov_base=outpack;
iovector.iov_len=pktsize;
if((i=sendmsg(s,&msghdr,0))<0)
perror("sendmsg");
elseif(debug)
printf("sendmsgworked,%d(%d).\n",i,errno);
}
#endif
return0;
}
/*ConvertthehostIDstringtoaMACaddress.
Thestringmaybea
Hostname
IPaddressstring
MACaddressstring
*/
stat
上一页12345下一页


RedHatCentOS等等.学习linux不是逛自由市场,选定版本就要静下心来学习.不要今天换版本明天要升级.这样对你没有好处。

透明 发表于 2015-1-16 14:31:34

来一发Linux下远程叫醒盘算机 源码[C]

常常有些朋友在Linux论坛问一些问题,不过,其中大多数的问题都是很基的。
icintget_dest_addr(constchar*hostid,structether_addr*eaddr)
{
structether_addr*eap;</P>eap=ether_aton(hostid);
if(eap){
*eaddr=*eap;
if(debug)
fprintf(stderr,"Thetargetstationaddressis%s.\n",
ether_ntoa(eaddr));
}elseif(ether_hostton(hostid,eaddr)==0){
if(debug)
fprintf(stderr,"Stationaddressforhostname%sis%s.\n",
hostid,ether_ntoa(eaddr));
}else{
(void)fprintf(stderr,
"ether-wake:TheMagicPackethostaddressmustbe"
"specifiedas\n"
"-astationaddress,00:11:22:33:44:55,or\n"
"-ahostnamewithaknownethersentry.\n");
return-1;
}
return0;
}

staticintget_fill(unsignedchar*pkt,structether_addr*eaddr)
{
intoffset,i;
unsignedchar*station_addr=eaddr->ether_addr_octet;
if(opt_broadcast)
memset(pkt+0,0xff,6);
else
memcpy(pkt,station_addr,6);
memcpy(pkt+6,station_addr,6);
pkt=0x08;/*Or0x0806forARP,0x8035forRARP*/
pkt=0x42;
offset=14;
memset(pkt+offset,0xff,6);
offset+=6;
for(i=0;i<16;i++){
memcpy(pkt+offset,station_addr,6);
offset+=6;
}
if(debug){
fprintf(stderr,"Packetis");
for(i=0;i<offset;i++)
fprintf(stderr,"%2.2x",pkt);
fprintf(stderr,".\n");
}
returnoffset;
}
staticintget_wol_pw(constchar*optarg)
{
intpasswd;
intbyte_cnt;
inti;
byte_cnt=sscanf(optarg,"%2x:%2x:%2x:%2x:%2x:%2x",
&passwd,&passwd,&passwd,
&passwd,&passwd,&passwd);
if(byte_cnt<4)
byte_cnt=sscanf(optarg,"%d.%d.%d.%d",
&passwd,&passwd,&passwd,&passwd);
if(byte_cnt<4){
fprintf(stderr,"UnabletoreadtheWake-On-LANpassword.\n");
return0;
}
printf("TheMagicpacketpasswordis%2.2x%2.2x%2.2x%2.2x(%d).\n",
passwd,passwd,passwd,passwd,byte_cnt);
for(i=0;i<byte_cnt;i++)
wol_passwd=passwd;
returnwol_passwd_sz=byte_cnt;
}
#if0
{
to=(structsockaddr_in*)&whereto;
to->sin_family=AF_INET;
if(inet_aton(target,&to->sin_addr)){

上一页12345下一页


给你装的系统里为ubuntu12.04,它已经封装的很臃肿了,但是考虑到你没有很多时间投入其中,所以给你装了它,但是怎么用它提高开发效率,需要你在学习的过程中不断总结;

透明 发表于 2015-1-16 14:39:05

来一发Linux下远程叫醒盘算机 源码[C]

对于linux命令,一定要学会用man和info去查他们的解释;
hostname=target;
}
memset(&sa,0,sizeofsa);
sa.sa_family=AF_INET;
strncpy(sa.sa_data,interface,sizeofsa.sa_data);
sendto(sock,buf,bufix+len,0,&sa,sizeofsa);
strncpy(sa.sa_data,interface,sizeofsa.sa_data);
#if1
sendto(sock,buf,bufix+len,0,&sa,sizeofsa);
#else
bind(sock,&sa,sizeofsa);
connect();
send(sock,buf,bufix+len,0);
#endif
}
#endif</P>
/*
*Localvariables:
*compile-command:"gcc-O-Wall-oether-wakeether-wake.c"
*c-indent-level:4
*c-basic-offset:4
*c-indent-level:4
*tab-width:4
*End:
*/
</p>上一页12345


写学习日记,这是学习历程的见证,同时我坚持认为是增强学习信念的法宝。以上是我学习Linux的心得体会,希望对大家的学习有所帮助,由于水平有限,本文难免有所欠缺,望请指正。

愤怒的大鸟 发表于 2015-1-18 15:45:40

任何一个叫做操作系统的工具都是这样子构成的:内核+用户界面+一般应用程序。

只想知道 发表于 2015-1-27 10:07:00

尽我能力帮助他人,在帮助他人的同时你会深刻巩固知识。

简单生活 发表于 2015-2-5 10:11:40

任何人都可以根据自己的喜好来定制适合自己的操作系统,Linux?是抢占式多任务多用户操作系统.

精灵巫婆 发表于 2015-2-11 09:23:46

熟读Linux系统有关知识,如系统目录树,有关内容可购书阅读或搜索论坛。

山那边是海 发表于 2015-3-2 08:57:10

如果你想深入学习Linux,看不懂因为文档实在是太难了。写的最好的、最全面的文档都是英语写的,最先发布的技术信息也都是用英语写的。

飘飘悠悠 发表于 2015-3-11 03:46:18

一定要学好命令,shell是命令语言,命令解释程序及程序设计语言的统称,shell也负责用户和操作系统之间的沟通。

再现理想 发表于 2015-3-17 20:10:40

其实当你安装了一个完整的Linux系统后其中已经包含了一个强大的帮助,只是可能你还没有发现和使用它们的技巧。

金色的骷髅 发表于 2015-3-25 01:20:58

发问的时候一定要注意到某些礼节。因为Linux社区是一个松散的组织、也不承担回复每个帖子的义务。它不是技术支持。
页: [1]
查看完整版本: 来一发Linux下远程叫醒盘算机 源码[C]