當前位置:編程學習大全網 - 編程軟體 - 如何在c\c++程序中執行shell腳本命令?

如何在c\c++程序中執行shell腳本命令?

在C/C++源代碼中執行shell命令,最基本的函數就是system() C庫函數。其次,可以使用系統提供的api函數,比如windows上的createprocess shellexecuteex,linux上的exec()等函數。

下面是我在windows上做的測試版,用的是線程。我覺得用nc測試比較好,直接用netstat-an | Findstr/I " UDP " | Findstr " 22 "比較好,因為22口太特殊了,很容易被抓到。另外,nc判斷端口的常規方式是看提示信息。

# include & ltstdio.h & gt

# include & ltstdlib.h & gt

# include & lttchar.h & gt

# include & ltwindows.h & gt

# include & ltprocess.h & gt

UINT _ _ stdcall thread func(LPVOID lpParam){

//使用黑客字符串作為nc提交的數據。

int ret = system("回聲黑客| nc.exe-u 127.1 22 ");

//ret==0表示端口22沒有打開。

if (ret == 0 ) printf("nc:寫錯誤:連接被拒絕。\ n ");

返回0;

}

int main()

{

HANDLE hThread =(HANDLE)_ beginthreadex(0,0,ThreadFunc,0,0,0);

//等待3秒,表示端口22可用。

WaitForSingleObject(hThread,3000);

//結束測試線程

TerminateThread(hThread,0);

//重新打開NC連接的22號端口。

system(" NC . exe-vv-u 127.1 22 ");

返回0;

}

歡迎來到X 30貼吧,壹個高質量的計算機技術交流平臺。

  • 上一篇:linux下C語言編程如何判斷壹個程序是否運行成功 (結果是壹個界面) ?
  • 下一篇:重慶鼎信通電氣有限公司怎麽樣?
  • copyright 2024編程學習大全網