sharpssh远程linux监控系统,利用SharpSsh远程执行linux的shell命令
利用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)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: jsp中java代码无效_来杯咖啡,教你
- 下一篇: 安装linux前分区,安装Linux系统