Ana SayfaBlogHakkımdaİletişim

Kod tabanlı SFTP istemci işlemleri

24.05.2009 05:26

 

Geliştirdiğiniz bir uygulamada SFTP işlemlerini yapmanız gerekiyorsa piyasada bunu yapan ücretli bileşenler bulmanız mümkün. Ancak bunu özgür yazılımlar yardımı ile de yapabilirsiniz. Doğal olarak ücretli bileşenler kadar basit ve pratik olmasada en azından temel dosya aktarım işlemlerini gerçekleştirebilirsiniz.

Bunun için PuTTY isimli bir özgür yazılım kullanacağız. PuTTY, Windows için bir telnet ve SSH istemcisidir. [3] Bu yazılım SFTP işlemlerini komut satırından verilen argümanlar ile gerçekleştirmektedir. Bu dökümanda geliştirdiğimiz çözüm için PSCP ve PSFTP isimli iki exe dosyasını kullanacağız. Bu dosyaları indirebileceğiniz adresleri dökümanın sonunda bulunan bağlantılar kısmında bulabilirsiniz.

PSCP.exe  : Dizin listeleme, dosya yükleme ve indirme işlemleri, [4]

PSFTP.exe : Dosya silme işlemi, [5]

için kullanıyor olacağız.

1.1.          Komut Desenleri

 ·         Dizin Listeleme :
pscp.exe -l username -pw password -ls username@sftp.address.com:/

·         Dosya indirme :
pscp.exe -l username -pw password username@sftp.address.com:/remoteFilePath localFilePath

·         Dosya yükleme :
pscp.exe -l username -pw password localFilePath username@sftp.address.com:/remoteFilePath

·         Dosya silme :
psftp.exe username@sftp.address.com -pw password -b batchfile.txt
bactfile.txt content : del remoteFilePath,

1.2.          Örnek Kod Parçaları

1.2.1.     Dizin Listeleme

 string username = "testuser";string password = "testpass";string server = "127.0.0.1"; string commandFilePath = @"C:\pscp.exe";string commandPatern = "-l {0} -pw {1} -ls {0}@{2}:/";string commandLine = string.Format(                                    commandPatern,                                    username,                                    password,                                    server                                    ); Process process = new Process();process.StartInfo.FileName = commandFilePath;process.StartInfo.Arguments = commandLine;process.StartInfo.RedirectStandardOutput = true;process.StartInfo.UseShellExecute = false;process.Start();string standartOutput = process.StandardOutput.ReadToEnd();process.Close(); string[] lines;lines = standartOutput.Split(                            Environment.NewLine.ToCharArray(),                            StringSplitOptions.RemoveEmptyEntries                            );StringBuilder sb = new StringBuilder();foreach(string line in lines){    if(line.Substring(0,1) == "-")    {        string remoteFileName = line.Substring(56);        sb.AppendFormat("{0}\r\n",remoteFileName);    }}MessageBox.Show(sb.ToString()); 

1.2.2.      Dosya İndirme

 string username = "testuser";string password = "testpass";string server = "127.0.0.1";string remoteFilePath = "test.txt";string localFilePath = @"C:\test.txt";  string commandFilePath = @"C:\pscp.exe";string commandPatern = "-l {0} -pw {1} {0}@{2}:/{3} {4}";string commandLine = string.Format(                                    commandPatern,                                    "testuser",                                    "testpass",                                    "127.0.0.1",                                    remoteFilePath,                                    localFilePath                                    ); Process process = new Process();process.StartInfo.FileName = commandFilePath;process.StartInfo.Arguments = commandLine;process.StartInfo.RedirectStandardOutput = true;process.StartInfo.UseShellExecute = false;process.Start();string standartOutput = process.StandardOutput.ReadToEnd();process.Close(); if (standartOutput.Contains("100%") == true){     MessageBox.Show("Dosya başarıyla indirildi.");}else{    MessageBox.Show("Dosya indirme işlemi başarısız.");

}

 

1.2.3.     Dosya Yükleme

     string username = "testuser";    string password = "testpass";    string server = "127.0.0.1";    string localFilePath = @"C:\test.txt";    string remoteFilePath = "test.txt";      string commandFilePath = @"C:\pscp.exe";    string commandPatern = "-l {0} -pw {1} {3} {0}@{2}:/{4}";    string commandLine = string.Format(                                        commandPatern,                                        "testuser",                                        "testpass",                                        "127.0.0.1",                                        localFilePath,                                        remoteFilePath                                        );     Process process = new Process();    process.StartInfo.FileName = commandFilePath;    process.StartInfo.Arguments = commandLine;    process.StartInfo.RedirectStandardOutput = true;    process.StartInfo.UseShellExecute = false;    process.Start();    string standartOutput = process.StandardOutput.ReadToEnd();    process.Close();     if (standartOutput.Contains("100%") == true)    {        MessageBox.Show("Dosya başarıyla yüklendi.");    }    else    {        MessageBox.Show("Dosya yükleme işlemi başarısız.");

    }

1.2.4.     Dosya Silme

     string username = "testuser";    string password = "testpass";    string server = "127.0.0.1";    string localFilePath = @"C:\test.txt";    string remoteFilePath = "test.txt";     string tempFileName = string.Format("temp_{0}", remoteFilePath);    string tempDirectory = @"C:\temp";    string tempFilePath = Path.Combine(tempDirectory, tempFileName);    FileStream fs = new FileStream(tempFilePath, FileMode.Create);    System.IO.StreamWriter sw = new StreamWriter(fs);    sw.WriteLine(string.Format("del {0}", remoteFilePath));    sw.Close();    sw.Dispose();    fs.Close();    fs.Dispose();      string commandFilePath = @"C:\psftp.exe";    string commandPatern = "{0}@{1} -pw {2} -b {3}";    string commandLine = string.Format(                                        commandPatern,                                        username,                                        server,                                        password,                                        tempFilePath                                        );     Process process = new Process();    process.StartInfo.FileName = commandFilePath;    process.StartInfo.Arguments = commandLine;    process.StartInfo.RedirectStandardOutput = true;    process.StartInfo.UseShellExecute = false;    process.Start();    string standartOutput = process.StandardOutput.ReadToEnd();    process.Close();     if (standartOutput.Contains(": OK") == true)    {        MessageBox.Show("Dosya başarıyla silindi.");    }    else    {        MessageBox.Show("Dosya silme işlemi başarısız.");    }     System.IO.File.Delete(tempFilePath);  

 

Dikkat : Şimdiye kadar kullanılan komutlarda –l parametresi yerel kullanıcılar için kullanılmıştır. Domain kullanıcıları için –d parametresi kullanılması gerekmektedir. ( Düzeletme : Kasım Erkan )

 

Referanslar

 

 [1] http://sshwindows.sourceforge.net

[2] http://sshwindows.sourceforge.net/download

[3] http://www.chiark.greenend.org.uk/~sgtatham/putty/

[3] http://the.earth.li/~sgtatham/putty/latest/x86/pscp.exe

[4] http://the.earth.li/~sgtatham/putty/latest/x86/psftp.exe

 

Kaynaklar

 

PuTTY User Manual
(http://www.interix.com/downloads/Configuring_OpenSSH.pdf)

Configuring OpenSSH for passwordless login on the Interix subsystem
( http://the.earth.li/~sgtatham/putty/0.60/htmldoc )

Setting up a SFTP Server on Windows
(http://www.digitalmediaminute.com/article/1487/setting-up-a-sftp-server-on-windows )


 

Bu yazı 317 kere okundu.

Yorumlar

Bahri Kahraman
28
Ara
2009
bu kodutest ettimancak çalışmıyor herhangi bir hatada vermiyor dosya yüklenemedi diyor. nerde hata yapıyo olabilirim

Yorum Ekle

sen dinliyorum ??

Ad Soyad ( kimlik arayışındaki ergenler nick girebilir )

Web Site ( http ile başlayan ! )

E-posta ( spam ve reklam göndermek için )

Yorum ( XSS atack scriptleri eklenecek alan )

Blog yazılarına geri dön