本文整理汇总了C#中Renci.SshNet.SftpClient.Create方法的典型用法代码示例。如果您正苦于以下问题:C# SftpClient.Create方法的具体用法?C# SftpClient.Create怎么用?C# SftpClient.Create使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Renci.SshNet.SftpClient
的用法示例。
在下文中一共展示了SftpClient.Create方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Execute
private void Execute()
{
var keyPath = textBoxFile.Text;
var host = textBoxHost.Text;
var user = textBoxUser.Text;
var pass = textBoxPass.Text;
Action _execute = () =>
{
try
{
//Read Key
Status("opening key");
FileStream file = File.OpenRead(keyPath);
//Connect to SFTP
Status("sftp connecting");
SftpClient sftp = new SftpClient(host, user, pass);
sftp.Connect();
//users home directory
string homepath = "/home/" + user + "/";
if (user == "root")
{
homepath = "/root/";
}
//Find authorized keys
string authKeys = homepath + ".ssh/authorized_keys";
if (!sftp.Exists(authKeys))
{
Status("creating");
if (!sftp.Exists(homepath + ".ssh"))
sftp.CreateDirectory(homepath + ".ssh");
sftp.Create(authKeys);
}
//Download
Status("downloading");
Stream stream = new MemoryStream();
sftp.DownloadFile(authKeys, stream);
Status("downloaded");
//Read
byte[] buffer = new byte[10240]; //No key should be this large
int length = file.Read(buffer, 0, buffer.Length);
//Validate
String strKey;
if (length < 20)
{
Status("Invalid Key (Length)");
return;
}
if (buffer[0] == (byte) 's' && buffer[1] == (byte) 's' && buffer[2] == (byte) 'h' &&
buffer[3] == (byte) '-' && buffer[4] == (byte) 'r' && buffer[5] == (byte) 's' &&
buffer[6] == (byte) 'a')
{
strKey = Encoding.ASCII.GetString(buffer, 0, length).Trim();
}
else
{
Status("Invalid Key (Format)");
return;
}
stream.Seek(0, SeekOrigin.Begin);
StreamReader reader = new StreamReader(stream);
//Check for key that might already exist
while (!reader.EndOfStream)
{
var line = reader.ReadLine().Trim();
if (line == strKey)
{
Status("key already exists");
return;
}
}
//Check new line
if (stream.Length != 0)
{
stream.Seek(0, SeekOrigin.End);
stream.WriteByte((byte) '\n');
}
else
{
stream.Seek(0, SeekOrigin.End);
}
//Append
Status("appending");
stream.Write(buffer, 0, length);
//Upload
Status("uploading");
stream.Seek(0, SeekOrigin.Begin);
sftp.UploadFile(stream, authKeys);
Status("done");
//.........这里部分代码省略.........