J2SSH Maverick
Version 1.2.6
一段连接sftp服务器,上传,下载的代码:import com.maverick.ssh.*;
import com.maverick.ssh1.Ssh1Client;
import com.maverick.ssh2.*;
import java.io.*;
import com.sshtools.net.*;
import com.sshtools.sftp.*;
import com.maverick.sftp.*;
import com.sshtools.publickey.ConsoleKnownHostsKeyVerification;
/**
* This example demonstrates the connection process connecting to an SSH2 server and
* usage of the SFTP client.
*
* @author Lee David Painter
* @version $Id: SftpConnect.java,v 1.8 2006/01/30 17:46:04 lee Exp $
*
*/
public class SftpConnect {
public static void main(String[] args) {
final BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String str = "user.home";
try {
System.out.print("Hostname: ");
// String hostname = reader.readLine();
String hostname = "192.168.1.237";
int idx = hostname.indexOf(':');
int port = 22;
if(idx > -1) {
port = Integer.parseInt(hostname.substring(idx+1));
hostname = hostname.substring(0, idx);
}
System.out.print("Username [Enter for " + System.getProperty("user.name") + "]: ");
//String username = reader.readLine();
String username = "linuxborder";
if(username==null || username.trim().equals(""))
username = System.getProperty("user.name");
System.out.println("Connecting to " + hostname);
/**
* Create an SshConnector instance
*/
SshConnector con = SshConnector.getInstance();
// Lets do some host key verification
con.getContext(SshConnector.SSH2).setHostKeyVerification(new ConsoleKnownHostsKeyVerification());
// Set the preferred cipher to AES for best transfer performance
Ssh2Context ssh2Context = (Ssh2Context)con.getContext(SshConnector.SSH2);
ssh2Context.setPreferredPublicKey(Ssh2Context.PUBLIC_KEY_SSHDSS);
ssh2Context.supportedCiphers().add("aes128-cbc", com.sshtools.cipher.AES128Cbc.class);
ssh2Context.setPreferredCipherCS("aes128-cbc");
ssh2Context.setPreferredCipherSC("aes128-cbc");
/**
* Connect to the host
*/
SocketTransport t = new SocketTransport(hostname, port);
t.setTcpNoDelay(true);
SshClient ssh = con.connect(t, username);
/**
* Determine the version
*/
if(ssh instanceof Ssh1Client) {
System.out.println(hostname + " is an SSH1 server!! SFTP is not supported");
ssh.disconnect();
System.exit(0);
}
else
System.out.println(hostname + " is an SSH2 server");
Ssh2Client ssh2 = (Ssh2Client)ssh;
/**
* Authenticate the user using password authentication
*/
com.maverick.ssh.PasswordAuthentication pwd = new com.maverick.ssh.PasswordAuthentication();
do {
System.out.print("Password: ");
pwd.setPassword("linuxborder");
// pwd.setPassword(reader.readLine());
}
while(ssh2.authenticate(pwd)!=SshAuthentication.COMPLETE
&& ssh.isConnected());
/**
* Start a session and do basic IO
*/
if(ssh.isAuthenticated()) {
SftpClient sftp = new SftpClient(ssh2);
System.out.println("******************************************");
System.out.println(" sftp.pwd() " +sftp.pwd());
sftp.mkdir("netsky");
System.out.println(" sftp.pwd() " +sftp.getAbsolutePath("netsky"));
sftp.cd("netsky");
/**
* Perform some text mode operations
*/
sftp.setTransferMode(SftpClient.MODE_TEXT);
File textFile = new File(System.getProperty("user.home"), "shining.txt");
FileOutputStream tout = new FileOutputStream(textFile);
// Create a file with \r\n as EOL
for(int i=0;i<5;i++) {
tout.write("All work and no play makes Jack a dull boy\r\n".getBytes());
}
tout.close();
// Tell the client which EOL the remote client is using - note
// that this will be ignored with version 4 of the protocol
sftp.setRemoteEOL(SftpClient.EOL_LF);
// Now put the file, the remote file should end up with all \r\n changed to \n
sftp.put(textFile.getAbsolutePath());
/**
* Now perform some binary operations
*/
sftp.setTransferMode(SftpClient.MODE_BINARY);
/**
* List the contents of the directory
*/
SftpFile[] ls = sftp.ls();
for(int i=0;i<ls.length;i++) {
System.out.println(ls[i].toString());
System.out.println(SftpClient.formatLongname(ls[i]));
}
/**
* Generate a temporary file for uploading/downloading
*/
File f = new File(System.getProperty(str), "sftp-file");
java.util.Random rnd = new java.util.Random();
FileOutputStream out = new FileOutputStream(f);
byte[] buf = new byte[1024];
for(int i=0;i<30;i++) {
rnd.nextBytes(buf);
out.write(buf);
}
out.close();
/**
* Create a directory
*/
sftp.mkdirs("sftp/test-files");
/**
* Change directory
*/
sftp.cd("sftp/test-files");
/**
* Put a file into our new directory
*/
long length = f.length();
System.out.println("Putting file");
long t1 = System.currentTimeMillis();
sftp.put(f.getAbsolutePath());
long t2 = System.currentTimeMillis();
System.out.println("Completed.");
long e = t2-t1;
System.out.println("Took " + String.valueOf(e) + " milliseconds");
float secs, kbs;
if(e >= 1000) {
kbs = ((float)length / 1024) / ((float)e / 1000);
System.out.println("Upload Transfered at " + String.valueOf(kbs) +
" kbs");
}
/**
* Get the attributes of the uploaded file
*/
System.out.println("Getting attributes of the remote file");
SftpFileAttributes attrs = sftp.stat(f.getName());
System.out.println(SftpClient.formatLongname(attrs, f.getName()));
/**
* Download the file inot a new location
*/
File f2 = new File(System.getProperty(str), "downloaded");
f2.mkdir();
sftp.lcd(f2.getAbsolutePath());
System.out.println("Getting file");
t1 = System.currentTimeMillis();
sftp.get(f.getName());
t2 = System.currentTimeMillis();
System.out.println("Completed.");
e = t2-t1;
System.out.println("Took " + String.valueOf(e) + " milliseconds");
if(e >= 1000) {
kbs = ((float)length / 1024) / ((float)e / 1000);
System.out.println("Download Transfered at " + String.valueOf(kbs) +
" kbs");
}
/**
* Set the permissions on the file and check they were changed
* they should be -rw-r--r--
*/
sftp.chmod(0644, f.getName());
attrs = sftp.stat(f.getName());
System.out.println(SftpClient.formatLongname(attrs, f.getName()));
System.out.println("******************************************");
System.out.println(" sftp.pwd() " + sftp.pwd());
sftp.quit();
ssh.disconnect();
}
} catch(Throwable th) {
th.printStackTrace();
}
}
}
分享到:
相关推荐
Maverick是原始J2SSH API的继承者,并且包括SSH2客户端的完整且稳定的实现。 该产品最初由原始作者于2003年从头开始构建,到目前为止,只有在获得商业许可的情况下才能使用。 随着第三代SSH API的开发和即将发布,此...
J2SSH Maverick是原始J2SSH API的继承者,并且包括SSH2客户端的完整且稳定的实现。 该产品最初由原始作者于2003年从头开始构建,到目前为止,只有在获得商业许可的情况下才能使用。 随着第三代SSH API的开发和即将...
Maverick .NET是使用C#编写的.NET平台的SSH API。 该项目最初是一个商业API,由于资源限制,于2009年被搁置。 现在,我们已经恢复了该项目,并根据GPLv3许可为开源开发人员发布了SSH2实现。
Maverick是一个轻量而完备的MVC Model 2框架。Maverick的Action称作Controller。Controller只接受一个ControllerContext参数。request,response, servlet config, servelt context等输入信息都包装在...
SSH2 协议的纯 Java 实现,使您能够在开源和商业友好的 LGPL 许可下创建客户端和服务器解决方案。 第三代 API 建立在 Java NIO 框架之上,首次为开发客户端和服务器解决方案提供了统一的框架。 特征 以下是 API 的...
Maverick
该项目现在托管第三代Java SSH API Maverick Synergy。 该API建立在Maverick Legacy商业API的基础上,并在统一的客户端/服务器框架中提供了新的API。 该API在LGPL开源许可证下可供社区使用。 此更新包括ed25519支持...
JEDEC JESD50C:2018 Special Requirements for Maverick Product Eli
JEDEC JESD50B-01:2008 Special Requirements for Maverick Product Elimination and Outlier Management - 完整英文电子版(15页).zip
可在此处找到实验性的dev热装服务器: ://dev.maverick.one/maverick-web在此处可以找到实验性的静态小型生产站点: : 开发构建设置 # install dependencies npm -g install yarn npm -g install @vue/c
maple公式转换成matlab代码小牛 Maverick是用于解决非线性最优控制问题(OCP)的库。 它基于直接的完全配置方法,即将最佳控制问题转换为NLP,然后使用第三方NLP求解器。 它与大多数OCP软件的区别在于可以处理隐式...
et732_logger 在 Dangerous Prototypes Web 平台上运行的代码,该平台解码来自 Maverick ET732 无线温度计的信号并将数据发送到 Web 服务器。 快速而肮脏,使用几乎没有修改的示例文件。 使用 Microchip C30 编译器 ...
完整英文电子版 JEDEC JESD50C:2018 Special Requirements for Maverick Product Elimination and Outlier Management (Maverick 产品排除和异常品管理的特殊要求)。 Maverick 产品排除 (MPE) 和异常品管理标准...
MavericK 这是组成程序的原始文件。 这些文件是根据分发的,这意味着您基本上可以对它们进行任何处理,只要您在软件/源代码的任何副本中包含原始版权和许可声明即可。 Makefile包含在类似Unix的系统(例如Mac和...
EASARS-Maverick-20140401 非常好的字典文件
伊塞斯龙翼Maverick游戏鼠标驱动是小编今天为大家提供的此款驱动是伊塞斯官方发布的龙翼Maverick游戏鼠标最新驱动程序,玩家可以通过驱动对鼠标的按键、呼吸灯等进行设置,让你的游戏体验更加流畅,操作更加的便利,...
Maverick模块具有功耗低、体积小、抗干扰能力强等特点,使其可方便地嵌入掌上电脑、PDA、智能手机等手持移动设备,从而实现这类设备的RFID功能扩展,另外本产品同样适用于台式读写器的设计。 默认支持中国的UHF ...
该存储库包含我为达拉斯德克萨斯大学DSC的Maverick团队所做的工作。 该存储库中包含的所有代码都是我自己的(不包括库)。 我计划暂停该项目一段时间(由于时间紧迫,团队成员都退出了),并可能在夏天重新访问该...
JEDEC JESD50C:2018 Special Requirements for Maverick Product Elimination and Outlier Management - 完整英文电子版(17页).pdf
Maverick是使用python构建的静态博客生成器。 像Hexo和Jekyll一样,它以Markdown( .md )文件作为输入,并输出格式精美,结构合理的网站页面( .html )。 它具有许多内置的有用功能(提要,搜索,站点地图等),...