C语言实现P命令
时间: 2023-07-07 14:14:04 浏览: 60
在C语言中,可以使用system函数来执行系统命令,其中可以通过传递参数来执行P命令。P命令的作用是打印当前进程的进程号(PID),可以使用如下代码实现:
```c
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main() {
char cmd[50];
sprintf(cmd, "echo $$$$");
system(cmd);
return 0;
}
```
在上面的代码中,使用sprintf函数将字符串"echo $$$$"赋值给字符数组cmd,其中"$$$$"是一个特殊的字符串,在shell中表示当前进程的PID。然后使用system函数执行cmd命令,即可打印当前进程的PID。
相关问题
c语言实现ping命令
实现ping命令需要使用系统底层的网络API,这里给出一个基于Linux系统的C语言实现示例:
```c
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <netinet/ip_icmp.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <sys/time.h>
#define PACKET_SIZE 4096
#define MAX_WAIT_TIME 5
#define MAX_NO_PACKETS 4
char sendpacket[PACKET_SIZE];
char recvpacket[PACKET_SIZE];
int sockfd, datalen = 56;
int nsend = 0, nreceived = 0;
struct sockaddr_in dest_addr;
struct sockaddr_in from;
struct timeval tvrecv;
unsigned short cal_chksum(unsigned short *addr, int len)
{
int sum = 0;
unsigned short res = 0;
while(len > 1)
{
sum += *addr++;
len -= 2;
}
if(len == 1)
{
*((unsigned char *)(&res)) = *((unsigned char *)addr);
sum += res;
}
sum = (sum >> 16) + (sum & 0xffff);
sum += (sum >> 16);
res = ~sum;
return res;
}
int pack(int pack_no)
{
int i, packsize;
struct icmp *icmp;
struct timeval *tval;
icmp = (struct icmp*)sendpacket;
icmp->icmp_type = ICMP_ECHO;
icmp->icmp_code = 0;
icmp->icmp_cksum = 0;
icmp->icmp_seq = pack_no;
icmp->icmp_id = getpid();
packsize = 8 + datalen;
tval = (struct timeval *)icmp->icmp_data;
gettimeofday(tval, NULL);
icmp->icmp_cksum = cal_chksum((unsigned short *)icmp, packsize);
return packsize;
}
int unpack(char *buf, int len)
{
int i, iphdrlen;
struct ip *ip;
struct icmp *icmp;
struct timeval *tvsend;
double rtt;
ip = (struct ip *)buf;
iphdrlen = ip->ip_hl << 2;
icmp = (struct icmp *)(buf + iphdrlen);
len -= iphdrlen;
if(len < 8)
{
printf("ICMP packets\'s length is less than 8\n");
return -1;
}
if((icmp->icmp_type == ICMP_ECHOREPLY) && (icmp->icmp_id == getpid()))
{
tvsend = (struct timeval *)icmp->icmp_data;
gettimeofday(&tvrecv, NULL);
tv_sub(&tvrecv, tvsend);
rtt = tvrecv.tv_sec * 1000 + tvrecv.tv_usec / 1000.0;
printf("%d byte from %s: icmp_seq=%u ttl=%d rtt=%.3f ms\n",
len, inet_ntoa(from.sin_addr), icmp->icmp_seq, ip->ip_ttl, rtt);
return 0;
}
else
{
return -1;
}
}
void tv_sub(struct timeval *out, struct timeval *in)
{
if((out->tv_usec -= in->tv_usec) < 0)
{
--out->tv_sec;
out->tv_usec += 1000000;
}
out->tv_sec -= in->tv_sec;
}
void ping()
{
int i, packsize;
struct timeval tv;
tv.tv_sec = MAX_WAIT_TIME;
tv.tv_usec = 0;
for (i = 0; i < MAX_NO_PACKETS; i++)
{
nsend++;
packsize = pack(nsend);
if (sendto(sockfd, sendpacket, packsize, 0, (struct sockaddr *)&dest_addr, sizeof(dest_addr)) < 0)
{
printf("sendto error: %s\n", strerror(errno));
continue;
}
if (recvfrom(sockfd, recvpacket, sizeof(recvpacket), 0, (struct sockaddr *)&from, (socklen_t *)&fromlen) < 0)
{
printf("recvfrom error: %s\n", strerror(errno));
continue;
}
if (unpack(recvpacket, sizeof(recvpacket)) == -1)
{
printf("unpack error\n");
continue;
}
nreceived++;
usleep(1000000);
}
}
int main(int argc, char *argv[])
{
struct hostent *host;
struct protoent *protocol;
char protoname[] = "icmp";
int size = 50 * 1024;
if (argc < 2)
{
printf("usage: ping hostname/IP address\n");
return 0;
}
if ((protocol = getprotobyname(protoname)) == NULL)
{
printf("getprotobyname() error\n");
return 0;
}
if ((sockfd = socket(AF_INET, SOCK_RAW, protocol->p_proto)) < 0)
{
printf("socket() error\n");
return 0;
}
setuid(getuid());
setsockopt(sockfd, SOL_SOCKET, SO_RCVBUF, &size, sizeof(size));
bzero(&dest_addr, sizeof(dest_addr));
dest_addr.sin_family = AF_INET;
if ((dest_addr.sin_addr.s_addr = inet_addr(argv[1])) == INADDR_NONE)
{
if ((host = gethostbyname(argv[1])) == NULL)
{
printf("gethostbyname() error\n");
return 0;
}
memcpy((char *)&dest_addr.sin_addr, host->h_addr, host->h_length);
}
printf("PING %s(%s) %d bytes of data.\n", argv[1], inet_ntoa(dest_addr.sin_addr), datalen);
ping();
printf("--- %s ping statistics ---\n", argv[1]);
printf("%d packets transmitted, %d received, %%%.2f packet loss\n",
nsend, nreceived, (nsend - nreceived) * 100.0 / nsend);
close(sockfd);
return 0;
}
```
这个实现使用了ICMP协议来模拟ping命令。需要注意的是,这个实现中使用了系统底层的网络API,因此需要在Linux系统下编译运行。如果需要在其他系统上运行,请根据具体系统的网络API进行调整。
用c语言实现ping命令
以下是使用C语言实现ping命令的示例代码:
```
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <netdb.h>
#include <netinet/in.h>
#include <netinet/ip_icmp.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <sys/time.h>
#define PACKET_SIZE 4096
#define MAX_WAIT_TIME 5
#define MAX_NO_PACKETS 3
char sendpacket[PACKET_SIZE];
char recvpacket[PACKET_SIZE];
int sockfd, datalen = 56;
int nsend = 0, nreceived = 0;
struct sockaddr_in dest_addr;
struct timeval tvrecv;
unsigned short cal_chksum(unsigned short *addr, int len);
void tv_sub(struct timeval *out, struct timeval *in) {
if ((out->tv_usec -= in->tv_usec) < 0) {
--out->tv_sec;
out->tv_usec += 1000000;
}
out->tv_sec -= in->tv_sec;
}
unsigned short cal_chksum(unsigned short *addr, int len) {
int nleft = len;
int sum = 0;
unsigned short *w = addr;
unsigned short answer = 0;
while (nleft > 1) {
sum += *w++;
nleft -= 2;
}
if (nleft == 1) {
*(unsigned char *)(&answer) = *(unsigned char *)w;
sum += answer;
}
sum = (sum >> 16) + (sum & 0xffff);
sum += (sum >> 16);
answer = ~sum;
return answer;
}
void send_packet() {
int packetsize;
struct icmp *icmp;
icmp = (struct icmp*) sendpacket;
icmp->icmp_type = ICMP_ECHO;
icmp->icmp_code = 0;
icmp->icmp_cksum = 0;
icmp->icmp_seq = nsend++;
icmp->icmp_id = getpid();
packetsize = 8 + datalen;
gettimeofday((struct timeval*) icmp->icmp_data, NULL);
icmp->icmp_cksum = cal_chksum((unsigned short *) icmp, packetsize);
sendto(sockfd, sendpacket, packetsize, 0, (struct sockaddr*) &dest_addr, sizeof(dest_addr));
}
void recv_packet() {
int n, fromlen;
extern int errno;
fromlen = sizeof(struct sockaddr_in);
while (1) {
fd_set rfds;
FD_ZERO(&rfds);
FD_SET(sockfd, &rfds);
struct timeval timeout;
timeout.tv_sec = MAX_WAIT_TIME;
timeout.tv_usec = 0;
int retval = select(sockfd + 1, &rfds, NULL, NULL, &timeout);
if (retval == -1) {
printf("select error\n");
break;
} else if (retval == 0) {
printf("request timeout.\n");
break;
} else {
n = recvfrom(sockfd, recvpacket, sizeof(recvpacket), 0, (struct sockaddr*) &dest_addr, &fromlen);
if (n < 0) {
if (errno == EINTR)
continue;
printf("recvfrom error\n");
break;
}
gettimeofday(&tvrecv, NULL);
struct ip *ip;
struct icmp *icmp;
ip = (struct ip*) recvpacket;
icmp = (struct icmp*) (recvpacket + (ip->ip_hl << 2));
int icmplen = n - (ip->ip_hl << 2);
if (icmp->icmp_type == ICMP_ECHOREPLY) {
if (icmp->icmp_id != getpid())
continue;
if (icmplen < 8)
continue;
if (icmp->icmp_seq != nreceived)
continue;
nreceived++;
printf("%d bytes from %s: icmp_seq=%u ttl=%d time=%ld ms\n", icmplen, inet_ntoa(dest_addr.sin_addr), icmp->icmp_seq, ip->ip_ttl, (tvrecv.tv_sec - ((struct timeval*) icmp->icmp_data)->tv_sec) * 1000 + (tvrecv.tv_usec - ((struct timeval*) icmp->icmp_data)->tv_usec) / 1000);
} else {
continue;
}
}
}
}
void ping() {
struct hostent *host;
struct protoent *protocol;
if ((protocol = getprotobyname("icmp")) == NULL) {
printf("getprotobyname error\n");
exit(1);
}
if ((sockfd = socket(AF_INET, SOCK_RAW, protocol->p_proto)) < 0) {
printf("socket error\n");
exit(1);
}
if ((host = gethostbyname("localhost")) == NULL) {
printf("gethostbyname error\n");
exit(1);
}
memset(&dest_addr, 0, sizeof(dest_addr));
dest_addr.sin_family = AF_INET;
dest_addr.sin_addr = *((struct in_addr*) host->h_addr);
printf("PING %s (%s): %d data bytes\n", host->h_name, inet_ntoa(dest_addr.sin_addr), datalen);
send_packet();
recv_packet();
close(sockfd);
}
int main(int argc, char **argv) {
ping();
return 0;
}
```
注意:该示例代码只实现了基本的ping功能,没有实现ping命令的全部参数和功能。如果需要实现更完整的ping命令,请参考相关文档或代码库。