日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 运维知识 > linux >内容正文

linux

sharpssh远程linux监控系统,利用SharpSsh远程执行linux的shell命令

發(fā)布時(shí)間:2025/3/12 linux 52 豆豆
生活随笔 收集整理的這篇文章主要介紹了 sharpssh远程linux监控系统,利用SharpSsh远程执行linux的shell命令 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

利用SharpSsh遠(yuǎn)程執(zhí)行l(wèi)inux的shell命令 (2011-07-26 14:38:02)

SharpSSH是一個(gè)C#的開源項(xiàng)目,可以利用SSH連接linux系統(tǒng)。并執(zhí)行shell等命令。

而SharpSSH提供的例子的輸入輸出都是定向到console。因此不容易從其中取出它的結(jié)果。

因此需要對源碼進(jìn)行一定的修改,從而得到我們想要的結(jié)果。

執(zhí)行SSH前,應(yīng)確保linux主機(jī)上的服務(wù)已啟動,命令:service sshdstart

引用工程:SharpSSH

源代碼:

static voidMain(string[]args)

{

try

{

//Create a new JSch instance

JSch jsch = newJSch();

//Prompt for username and serverhost

Console.WriteLine("Please input hostname:");

String host = Console.ReadLine();

Console.WriteLine("Please input username:");

String user = Console.ReadLine();

Console.WriteLine("Please input password:");

String pwd = Console.ReadLine();

//Create a new SSH session

Session session = jsch.getSession(user, host,22);

// username and password will be givenvia UserInfo interface.

UserInfo ui = newShellUserInfo();

ui.setPassword(pwd);

session.setUserInfo(ui);

//Connect to remote SSHserver

session.connect();

//Open a new Shell channel on the SSHsession

Channel channel = session.openChannel("shell");

//Redirect standard I/O to the SSHchannel

channel.setInputStream(Console.OpenStandardInput());

channel.setOutputStream(Console.OpenStandardOutput());

//Connect the channel

channel.connect();

Console.WriteLine("-- Shell channel is connected using the {0}cipher", session.getCipher());

//Wait till channel is closed

while(!channel.isClosed())

{

System.Threading.Thread.Sleep(500);

}

//Disconnect from remoteserver

channel.disconnect();

session.disconnect();

}

catch (Exception e)

{

Console.WriteLine(e);

}

}

以上只能將輸入輸出定向到console。

修改:打開SharpSSH中的SshStream.cs,加一個(gè)方法

public voidset_OutputStream(Streamstream){m_channel.setOutputStream(stream);}

接下來封裝一個(gè)類:

class ShellHelp

{

System.IO.MemoryStreamoutputstream = newMemoryStream();

Tamir.SharpSsh.SshStream inputstream = null;

Channel channel = null;

Session session = null;

///

/// 命令等待標(biāo)識

///

string waitMark = "]#";

///

/// 打開連接

///

///

///

///

///

public bool OpenShell(string host, string username, string pwd)

{

try

{

Redirect standard I/O to the SSHchannel

inputstream = newTamir.SharpSsh.SshStream(host, username, pwd);

///我手動加進(jìn)去的方法。。為了讀取輸出信息

inputstream.set_OutputStream(outputstream);

return inputstream != null;

}

catch{throw;}

}

///

/// 執(zhí)行命令

///

///

public bool Shell(string cmd)

{

if (inputstream == null) returnfalse;

string initinfo =GetAllString();

inputstream.Write(cmd);

inputstream.Flush();

string currentinfo =GetAllString();

while (currentinfo ==initinfo)

{

System.Threading.Thread.Sleep(100);

currentinfo = GetAllString();

}

return true;

}

///

/// 獲取輸出信息

///

///

public string GetAllString()

{

string outinfo = Encoding.UTF8.GetString(outputstream.ToArray());

//等待命令結(jié)束字符

while(!outinfo.Trim().EndsWith(waitMark))

{

System.Threading.Thread.Sleep(200);

outinfo = Encoding.UTF8.GetString(outputstream.ToArray());

}

outputstream.Flush();

returnoutinfo.ToString();

}

///

/// 關(guān)閉連接

///

public void Close()

{

if (inputstream != null) inputstream.Close();

}

}

注意:string?waitMark?=?"]#";?在這里是用來標(biāo)識命令是否執(zhí)行完成的,,執(zhí)行完成就會在后面輸出這個(gè)字符,,有時(shí)也有可能是"]$"

接下來執(zhí)行shell命令:

static voidMain(string[] args)

{

Console.WriteLine("Please input hostname:");

String host = Console.ReadLine();

Console.WriteLine("Please input username:");

String user = Console.ReadLine();

Console.WriteLine("Please input password:");

String pwd = Console.ReadLine();

ShellHelp shell = newShellHelp();

//連接linux成功

if (shell.OpenShell(host, user,pwd))

{

shell.Shell("df-h");//執(zhí)行獲取命令

//shell.Shell("dmidecode");//執(zhí)行獲取命令

string info =shell.GetAllString();//獲取返回結(jié)果

Console.WriteLine(info);

shell.Close();//關(guān)閉連接

}

Console.ReadLine();

}

轉(zhuǎn)自:http://blog.sina.com.cn/s/blog_6ffbfc880100tt85.html

總結(jié)

以上是生活随笔為你收集整理的sharpssh远程linux监控系统,利用SharpSsh远程执行linux的shell命令的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。