sharpssh远程linux监控系统,利用SharpSsh远程执行linux的shell命令
利用SharpSsh遠程執行linux的shell命令 (2011-07-26 14:38:02)
SharpSSH是一個C#的開源項目,可以利用SSH連接linux系統。并執行shell等命令。
而SharpSSH提供的例子的輸入輸出都是定向到console。因此不容易從其中取出它的結果。
因此需要對源碼進行一定的修改,從而得到我們想要的結果。
執行SSH前,應確保linux主機上的服務已啟動,命令: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,加一個方法
public voidset_OutputStream(Streamstream){m_channel.setOutputStream(stream);}
接下來封裝一個類:
class ShellHelp
{
System.IO.MemoryStreamoutputstream = newMemoryStream();
Tamir.SharpSsh.SshStream inputstream = null;
Channel channel = null;
Session session = null;
///
/// 命令等待標識
///
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);
///我手動加進去的方法。。為了讀取輸出信息
inputstream.set_OutputStream(outputstream);
return inputstream != null;
}
catch{throw;}
}
///
/// 執行命令
///
///
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());
//等待命令結束字符
while(!outinfo.Trim().EndsWith(waitMark))
{
System.Threading.Thread.Sleep(200);
outinfo = Encoding.UTF8.GetString(outputstream.ToArray());
}
outputstream.Flush();
returnoutinfo.ToString();
}
///
/// 關閉連接
///
public void Close()
{
if (inputstream != null) inputstream.Close();
}
}
注意:string?waitMark?=?"]#";?在這里是用來標識命令是否執行完成的,,執行完成就會在后面輸出這個字符,,有時也有可能是"]$"
接下來執行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");//執行獲取命令
//shell.Shell("dmidecode");//執行獲取命令
string info =shell.GetAllString();//獲取返回結果
Console.WriteLine(info);
shell.Close();//關閉連接
}
Console.ReadLine();
}
轉自:http://blog.sina.com.cn/s/blog_6ffbfc880100tt85.html
總結
以上是生活随笔為你收集整理的sharpssh远程linux监控系统,利用SharpSsh远程执行linux的shell命令的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: jsp中java代码无效_来杯咖啡,教你
- 下一篇: 利用next_permutation解答