本文整理汇总了C#中Tamir.SharpSsh.jsch.JSch类的典型用法代码示例。如果您正苦于以下问题:C# JSch类的具体用法?C# JSch怎么用?C# JSch使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
JSch类属于Tamir.SharpSsh.jsch命名空间,在下文中一共展示了JSch类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: RunExample
public static void RunExample(String[] arg)
{
try
{
//Create a new JSch instance
JSch jsch=new JSch();
//Prompt for username and server host
Console.WriteLine("Please enter the user and host info at the popup window...");
String host = InputForm.GetUserInput
("Enter [email protected]",
Environment.UserName+"@localhost");
String user=host.Substring(0, host.IndexOf('@'));
host=host.Substring(host.IndexOf('@')+1);
//Create a new SSH session
Session session=jsch.getSession(user, host, 22);
// username and password will be given via UserInfo interface.
UserInfo ui=new MyUserInfo();
session.setUserInfo(ui);
//Add AES128 as default cipher in the session config store
System.Collections.Hashtable config=new System.Collections.Hashtable();
config.Add("cipher.s2c", "aes128-cbc,3des-cbc");
config.Add("cipher.c2s", "aes128-cbc,3des-cbc");
session.setConfig(config);
//Connect to remote SSH server
session.connect();
//Open a new Shell channel on the SSH session
Channel channel=session.openChannel("shell");
//Redirect standard I/O to the SSH channel
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 remote server
channel.disconnect();
session.disconnect();
}
catch(Exception e)
{
Console.WriteLine(e.Message);
}
}
示例2: Run
public static void Run()
{
JSch jsch=new JSch();
Session session=jsch.getSession("root", "rhmanage", 22);
session.setPassword( "cisco" );
Hashtable config=new Hashtable();
config.Add("StrictHostKeyChecking", "no");
session.setConfig(config);
session.connect();
Channel channel=session.openChannel("exec");
((ChannelExec)channel).setCommand("ifconfig");
StreamReader sr = new StreamReader( channel.getInputStream() );
channel.connect();
string line;
while( (line=sr.ReadLine()) != null )
{
Console.WriteLine( line );
}
channel.disconnect();
session.disconnect();
}
示例3: SshBase
/// <summary>
/// Constructs a new SSH instance
/// </summary>
/// <param name="sftpHost">The remote SSH host</param>
/// <param name="user">The login username</param>
/// <param name="password">The login password</param>
public SshBase(string sftpHost, string user, string password)
{
this.m_host = sftpHost;
this.m_user = user;
this.Password = password;
m_jsch = new JSch();
}
示例4: Connect
public static void Connect()
{
try
{
var jsch = new JSch();
_session = jsch.getSession(Settings.SSHUsername, Settings.SSHHost, Settings.SSHPort);
_session.setHost(Settings.SSHHost);
_session.setPassword(Settings.SSHPassword);
UserInfo ui = new MyUserInfo(Settings.SSHPassword);
_session.setUserInfo(ui);
_session.connect();
int port;
if (!int.TryParse(Settings.Port, out port))
port = 3306;
_session.setPortForwardingL(Settings.SSHLocalPort, "localhost", port);
if (!_session.isConnected())
Enabled = false;
}
catch (Exception ex)
{
Enabled = false;
Trace.WriteLine(ex.Message + " at ssh connect.");
Disconnect();
}
}
示例5: Connect
public void Connect()
{
InitVAHInfo();
try
{
JSch jsch = new JSch();
_ssn = jsch.getSession(_usr, _hip, _hp);
System.Collections.Hashtable hashConfig = new Hashtable();
hashConfig.Add("StrictHostKeyChecking", "No");
_ssn.setConfig(hashConfig);
jsch.addIdentity(_ppk);
_ssn.connect();
if (_ssn.isConnected())
{
Console.WriteLine("Log Successfully.");
}
else
{
Console.WriteLine("Log failed.");
}
}
catch (Tamir.SharpSsh.jsch.JSchException jschex)
{
Console.WriteLine(jschex.Message);
}
catch (Exception anyex)
{
Console.WriteLine(anyex.Message);
}
}
示例6: SshHelper
public SshHelper(string host, string username, string password)
{
this.host = host;
JSch jsch=new JSch();
Session session=jsch.getSession(username, host, 22);
session.setPassword( password );
Hashtable config=new Hashtable();
config.Add("StrictHostKeyChecking", "no");
session.setConfig(config);
session.connect();
channel=(ChannelShell)session.openChannel("shell");
writer_po = new PipedOutputStream();
PipedInputStream writer_pi = new PipedInputStream( writer_po );
PipedInputStream reader_pi = new PipedInputStream();
PipedOutputStream reader_po = new PipedOutputStream( reader_pi );
reader = new StreamReader (reader_pi,Encoding.UTF8);
channel.setInputStream( writer_pi );
channel.setOutputStream( reader_po );
channel.connect();
channel.setPtySize(132, 132, 1024, 768);
}
示例7: SshHelper
public SshHelper(string Host, string UserName, string Password)
{
host = Host;
var jsch = new JSch();
session = jsch.getSession(UserName, host, 22);
session.setPassword(Password);
var config = new Hashtable { { "StrictHostKeyChecking", "no" } };
session.setConfig(config);
session.connect();
channel = (ChannelShell)session.openChannel("shell");
writer_po = new PipedOutputStream();
var writer_pi = new PipedInputStream(writer_po);
var reader_pi = new PipedInputStream();
var reader_po = new PipedOutputStream(reader_pi);
reader = new StreamReader(reader_pi, Encoding.UTF8);
channel.setInputStream(writer_pi);
channel.setOutputStream(reader_po);
channel.connect();
channel.setPtySize(132, 132, 1024, 768);
}
示例8: getFingerPrint
public String getFingerPrint(JSch jsch){
HASH hash=null;
try{
hash=(HASH)Activator.CreateInstance(Type.GetType(jsch.getConfig("md5")));
}
catch(Exception e){ Console.Error.WriteLine("getFingerPrint: "+e); }
return Util.getFingerPrint(hash, key);
}
示例9: RunExample
public static void RunExample(String[] arg)
{
try
{
JSch jsch=new JSch();
//Get the "known hosts" filename from the user
Console.WriteLine("Please choose your private key file...");
String file = InputForm.GetFileFromUser("Choose your privatekey(ex. ~/.ssh/id_dsa)");
Console.WriteLine("You chose "+file+".");
//Add the identity file to JSch
jsch.addIdentity(file);
//Prompt for username and server host
Console.WriteLine("Please enter the user and host info at the popup window...");
String host = InputForm.GetUserInput
("Enter [email protected]",
Environment.UserName+"@localhost");
String user=host.Substring(0, host.IndexOf('@'));
host=host.Substring(host.IndexOf('@')+1);
//Create a new SSH session
Session session=jsch.getSession(user, host, 22);
// username and password will be given via UserInfo interface.
UserInfo ui=new MyUserInfo();
session.setUserInfo(ui);
//Connect to remote SSH server
session.connect();
//Open a new Shell channel on the SSH session
Channel channel=session.openChannel("shell");
//Redirect standard I/O to the SSH channel
channel.setInputStream(Console.OpenStandardInput());
channel.setOutputStream(Console.OpenStandardOutput());
//Connect the channel
channel.connect();
//Wait till channel is closed
while(!channel.isClosed())
{
System.Threading.Thread.Sleep(500);
}
//Disconnect from remote server
channel.disconnect();
session.disconnect();
}
catch(Exception e)
{
Console.WriteLine(e);
}
}
示例10: RunExample
public static void RunExample(params string[] arg)
{
if(arg.Length<3)
{
Console.Error.WriteLine(
"usage: java KeyGen rsa output_keyfile comment\n"+
" java KeyGen dsa output_keyfile comment");
return;
}
try
{
//Get sig type ('rsa' or 'dsa')
String _type=arg[0];
int type=0;
if(_type.Equals("rsa")){type=KeyPair.RSA;}
else if(_type.Equals("dsa")){type=KeyPair.DSA;}
else
{
Console.Error.WriteLine(
"usage: java KeyGen rsa output_keyfile comment\n"+
" java KeyGen dsa output_keyfile comment");
return;
}
//Output file name
String filename=arg[1];
//Signature comment
String comment=arg[2];
//Create a new JSch instance
JSch jsch=new JSch();
//Prompt the user for a passphrase for the private key file
String passphrase=InputForm.GetUserInput("Enter passphrase (empty for no passphrase)", true);
//Generate the new key pair
KeyPair kpair=KeyPair.genKeyPair(jsch, type);
//Set a passphrase
kpair.setPassphrase(passphrase);
//Write the private key to "filename"
kpair.writePrivateKey(filename);
//Write the public key to "filename.pub"
kpair.writePublicKey(filename+".pub", comment);
//Print the key fingerprint
Console.WriteLine("Finger print: "+kpair.getFingerPrint());
//Free resources
kpair.dispose();
}
catch(Exception e)
{
Console.WriteLine(e);
}
return;
}
示例11: genKeyPair
public static KeyPair genKeyPair(JSch jsch, int type, int key_size)
{
KeyPair kpair=null;
if(type==DSA){ kpair=new KeyPairDSA(jsch); }
else if(type==RSA){ kpair=new KeyPairRSA(jsch); }
if(kpair!=null)
{
kpair.generate(key_size);
}
return kpair;
}
示例12: SftpHelper
/// <summary>
/// 构造方法
/// </summary>
/// <param name="host"></param>
/// <param name="user"></param>
/// <param name="pwd"></param>
public SftpHelper(string host, string user, string pwd)
{
string[] arr = host.Split(':');
string ip = arr[0];
int port = 22;
if (arr.Length > 1) port = Int32.Parse(arr[1]);
JSch jsch = new JSch();
m_session = jsch.getSession(user, ip, port);
MyUserInfo ui = new MyUserInfo();
ui.setPassword(pwd);
m_session.setUserInfo(ui);
}
示例13: RunExample
public static void RunExample(String[] arg)
{
if(arg.Length<3)
{
Console.Error.WriteLine(
"usage: java KeyGen rsa output_keyfile comment\n"+
" java KeyGen dsa output_keyfile comment");
Environment.Exit(-1);
}
String _type=arg[0];
int type=0;
if(_type.Equals("rsa")){type=KeyPair.RSA;}
else if(_type.Equals("dsa")){type=KeyPair.DSA;}
else
{
Console.Error.WriteLine(
"usage: java KeyGen rsa output_keyfile comment\n"+
" java KeyGen dsa output_keyfile comment");
Environment.Exit(-1);
}
String filename=arg[1];
String comment=arg[2];
JSch jsch=new JSch();
String passphrase="";
InputForm passphraseField=new InputForm();
passphraseField.PasswordField = true;
Object[] ob={passphraseField};
passphraseField.Text="Enter passphrase (empty for no passphrase)";
bool result=passphraseField.PromptForInput();
if(result)
{
passphrase=passphraseField.getText();
}
try
{
KeyPair kpair=KeyPair.genKeyPair(jsch, type);
kpair.setPassphrase(passphrase);
kpair.writePrivateKey(filename);
kpair.writePublicKey(filename+".pub", comment);
Console.WriteLine("Finger print: "+kpair.getFingerPrint());
kpair.dispose();
}
catch(Exception e)
{
Console.WriteLine(e);
}
Environment.Exit(0);
}
示例14: RunExample
public static void RunExample(String[] arg)
{
int port;
try
{
//Create a new JSch instance
JSch jsch=new JSch();
//Prompt for username and server host
Console.WriteLine("Please enter the user and host info at the popup window...");
String host = InputForm.GetUserInput
("Enter [email protected]",
Environment.UserName+"@localhost");
String user=host.Substring(0, host.IndexOf('@'));
host=host.Substring(host.IndexOf('@')+1);
//Create a new SSH session
Session session=jsch.getSession(user, host, 22);
// username and password will be given via UserInfo interface.
UserInfo ui=new MyUserInfo();
session.setUserInfo(ui);
session.connect();
//Get from user the remote host and remote host port
String foo = InputForm.GetUserInput("Enter host and port", "host:port");
host=foo.Substring(0, foo.IndexOf(':'));
port=int.Parse(foo.Substring(foo.IndexOf(':')+1));
Console.WriteLine("System.{in,out} will be forwarded to "+
host+":"+port+".");
Channel channel=session.openChannel("direct-tcpip");
((ChannelDirectTCPIP)channel).setInputStream(Console.OpenStandardInput());
((ChannelDirectTCPIP)channel).setOutputStream(Console.OpenStandardOutput());
((ChannelDirectTCPIP)channel).setHost(host);
((ChannelDirectTCPIP)channel).setPort(port);
channel.connect();
while(!channel.isClosed())
{
System.Threading.Thread.Sleep(500);
}
channel.disconnect();
session.disconnect();
}
catch(Exception e)
{
Console.WriteLine(e);
}
}
示例15: RunExample
public static void RunExample(String[] arg)
{
//Get the private key filename from the user
Console.WriteLine("Please choose your private key file...");
String pkey = InputForm.GetFileFromUser("Choose your privatekey(ex. ~/.ssh/id_dsa)");
Console.WriteLine("You chose "+pkey+".");
//Create a new JSch instance
JSch jsch=new JSch();
try
{
//Load the key pair
KeyPair kpair=KeyPair.load(jsch, pkey);
//Print the key file encryption status
Console.WriteLine(pkey+" has "+(kpair.isEncrypted()?"been ":"not been ")+"encrypted");
String passphrase = "";
while(kpair.isEncrypted())
{
passphrase = InputForm.GetUserInput("Enter passphrase for "+pkey, true);
if(!kpair.decrypt(passphrase))
{
Console.WriteLine("failed to decrypt "+pkey);
}
else
{
Console.WriteLine(pkey+" is decrypted.");
}
}
passphrase="";
passphrase=InputForm.GetUserInput("Enter new passphrase for "+pkey+
" (empty for no passphrase)", true);
//Set the new passphrase
kpair.setPassphrase(passphrase);
//write the key to file
kpair.writePrivateKey(pkey);
//free the resource
kpair.dispose();
}
catch(Exception e)
{
Console.WriteLine(e.Message);
}
}