您的位置:首页 > 运维架构 > Linux

pc机取得并修改arm linux IP地址 网关 子网掩码 主机名等信息

2010-01-01 20:59 453 查看
(一)获取

1、当pc发送广播,arm linux收到广播信息时,向客户端发送确认信息

客户端可通过socket取出IP地址

2、当pc机发送请求取得上述信息参数时,执行以下系统调用

system("hostname > hostfile");

system("ifconfig > ipfile");

system("route > gwfile");

并将这些文件读取传送到客户端,即pc3、客户端接收到信息后,将其写到一个文件中4、通过文件的特殊标记定位,就可以取得子网掩码 网关 主机名等信息

(二)设置

1、取得设置的主机名、IP、网关、子网掩码等信息

2、将其格式化如下:

对主机名而言:

(1)单独发送到服务器,服务器程序将其写在一个单独文件中,如:/prog/HOSTNAME

(2)修改/etc/init.d/rcS应项为/bin/hostname -F /prog/HOSTNAME 对其他来说:

(1)格式化信息如下:

假若你输入的IP地址为172.29.26.41 子网掩码为255.255.255.0

/sbin/ifconfig eth0 172.29.26.41 netmask 255.255.255.0

(2)假若你输入的默认网关为172.29.26.1

/sbin/route add default gw 172.29.26.1

3、将其替代rcS中的对应项,然后发送到服务器端

4、重新启动服务器,设置生效//客户端获取

void CWireDialog::OnDownload()

{

/*

向服务器发送获取信息,服务器进行系统调用,将信息写在文件中:ip gw

*/

SOCKET sockfd;

SOCKADDR_IN sin,saUdpServ,saClient;

struct TO_SERVER m_msg;

int nSize,nbSize;

int ncount=0;

char *userinfo;

userinfo = new char[2048];

sockfd=socket(PF_INET,SOCK_DGRAM,0);

sin.sin_family = AF_INET;

sin.sin_port = htons(0);

sin.sin_addr.s_addr=htonl(INADDR_ANY);

if(bind( sockfd, (SOCKADDR *)&sin, sizeof(sin))!=0)

{

AfxMessageBox("不能创建网络接口/n");//初始化失败返回-1

closesocket(sockfd);

return;

}

saUdpServ.sin_family = AF_INET;

saUdpServ.sin_addr.s_addr = inet_addr(curIP);

saUdpServ.sin_port = htons (PORT);//发送用的端口,可以根据需要更改 //

nSize = sizeof ( SOCKADDR_IN );

m_msg.MsgID = ID_GETNETINFO;

sendto ( sockfd,(char*)&m_msg, sizeof(m_msg),0,(SOCKADDR *) &saUdpServ,sizeof ( SOCKADDR_IN ));

nSize = sizeof ( SOCKADDR_IN );

while(1)

{

if((nbSize=recvfrom (sockfd,(char*)&m_msg,sizeof(m_msg),0,(SOCKADDR FAR *) &saClient,&nSize))==-1)

{

AfxMessageBox("无法接到日志");

closesocket(sockfd);

return;

}

//正确接收日志文件,写文件

if(m_msg.MsgID == ID_RETNETINFO)

{

//AfxMessageBox("正确接收用户信息");

for(int i=0;i<nbSize-4;i++)

{

userinfo[recv] = m_msg.Msg.usermodify[i];

++recv;

}

else if(m_msg.MsgID = ID_NETINFOOVER)

{

break;

}

}

userinfo[recv] = '/0';

closesocket(sockfd);

CFile userFile;

if(!userFile.Open("netinfo/netinfo",CFile::modeCreate|CFile::modeWrite|CFile::typeBinary))

{

MessageBox("无法打用户信息文件");

return;

}

userFile.Write(userinfo,recv);

userFile.Close();

recv = 0;

delete[] userinfo;

/*

将需要的信息从文件中取出

*/

GetDlgItem(IDC_ID)->SetWindowText(curID);

GetDlgItem(IDC_IP)->SetWindowText(curIP); //IP

/*

NetMask

*/

setmask();

setgateway();

}void CWireDialog::setmask()

{

CFile nFile;

if(!nFile.Open("netinfo/netinfo",CFile::modeRead|CFile::typeBinary))

{

MessageBox("文件打开失败");

return;

}

int nlength = nFile.GetLength();

char *content;

content = new char[nlength]; int nret = nFile.Read(content,nlength);

if(nret < 0)

{

MessageBox("文件读取失败");

return;

}

int pos;

for(int i=0;i<nlength-4;i++)

{

if(content[i] == 'M' && content[i+1] == 'a' && content[i+2] == 's' && content[i+3] == 'k')

{

pos = i;

break;

}

}

if(i == nlength - 5)

{

MessageBox("不能找到子网掩码");

return;

}

pos += 5;

CString str,mask;

while(content[pos] != '/n')

{

str.Format("%c",content[pos]);

mask += str;

pos ++;

}

GetDlgItem(IDC_MASK)->SetWindowText(mask);

nFile.Close();

}void CWireDialog::setgateway()

{

CFile nFile;

if(!nFile.Open("netinfo/netinfo",CFile::modeRead|CFile::typeBinary))

{

MessageBox("文件打开失败");

return;

}

int nlength = nFile.GetLength();

char *con;

con = new char[nlength];

int ret = nFile.Read(con,nlength);

if(ret < 0)

{

MessageBox("文件读取失败");

return;

}

int pos;

for(int i=0;i<nlength-7;i++)

{

if(con[i]=='d'&&con[i+1]=='e'&&con[i+2]=='f'&&con[i+3]=='a'&&con[i+4]=='u'&&con[i+5]=='l'&&con[i+6]=='t')

{

pos = i;

break;

}

}

if(i == nlength-8)

{

MessageBox("不能找到默认网关");

return;

}

pos += 7;

while(con[pos] == ' ')

pos++;

CString str,gw;

while(con[pos] != ' ')

{

str.Format("%c",con[pos]);

gw += str;

pos ++;

}

GetDlgItem(IDC_GATEWAY)->SetWindowText(gw);

nFile.Close();

}//客户端设置

void CWireDialog::OnSet()

{

/*

将信息格式化,然后写入文件

*/

UpdateData(TRUE);

int ret = writeinfo();

if(ret != 0)

{

MessageBox("格式化配置信息失败");

return;

} SOCKET sockfd;

SOCKADDR_IN sin,saUdpServ,saClient;

struct TO_SERVER m_msg;

int nSize,nbSize;

int ncount=0;

sockfd=socket(PF_INET,SOCK_DGRAM,0);

sin.sin_family = AF_INET;

sin.sin_port = htons(0);

sin.sin_addr.s_addr=htonl(INADDR_ANY);

if(bind( sockfd, (SOCKADDR *)&sin, sizeof(sin))!=0)

{

AfxMessageBox("不能创建网络接口/n");//初始化失败返回-1

closesocket(sockfd);

return;

}

saUdpServ.sin_family = AF_INET;

saUdpServ.sin_addr.s_addr = inet_addr(curIP);

saUdpServ.sin_port = htons (PORT);//发送用的端口,可以根据需要更改 /*

发送修改主机名称请求

*/

nSize = sizeof ( SOCKADDR_IN );

m_msg.MsgID = ID_SETHOSTNAME;

strcpy(m_msg.Msg.usermodify,m_strid);

ncount = m_strid.GetLength();

sendto ( sockfd,(char*)&m_msg, ncount+4,0,(SOCKADDR *) &saUdpServ,sizeof ( SOCKADDR_IN ));

nSize = sizeof ( SOCKADDR_IN );

/*

发送修改IP 网关 子网掩码请求

*/

CFile nFile;

if(!nFile.Open("netinfo/rcS",CFile::modeRead|CFile::typeBinary))

{

MessageBox("打开配置文件失败");

return;

}

ncount = nFile.GetLength();

if(!nFile.Read(m_msg.Msg.usermodify,ncount))

{

MessageBox("读取配置文件失败");

return;

}

m_msg.MsgID = ID_SETNETINFO;

sendto ( sockfd,(char*)&m_msg, ncount+4,0,(SOCKADDR *) &saUdpServ,sizeof ( SOCKADDR_IN ));

/*

等待服务器返回标志

*/

if((nbSize=recvfrom (sockfd,(char*)&m_msg,sizeof(m_msg),0,(SOCKADDR FAR *) &saClient,&nSize))==-1)

{

MessageBox("无法接受正确反馈信息");

closesocket(sockfd);

return;

}

if(m_msg.MsgID == ID_SETNETOK)

{

MessageBox("成功修改/n要使修改生效,请重新启动网络");

}

else if(m_msg.MsgID = ID_NETINFOOVER)

{

MessageBox("修改失败");

}

closesocket(sockfd);

return;

}int CWireDialog::writeinfo()

{

UpdateData(TRUE);

CFile nFile;

CString info;

char *buf;

if(!nFile.Open("netinfo/basic",CFile::modeRead|CFile::typeBinary))

{

MessageBox("基本配置文件打开失败");

return -1;

}

int nlength = nFile.GetLength();

buf = new char[nlength];

if(!nFile.Read(buf,nlength))

{

MessageBox("基本配置信息读取失败");

return -1;

}

nFile.Close();

if(!nFile.Open("netinfo/rcS",CFile::modeCreate|CFile::modeWrite|CFile::typeBinary))

{

MessageBox("创建配置文件失败");

return -1;

}

nFile.Write(buf,nlength);

info.Format("/sbin/ifconfig eth0 %s netmask %s/n",m_strip,m_strmask);

nFile.Write(info,info.GetLength());

info.Empty();

info.Format("/sbin/route add default gw %s",m_strgate);

nFile.Write(info,info.GetLength());

return 0;

}

//服务器端设置

if(m_msg.MsgID == ID_SETHOSTNAME)

{

printf("receve quest to modify the host name/n");

int ncount;

if((nfile = open("HOSTNAME",O_WRONLY|O_CREAT|O_TRUNC)) == -1)

{

printf("the host file open error/n");

return ;

}

if((ncount = write(nfile,m_msg.Msg.usermodify,nrsize-4)) != nrsize-4)

{

printf("write host file error");

return ;

}

close(nfile);

break;

}

if(m_msg.MsgID == ID_SETNETINFO)

{

printf("receive quest to modify the net info/n");

int count;

rename("/etc/init.d/rcS","/etc/init.d/rcSold");

printf("rename the rcS file/n");

if((nfile = open("/etc/init.d/rcS",O_WRONLY|O_CREAT|O_TRUNC)) == -1)

{

printf("the rcs file open error/n");

rename("/etc/init.d/rcSold","/etc/init.d/rcS");

printf("rename the rcS file/n");

m_msg.MsgID = ID_SETNETFAIL;

sendto(sockfd,(char*)&m_msg,4,0,(struct sockaddr*)&fromsin,sin_size);

return;

}

if((count = write(nfile,m_msg.Msg.usermodify,nrsize-4)) != nrsize-4)

{

printf("write the rcs file fail/n");

rename("/etc/init.d/rcSold","/etc/init.d/rcS");

printf("rename the rcS file/n");

m_msg.MsgID = ID_SETNETFAIL;

sendto(sockfd,(char*)&m_msg,4,0,(struct sockaddr*)&fromsin,sin_size);

close(nfile);

}

else

{

printf("write the rcs file success/n");

m_msg.MsgID = ID_SETNETOK;

}

sendto(sockfd,(char*)&m_msg,4,0,(struct sockaddr*)&fromsin,sin_size);

close(nfile);

chmod("/etc/init.d/rcS",755);

printf("change the attr of the rcS file/n");

break;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: