例子1: <?xml version= "1.0 "?> <project name= "buildssh " default= "DEFAULT " basedir= ". "> <target name= "init "> <!-- set properties, mkdir, etc. --> <property file= "build.properties " /> <property name= "this.project " value= "buildssh " /> <echo message= "init in ${this.project} " /> <tstamp /> </target>
<target name= "DEFAULT " depends= "init "> <echo message= "connecting to ${build.server} " /> <sshexec host= "Linux server IP address " username= "Linux server username " password= "Linux server password " trust= "true " command= "Command you want to run on the server " /> </target> </project> 例子2: import com.jcraft.jsch.Channel; import com.jcraft.jsch.ChannelSftp; import com.jcraft.jsch.JSch; import com.jcraft.jsch.Session; import com.jcraft.jsch.UserInfo; public class ExecSCP { public static final UserInfo defaultUserInfo = new UserInfo(){ public String getPassphrase() { return null; } public String getPassword() { return null; } public boolean promptPassword(String arg0) { return false; } public boolean promptPassphrase(String arg0) { return false; } public boolean promptYesNo(String arg0) { return true; } public void showMessage(String arg0) { } }; /** * @param args */ public static void main(String[] args) throws Exception{ String hostname = "www.mozat.com "; String username = "wiimii "; String password = "jtev000 "; String remoteFile = "Setup.ini "; String localFile = "C:\\ ";
JSch jsch=new JSch();
Session session=jsch.getSession(username, hostname, 990); session.setPassword(password); session.setUserInfo(defaultUserInfo); session.connect();
Channel channel=session.openChannel( "sftp "); channel.connect(); ChannelSftp c=(ChannelSftp)channel; c.get(remoteFile, localFile); session.disconnect(); } } |