The following video discusses phone authentication in general and how to use within JSCAPE Secure FTP Server.
http://www.jscape.com/secureftpserver/videos/phone_authentication/phone_authentication.html
The following video discusses phone authentication in general and how to use within JSCAPE Secure FTP Server.
http://www.jscape.com/secureftpserver/videos/phone_authentication/phone_authentication.html
Posted at 11:24 AM in Tutorials | Permalink | Comments (0) | TrackBack (0)
Technorati Tags: ftp authentication, ftp two-factor, phone authentication
This video demonstrates how to detect and handle brute force password attacks in JSCAPE Secure FTP Server.
Posted at 10:44 AM in Tutorials | Permalink | Comments (0) | TrackBack (0)
Technorati Tags: brute force password attack, secure ftp server
This video demonstrates how to create a domain in JSCAPE Secure FTP Server.
http://www.jscape.com/secureftpserver/videos/creating_domain/creating_domain.html
Posted at 12:33 PM in Tutorials | Permalink | Comments (0) | TrackBack (0)
Technorati Tags: ftps server, secure ftp server, sftp server
Overview
The purpose of this article is to demonstrate public-key authentication in SSH/SFTP. SSH uses public-key cryptography to allow the remote computer to authenticate the user. SSH is typically used to log into a remote machine and to execute commands (e.g. logging into a remote unix machine). It can also be used to transfer files using the SFTP protocol. An SSH client program is typically used for establishing connections to an SSH server accepting remote connections (e.g. AnyClient).
Key Authentication
First, a pair of cryptographic keys is generated. The first of these keys is the public key and this resides on the remote server and is used by SSH to authenticate users who have the correct matching private key. The private key resides with the user and can be protected by a passphrase. When a user connects to the remote server both the private key and the passphrase are required in addition to the username/password combination. An SSH server, by default, listens on the standard TCP port 22.
In the following demonstration we are going to use Jscape Secure FTP Server as the remote server and AnyClient as the local client. This is laid in a series of steps :
Step 1 (Generating a client key)
Go to 'File -> Key Manager'. This will bring up the following screen :
Click on the 'Generate' button. This will bring up the following screen :
'Alias' is simply a name given to the generated key. These names can then be used to assign keys to
specific user(s). (The tutorial will show how to do this at a later stage)
'Period' - The number of days this key is valid.
'Common Name' - The name you wish to assign this key. For a client key this is typically the full name of the user e.g. John Smith.
'Organizational Unit' - The unit within the users organization that this key will be used for e.g. IT.
'Organization' - The users organization name.
'Locality' - The users city.
'State/Province' - The users state or province.
'Country code' - The users 2 character country code e.g. US.
Press the 'Ok' button after you have provided all of the above fields. This will bring up the following screen :
'Private key file' - The file you wish to export the private key to. This should be a valid 'PEM' file (e.g. license.pem). SSH authentication at this time only supports 'PEM' file types.
'File password' - The password used to protect private key. Leave blank for no password.
'Certificate file' - The file you wish to export certificate to. (This is not a required step)
'File type' - The format in which you wish to export certificate.
'Public key file' - The file you wish to export public key to.
'File type' - The format in which you wish to export public key.
When the 'Ok' button is pressed the license file is generated and saved to the specified location. However at this stage it cannot be used to connect from a client program. It needs to be assigned to user account(s) on the server. The article will demonstrate how to do this next :
Step 2 (Assigning generated keys to a user account)
Go to the 'Users' node under the server node. This will display a 'Users' tab displaying the users for this account. Select the account you want a assign a authentication key to and click the 'Edit' button. This will bring up the following screen :
Select the 'Info' tab as shown above. As you can see in the 'Client keys' section there are two keys available. I created these keys when writing this tutorial. You can select as many keys as you want to assign to the user. Remember that each key will have a separate valid PEM file that resides with the client (the user that connects to the remote server). Click the 'Ok' button to finish and save the assignment of keys.
This finishes the creation and assignment of authentication keys. Next we will show how to actually use this authentication key from a client program (AnyClient).
Start AnyClient on your PC. Go to 'File -> Site Manager'. This will bring up the following screen :
We will need a site to connect to the remote server. Click on the 'New' button on the screen above. This will bring you to the following screen :
I created a site called 'TestSite' with the settings shown.
'Host' - the name of the remote server. Port 22 is used for SSH connections.
'Username' - username of the account on the server.
'Password' - password of the account on the server.
'Connection Type' - Set this to 'SFTP/SSH' for the purposes of this tuorial. This will enable extra authentication options that must be provided before a successful connection can be made :
-) 'Private Key File' - this is the private key file generated from Step 1 above.
-) 'Key Password' - this is the private key file passphrase set in Step 1 above.
-) 'Use public key authentication' - Select this checkbox to use public key authentication.
-) 'Use password authentication' - Select this checkbox to use password enabled authentication.
The last two of the above options depend on the type of authentication the server expects for SFTP/SSH connections. This can be controlled by performing the following steps :
1) Select 'Services' node under Jscape Secure FTP Server.
2) Select SFTP service under the Services tab.
3) Click the 'Edit' button.
This will bring up the following screen :
Note that I used 'password AND publickey' option for the 'Authentication' category. Hence I need to select both 'Use public key authentication' and 'Use password authentication' checkboxes on the AnyClient connect option screen (previous screenshot). Click 'Connect' on the AnyClient screen, provided you have completed all steps correctly and provided all the right credentials AnyClient will forward you to it's file transfer interface screen.
Posted at 07:59 AM in Articles, Tutorials | Permalink | Comments (1) | TrackBack (0)
Overview
This article will demonstrate how to format Mime Messages using Email Factory for .NET.
Code Example
Download licenseEmail.txt
| Download MMessage.cs
| Download MMessage.vb
1: using System;
2: using System.Collections.Generic;
3: using System.Collections;
4: using System.Text;
5: using Jscape.Email;
6: using System.IO;
7:
8: namespace MMessageNet
9: {
10: class MMessage
11: {
12: MimeMessage message = null;
13:
14: public MMessage()
15: {
16: message = new MimeMessage();
17: }
18:
19: public MMessage(string filename)
20: {
21: FileStream fs = new FileStream(filename, FileMode.Open, FileAccess.Read);
22: BinaryReader r = new BinaryReader(fs);
23: byte[] data = r.ReadBytes((int)fs.Length);
24: message = new MimeMessage(data);
25: fs.Close();
26: r.Close();
27: fs.Dispose();
28: }
29:
30: public void addHeader(string name, string value)
31: {
32: message.AddHeader(new MimeHeader(name, value));
33: }
34:
35: public string getHeaderValue(string name)
36: {
37: return message.GetHeaderValue(name);
38: }
39:
40: public void setBody(string body)
41: {
42: message.SetBody(body);
43: }
44:
45: public string getBody()
46: {
47: return message.GetBody();
48: }
49:
50: public void addPart(string filename)
51: {
52: byte[] data = null;
53: StreamReader sr = File.OpenText(filename);
54: data = Encoding.Default.GetBytes(sr.ReadToEnd());
55: message.AddPart(new MimeMessage(data));
56: sr.Close();
57: sr.Dispose();
58: }
59:
60: public MimeMessage getPart(int index)
61: {
62: return message.GetPart(index);
63: }
64:
65: static void Main(string[] args)
66: {
67: try
68: {
69: MMessage m = new MMessage();
70: m.addHeader("X-Mailer", "Microsoft Outlook");
71: Console.WriteLine(m.getHeaderValue("X-Mailer"));
72: m.setBody("Go over status of project.\r\nThanks\r\nBob");
73: Console.WriteLine(m.getBody());
74: }
75: catch (Exception e)
76: {
77: Console.WriteLine(e.Message);
78: }
79:
80: Console.WriteLine("Press any key to exit ... ");
81: Console.Read();
82: }
83: }
84: }
Lines 1-6 : Necessary import statements.
Line 16 : Initialize MimeMessage instance.
Lines 19-28 : Initialize MimeMessage instance from filename.
Lines 30-33 : Add MimeMessage header.
Lines 35-38 : Get MimeMessage header value.
Lines 40-43 : Set MimeMessage body.
Lines 45-48 : Get MimeMessage body.
Lines 50-58 : Add message part to existing MimeMessage.
Lines 60-63 : Get MimeMessage part by index.
Line 69 : Initialize MMessage instance.
Posted at 04:31 PM in .NET, Tutorials | Permalink | Comments (0) | TrackBack (0)
Overview
This article will demonstrate how to communicate with a IMAP4 server using Email Factory for .NET.
Code Example
Download licenseEmail.txt
| Download IMAPEx.cs
| Download IMapEx.vb
1: using System;
2: using System.Collections.Generic;
3: using System.Collections;
4: using System.Text;
5: using Jscape.Email;
6:
7: namespace IMAPNET
8: {
9: class IMAPEx
10: {
11: Imap imap = null;
12: public IMAPEx(string server, string username, string password)
13: {
14: imap = new Imap(server, username, password);
15: imap.LicenseKey = null; // copy license from license.txt
16: imap.DeleteMessages = true;
17: imap.ConnectedEvent +=
18: new Imap.ConnectedEventHandler(imap_ConnectedEvent);
19: imap.DisconnectedEvent +=
20: new Imap.DisconnectedEventHandler(imap_DisconnectedEvent);
21: }
22:
23: private void imap_DisconnectedEvent(object sender, ImapDisconnectedEventArgs e)
24: {
25: Console.WriteLine("Disconnected from : " + e.Host);
26: }
27:
28: private void imap_ConnectedEvent(object sender, ImapConnectedEventArgs e)
29: {
30: Console.WriteLine("Connected to : " + e.Host);
31: }
32:
33: public void openConnection()
34: {
35: imap.Connect();
36: }
37:
38: public void closeConnection()
39: {
40: imap.Expunge();
41: imap.Disconnect();
42: }
43:
44: public string issueCommand(string cmd)
45: {
46: return imap.IssueCommand(cmd);
47: }
48:
49: public int getMessageCount()
50: {
51: return imap.GetMessageCount();
52: }
53:
54: public ArrayList getAllMessages()
55: {
56: ArrayList ret = new ArrayList();
57: IEnumerator e = imap.GetMessages();
58: while (e.MoveNext()) ret.Add((EmailMessage)e.Current);
59: return ret;
60: }
61:
62: public ArrayList getNewMessages()
63: {
64: ArrayList ret = new ArrayList();
65: IEnumerator e = imap.GetNewMessages();
66: while (e.MoveNext()) ret.Add((EmailMessage)e.Current);
67: return ret;
68: }
69:
70: public EmailMessage getMessage(int id)
71: {
72: return imap.GetMessage(id);
73: }
74:
75: public void deleteMessage(int id)
76: {
77: imap.DeleteMessage(id);
78: }
79:
80: public ArrayList GetMessagesWithMinimumSize(long size)
81: {
82: ArrayList ret = new ArrayList();
83: IEnumerator e = imap.GetMessages("LARGER " + size.ToString());
84: while (e.MoveNext()) ret.Add((EmailMessage)e.Current);
85: return ret;
86: }
87:
88: public ArrayList GetMessagesWithMaximumSize(long size)
89: {
90: ArrayList ret = new ArrayList();
91: IEnumerator e = imap.GetMessages("SMALLER " + size.ToString());
92: while (e.MoveNext()) ret.Add((EmailMessage)e.Current);
93: return ret;
94: }
95:
96: public void setFlags(int startMessage, int endMessage,
97: int mode, int flag)
98: {
99: imap.Store(startMessage, endMessage, mode, flag);
100: }
101:
102: static void Main(string[] args)
103: {
104: try
105: {
106: string server = "[server]";
107: string username = "[username]";
108: string password = "[password]";
109: IMAPEx ie = new IMAPEx(server, username, password);
110: ie.openConnection();
111: ie.closeConnection();
112: }
113: catch (Exception e)
114: {
115: Console.WriteLine(e.Message);
116: }
117:
118: Console.WriteLine("Press any key to exit ... ");
119: Console.Read();
120: }
121: }
122: }
Lines 1-5 : Necessary import statements.
Lines 11-21 : Initialize Imap instance and connect event handlers.
Lines 23-31 : Event handlers for connect / disconnect events.
Lines 33-42 : Methods to open/close connection to the server.
Lines 44-47 : Issue a command to the server.
Lines 49-52 : Get message count.
Lines 54-60 : Get all messages from the server.
Lines 62-68 : Get new messages from the server.
Lines 70-73 : Get message with a particular id.
Lines 75-78 : Delete message with a particular id.
Lines 80-86 : Get messages with minimum size.
Lines 88-94 : Get messages with maximum size.
Lines 96-100 : Set flags for a range of messages.
Line 109 : Initialize ImapEx instance.
Posted at 03:54 PM in .NET, Tutorials | Permalink | Comments (0) | TrackBack (0)
Technorati Tags: .NET, C#, imap, VB.NET
Overview
This article will demonstrate how to communicate with a POP3 server using Email Factory for .NET.
Code Example
Download licenseEmail.txt
| Download PopEx.cs | Download PopEx.vb
1: using System;
2: using System.Collections.Generic;
3: using System.Collections;
4: using System.Text;
5: using Jscape.Email;
6:
7: namespace POPNET
8: {
9: class PopEx
10: {
11: Pop pop = null;
12: public delegate void OnPopExEventHandler(string value);
13: public event OnPopExEventHandler OnPopEvent;
14:
15: public PopEx(string server, string username, string password)
16: {
17: pop = new Pop(server, username, password);
18: pop.LicenseKey = null; // copy license from license.txt here
19: pop.DeleteMessages = true;
20: pop.ConnectedEvent
21: += new Pop.ConnectedEventHandler(pop_ConnectedEvent);
22: pop.DisconnectedEvent
23: += new Pop.DisconnectedEventHandler(pop_DisconnectedEvent);
24: pop.DataReceivedEvent
25: += new Pop.DataReceivedEventHandler(pop_DataReceivedEvent);
26: }
27:
28: private void pop_DataReceivedEvent(object sender, PopDataReceivedEventArgs e)
29: {
30: OnPopEvent(e.Response);
31: }
32:
33: private void pop_DisconnectedEvent(object sender, PopDisconnectedEventArgs e)
34: {
35: Console.WriteLine("Disconnected from : " + e.Host);
36: }
37:
38: private void pop_ConnectedEvent(object sender, PopConnectedEventArgs e)
39: {
40: Console.WriteLine("Connected to : " + e.Host);
41: }
42:
43: public void openConnection()
44: {
45: pop.Connect();
46: }
47:
48: public void closeConnection()
49: {
50: pop.Disconnect();
51: }
52:
53: public void useAPOPAuthentication()
54: {
55: pop.AuthMode = Pop.AUTH_APOP;
56: }
57:
58: public string issuePopCommand(string cmd)
59: {
60: return pop.IssueCommand(cmd);
61: }
62:
63: public int getMessageCount()
64: {
65: return pop.GetMessageCount();
66: }
67:
68: public ArrayList getAllMessages()
69: {
70: ArrayList ret = new ArrayList();
71: IEnumerator e = pop.GetMessages();
72: while (e.MoveNext()) ret.Add((EmailMessage)e.Current);
73: return ret;
74: }
75:
76: public EmailMessage GetId(int id)
77: {
78: return pop.GetMessage(id);
79: }
80:
81: public void DeleteMessage(int id)
82: {
83: pop.DeleteMessage(id);
84: }
85:
86: static void Main(string[] args)
87: {
88: try
89: {
90: string server = "[server]";
91: string username = "[username]";
92: string password = "[password]";
93: PopEx pe = new PopEx(server, username, password);
94: pe.useAPOPAuthentication();
95: pe.openConnection();
96: pe.issuePopCommand("HELO 10.0.0.1");
97: pe.closeConnection();
98: }
99: catch (Exception e)
100: {
101: Console.WriteLine(e.Message);
102: }
103:
104: Console.WriteLine("Press any key to exit ... ");
105: Console.Read();
106: }
107: }
108: }
Lines 1-5 : Necessary import statements.
Lines 11-26 : Initialize Pop instance and connect event handlers.
Lines 28-36 : Methods to catch events from Pop instance.
Lines 43-51 : Methods to connect/disconnect from server.
Lines 53-56 : Use APOP authentication.
Lines 58-61 : Issue a command to the server.
Lines 68-74 : Get all messages from the server.
Lines 76-79 : Get message by id.
Lines 81-84 : Delete message by id.
Line 93 : Initialize PopEx instance.
Posted at 12:11 PM in .NET, Tutorials | Permalink | Comments (0) | TrackBack (0)
Technorati Tags: .NET, C#, POP3, VB.NET
Overview
This article will demonstrate how to communicate with an SMTP server and how to send emails in NET using Email Factory for .NET.
Code Example
Download licenseEmail.txt
| Download SMTPEmailEx.cs
| Download SMTPEmailEx.vb
1: using System;
2: using System.Collections.Generic;
3: using System.Text;
4: using Jscape.Email;
5:
6: namespace SMTPNet
7: {
8: class SMTPEx
9: {
10: Smtp smtp = null;
11: public SMTPEx(string server, string username, string password)
12: {
13: smtp = new Smtp(server);
14: smtp.LicenseKey = null; // copy license from license.txt here
15: smtp.Login(username, password);
16: smtp.ConnectedEvent +=
17: new Smtp.ConnectedEventHandler(smtp_ConnectedEvent);
18: smtp.DisconnectedEvent +=
19: new Smtp.DisconnectedEventHandler(smtp_DisconnectedEvent);
20: }
21:
22: private void smtp_DisconnectedEvent(object sender, SmtpDisconnectedEventArgs e)
23: {
24: Console.WriteLine("Disconnected from : " + e.Host);
25: }
26:
27: private void smtp_ConnectedEvent(object sender, SmtpConnectedEventArgs e)
28: {
29: Console.WriteLine("Connected to : " + e.Host);
30: }
31:
32: public void openConnection()
33: {
34: smtp.Connect();
35: }
36:
37: public void closeConnection()
38: {
39: smtp.Disconnect();
40: }
41:
42: public string IssueCommand(string command)
43: {
44: return smtp.IssueCommand(command);
45: }
46:
47: public void sendMessage(string from, string to, string subject, string body)
48: {
49: EmailMessage message = new EmailMessage(to, from);
50: message.Subject = subject;
51: message.SetBody(body);
52: smtp.Send(message);
53: }
54:
55: public void sendHtmlMessage(string to, string from, string subject,
56: string textBody, string htmlBody,
57: Dictionary<string, string> resources)
58: {
59: HtmlEmailMessage ret = new HtmlEmailMessage();
60: ret.To = to;
61: ret.From = from;
62: ret.Subject = subject;
63: ret.SetTextBody(textBody);
64: ret.SetHtmlBody(htmlBody);
65: foreach (KeyValuePair<string, string> pair in resources)
66: ret.Embed(pair.Value, pair.Key);
67: smtp.Send(ret);
68: }
69:
70:
71:
72: static void Main(string[] args)
73: {
74: try
75: {
76: string server = "[server]";
77: string username = "[username]";
78: string password = "[password]";
79: SMTPEx e = new SMTPEx(server, username, password);
80: e.openConnection();
81: string response = e.IssueCommand("HELO 10.0.0.1");
82: Dictionary<string, string> dict = new Dictionary<string, string>();
83: dict.Add("imageUrl1", "imageKey1");
84: dict.Add("imageUrl2", "imageKey2");
85: dict.Add("imageUrl3", "imageKey3");
86: e.sendHtmlMessage("to@to.com", "from@from.com", "subject",
87: "testBody", "htmlBody", dict);
88: e.closeConnection();
89: }
90: catch (Exception e)
91: {
92: Console.WriteLine(e.Message);
93: }
94:
95: Console.WriteLine("Press any key to exit ... ");
96: Console.Read();
97: }
98: }
99: }
Lines 1-4 : Necessary import statements.
Lines 10-20 : Initialize Smtp instance and connect event handlers.
Lines 22-30 : Event handlers for connected/disconnected events.
Lines 32-40 : Methods for opening and closing connections.
Lines 42-45 : Issue a SMTP command to the server.
Lines 47-53 : Send an email message.
Lines 55-68 : Send an html message.
Line 79 : Initialize SMTPEx instance.
Line 81 : Issue a command to the server.
Lines 82-85 : Create a dictionary instance.
Lines 86-87 : Send an html message.
Posted at 11:13 AM in .NET, Tutorials | Permalink | Comments (0) | TrackBack (0)
Technorati Tags: C#, email, smtp, VB.NET
Overview
This article will demonstrate how to securely transfer files over FTP using Secure FTP Factory for .NET
Code Example
Download licenseSecureFTP.txt
| Download SecureFTP.cs
| Download SecureFTPEx.vb
1: using System;
2: using System.Collections.Generic;
3: using System.Collections;
4: using System.Text;
5: using Jscape.Ftp;
6: using System.IO;
7:
8: namespace SecureFTPNET
9: {
10: class SecureFTP
11: {
12: Ftp ftp = null;
13:
14: public SecureFTP(string hostname, string username, string password)
15: {
16: ftp = new Ftp(hostname, username, password);
17: ftp.LicenseKey = null; // copy license from license.txt here
18: ftp.FtpConnectedEvent +=
19: new FtpConnectEventHandler(ftp_FtpConnectedEvent);
20: ftp.FtpDisconnectedEvent +=
21: new FtpConnectEventHandler(ftp_FtpDisconnectedEvent);
22: }
23:
24: private void ftp_FtpDisconnectedEvent(object sender, FtpConnectEventArgs e)
25: {
26: Console.WriteLine("Connected to : " + e.HostName);
27: }
28:
29: private void ftp_FtpConnectedEvent(object sender, FtpConnectEventArgs e)
30: {
31: Console.WriteLine("Disconnected from : " + e.HostName);
32: }
33:
34: public void setProxy(string proxyHost, int proxyPort, string proxyPassword)
35: {
36: ftp.ProxyHostname = proxyHost;
37: ftp.ProxyPort = proxyPort;
38: ftp.ProxyPassword = proxyPassword;
39: }
40:
41: public void changePassword(string oldPassword, string newPassword)
42: {
43: ftp.ChangePassword(oldPassword, newPassword);
44: }
45:
46: public void setImplicitSSLConnectionType()
47: {
48: this.ftp.ConnectionType = Ftp.IMPLICIT_SSL;
49: }
50:
51: public void setSecureConnectionType()
52: {
53: this.ftp.ConnectionType = Ftp.AUTH_TLS;
54: }
55:
56: public void openConnection()
57: {
58: ftp.Connect();
59: }
60:
61: public void closeConnection()
62: {
63: ftp.Disconnect();
64: }
65:
66: public void enableZipCompression(bool value)
67: {
68: ftp.Compression = value;
69: }
70:
71: public void setPort(int port)
72: {
73: ftp.Port = port;
74: }
75:
76: public void clearCommandChannel()
77: {
78: ftp.ClearCommandChannel();
79: }
80:
81: public void setClientCertificate(string cert)
82: {
83: ftp.SetClientCertificate(cert);
84: }
85:
86: public void setTrustedCertificate(string cert, string pass)
87: {
88: ftp.SetTrustedCertificates(cert, pass);
89: }
90:
91: public void setLocalDir(string dir)
92: {
93: ftp.LocalDir = dir;
94: }
95:
96: public void setRemoteDir(string dir)
97: {
98: ftp.RemoteDir = dir;
99: }
100:
101: // ArrayList items should be cast to FtpFile before use.
102: public ArrayList getLocalDirListing()
103: {
104: return GetArrayListFromEnumerator(ftp.LocalDirListing());
105: }
106:
107: // ArrayList items should be cast to FileInfo before use.
108: public ArrayList getRemoteDirListing()
109: {
110: return GetArrayListFromEnumerator(ftp.GetDirListing());
111: }
112:
113: public ArrayList sortListing(ArrayList lt,
114: bool sortOption,
115: FtpFileSort.Comparator compareOption)
116: {
117: FtpFileSort sort = new FtpFileSort(lt.GetEnumerator());
118: sort.Ascendent = sortOption;
119: return GetArrayListFromEnumerator(sort.Sort(compareOption));
120: }
121:
122: public ArrayList GetArrayListFromEnumerator(IEnumerator enumerator)
123: {
124: ArrayList ret = new ArrayList();
125: while (enumerator.MoveNext()) ret.Add(enumerator.Current);
126: return ret;
127: }
128:
129: public void uploadFile(string source, string dest, bool append,
130: TransferModes mode)
131: {
132: ftp.TransferMode = mode;
133: ftp.Upload(new FileInfo(source), dest, append);
134: }
135:
136: public FileInfo downloadFile(string file, TransferModes modes)
137: {
138: ftp.TransferMode = modes;
139: return ftp.Download(file);
140: }
141:
142: public void downloadDir(string dir)
143: {
144: ftp.DownloadDir(dir);
145: }
146:
147: public void downloadToStream(string file)
148: {
149: Stream inputStream = ftp.GetInputStream(file, 0);
150: Stream outputStream = File.Create(file);
151: byte[] buffer = new byte[8192];
152: int read = 0;
153: while ((read = inputStream.Read(buffer, 0, buffer.Length)) != 0)
154: outputStream.Write(buffer, 0, read);
155: inputStream.Close();
156: outputStream.Close();
157: outputStream.Flush();
158: }
159:
160: public byte[] downloadToMemory(string rFile)
161: {
162: int buffersize = Convert.ToInt32(ftp.GetFileSize(rFile));
163: MemoryStream bout = new MemoryStream(buffersize);
164: ftp.Download(bout, rFile);
165: return bout.GetBuffer();
166: }
167:
168: public FileInfo resumeDownload(string file, TransferModes mode)
169: {
170: ftp.TransferMode = mode;
171: return ftp.ResumeDownload(file);
172: }
173:
174: public void uploadFromMemory(string str, string file)
175: {
176: str += "\r\n";
177: ftp.Upload(Encoding.Default.GetBytes(str), file);
178: }
179:
180: public void makeDir(string dir)
181: {
182: ftp.MakeDir(dir);
183: }
184:
185: public void deleteDir(string dir, bool recursive)
186: {
187: ftp.DeleteDir(dir, recursive);
188: }
189:
190: public void renameFile(string oldname, string newname)
191: {
192: ftp.RenameFile(oldname, newname);
193: }
194:
195: public void deleteFile(string file)
196: {
197: ftp.DeleteFile(file);
198: }
199:
200: public long getFileSize(string name)
201: {
202: return ftp.GetFileSize(name);
203: }
204:
205: public DateTime getTimeStamp(string filename)
206: {
207: return ftp.GetFileTimeStamp(filename);
208: }
209:
210: public void uploadFromStream(string file)
211: {
212: FileInfo fileInfo = new FileInfo(file);
213: Stream inputStream = fileInfo.OpenRead();
214: Stream outputStream = ftp.GetOutputStream(fileInfo.Name, false, 0);
215: byte[] buffer = new byte[8192];
216: int read = 0;
217: while((read = inputStream.Read(buffer, 0, buffer.Length)) != 0)
218: outputStream.Write(buffer, 0, read);
219: inputStream.Close();
220: outputStream.Close();
221: outputStream.Flush();
222: }
223:
224: static void Main(string[] args)
225: {
226: try
227: {
228: string hostname = "[hostname]";
229: string username = "[username]";
230: string password = "[password]";
231: SecureFTP sFTP = new SecureFTP(hostname, username, password);
232: sFTP.openConnection();
233: sFTP.closeConnection();
234: }
235: catch (Exception e)
236: {
237: Console.WriteLine(e.Message);
238: }
239:
240: Console.WriteLine("Press any key to continue ... ");
241: Console.Read();
242: }
243: }
244: }
Lines 1-6 : Necessary import statements.
Lines 14-22 : Initialize Ftp instance and connect event handlers.
Lines 24-32 : Event handlers for connect/disconnect events.
Lines 34-39 : Use this method if a proxy is going to be used.
Lines 41-44 : Method to change password.
Lines 46-49 : Use Implict SSL to connect to the server.
Lines 51-54 : Use Exlpicit SSL to connect to the server.
Lines 56-64 : Methods to open/close connection to the server.
Lines 66-69 : Enable zip compression when performing file transfers or directory listings.
Lines 71-74 : Set connection port.
Lines 76-79 : Clear the command channel.
Lines 81-84 : Provide a client certificate.
Lines 86-89 : Provide a trusted server certificate.
Lines 91-94 : Set current local directory.
Lines 96-99 : Set the remote directory.
Lines 102-105 : Get local directory listing.
Lines 108-111 : Get remote directory listing.
Lines 113-120 : Sort a given directory listing.
Lines 129-134 : Upload a file.
Lines 136-140 : Download a file.
Lines 142-145 : Download a directory.
Lines 147-158 : Download a file to a Stream instance.
Lines 160-166 : Download file as a byte array.
Lines 168-172 : Resume download of a remote file.
Lines 174-178 : Upload from memory.
Lines 180-183 : Create a remote directory.
Lines 185-188 : Delete a remote directory.
Lines 190-193 : Rename a remote file.
Lines 195-198 : Delete a remote file.
Lines 200-203 : Get the size of a remote file.
Lines 205-108 : Get the timestamp of a remote file.
Lines 210-222 : Upload a file from a Stream.
Line 231 : Initialize SecureFTP instance.
Posted at 01:13 PM in .NET, Tutorials | Permalink | Comments (0) | TrackBack (0)
Overview
This article will demonstrate how to create TCP/IP connections through an SSH server using SSH Factory for .NET.
Code Example
Download license.txt | Download IpClientSshEx.cs
| Download IpClientSshEx.vb
1: using System;
2: using System.Collections.Generic;
3: using System.Text;
4: using Jscape.Ssh;
5: using System.IO;
6:
7: namespace IPCLIENTSSH
8: {
9: class IpClientSshEx
10: {
11: IpClientSsh ssh = null;
12:
13: public IpClientSshEx(string hostname, string username, string password,
14: string pophostname, int popport, bool usepublicKeyAuth,
15: string publicKeyPass, string keyFile)
16: {
17: SshParameters sp = new SshParameters(hostname, username, password);
18: if (usepublicKeyAuth == true)
19: {
20: sp.SetPrivateKey(new System.IO.FileInfo(keyFile), publicKeyPass);
21: }
22: ssh = new IpClientSsh(sp, pophostname, popport);
23: ssh.IpClientConnectedEvent +=
24: new Jscape.IpClient.IpClient.IpClientConnectedEventHandler(ConnectedEvent);
25: ssh.IpClientDisconnectedEvent +=
26: new Jscape.IpClient.IpClient.IpClientDisconnectedEventHandler(DisconnectedEvent);
27: }
28:
29: private void DisconnectedEvent(object sender,
30: Jscape.IpClient.IpClientDisconnectedEventArgs args)
31: {
32: Console.WriteLine("Disconnected from : " + args.Hostname);
33: }
34:
35: private void ConnectedEvent(object sender,
36: Jscape.IpClient.IpClientConnectedEventArgs args)
37: {
38: Console.WriteLine("Connected to : " + args.Hostname);
39: }
40:
41: public void openConnection()
42: {
43: ssh.Connect();
44: }
45:
46: public void closeConnection()
47: {
48: ssh.Disconnect();
49: }
50:
51: public string readData()
52: {
53: StreamReader reader = new StreamReader(ssh.Stream);
54: StringBuilder builder = new StringBuilder();
55: string line = null;
56: while ((line = reader.ReadLine()) != null)
57: builder.Append(line);
58: return builder.ToString();
59: }
60:
61: public void writeData(string line)
62: {
63: StreamWriter writer = new StreamWriter(ssh.Stream);
64: writer.WriteLine(line);
65: writer.Flush();
66: }
67:
68: static void Main(string[] args)
69: {
70: try
71: {
72: IpClientSshEx es = new IpClientSshEx("hostname",
73: "username",
74: "password",
75: "pophost",
76: -1, false,
77: "keypass", "keyfile");
78: es.openConnection();
79: es.closeConnection();
80: }
81: catch (Exception e)
82: {
83: Console.WriteLine(e.Message);
84: }
85:
86: Console.WriteLine("Press any key to exit ... ");
87: Console.Read();
88: }
89: }
90: }
Lines 1-5 : Necessary import statements.
Lines 13-27 : Initialize SshParameters instance.
Initialize Ssh instance and connect event handlers.
Lines 29-39 : Event handlers for connect / disconnect events.
Lines 41-49 : Methods to connect / disconnect from the server.
Lines 51-59 : Read data from the server.
Lines 61-66 : Write data to the server.
Line 72 : Initialize IpClientSshEx instance.
Posted at 10:45 PM in .NET, Tutorials | Permalink | Comments (0) | TrackBack (0)