linux擷取主機資訊

來源:互聯網
上載者:User

標籤:


linux中主機的資訊通常儲存在/etc/hosts中,我們可以通過函數 gethostent讀取該檔案的資訊。




注意返回的struct hostent 結構體對象的地址是一塊靜態緩衝區,這個靜態緩衝區的地址是固定的,當多次調用gethostent的時候,後面調用
會把前面緩衝區內容覆蓋掉,多次讀取返回的指標指向的緩衝區的內容是一致的,這有助於防止沒有釋放記憶體而導致的記憶體流失。
 
  1. #include<netdb.h>
  2. #include<stdio.h>
  3. void getHostent()
  4. {
  5. struct hostent* host1;
  6. host1 = gethostent();
  7. struct hostent* host2;
  8. host2 = gethostent();
  9. printf("host1:%x,host2:%x\n",host1,host2);
  10. printf("h_name:%s\n",host1->h_name);
  11. ///////
  12. }
  13. int main()
  14. {
  15. getHostent();
  16. return 0;
  17. }



如果在調用gethostent的時候hosts檔案沒有開啟,這個函數會開啟該檔案,如果要關閉該檔案,可以使用endhostent函數:







來自為知筆記(Wiz)

linux擷取主機資訊

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.