Single command from a java application
Jack W Marks
jack@joatconsulting.com
Tue, 22 Oct 2002 16:35:34 -0400
I am trying to execute a single command on the remote server from within
my Java application. Ultimately I want to scp a file remotely, but at
this point, I am just testing various commands. The code below attempts
a simple `dir`, a remote --> local scp and local --> remote scp. None of
these commands seem to work; the first returns false, the others throw
exceptions. Can anyone point me in the right direction, as I am rather
stuck.
try {
//Make connection
SSH2SimpleClient ssh = new SSH2SimpleClient(sock, rand, user,
"password", auth, props);
//get connection object
SSH2Connection conn = ssh.getConnection();
//Set logging level for debugging
conn.setLog(new Log(Log.LEVEL_DEBUG));
//Try a simple `dir` command on the remote server (NT)
SSH2ConsoleRemote console = new SSH2ConsoleRemote(conn);
System.out.println(console.command("dir"));
console.close();
//Try an SCP command
FileOutputStream fos = null;
try {
fos = new FileOutputStream("d:\\java\\out.txt");
} catch ( FileNotFoundException nfe) {
System.out.println("Oops: " +
ExceptionHandler.StrackTrace2String(nfe));
}
File f = null;
try {
f = new File("d:\\java");
} catch (NullPointerException npe) {
System.out.println("Oops: " +
ExceptionHandler.StrackTrace2String(npe));
}
//Create a new SSHSCP1Client for scp command
SSH2SCP1Client scp = new SSH2SCP1Client(f, conn, fos, true);
try {
//Copy remote file here
scp.scp1().copyToLocal(
"d:\\java\\aCube.txt","d:\\Cubes\\aCube.txt", false);
//copy local file there
scp.scp1().copyToRemote(
"d:\\java\\jack.txt", "d:\\cubes\\jack.txt", false);
} catch (IOException ioe) {
System.out.println("Oops: " +
ExceptionHandler.StrackTrace2String(ioe));
}
//All done
conn.fatalDisconnect(-1, "");
} catch (SSH2Exception s2e) {
System.out.println("Error connecting: " +
ExceptionHandler.StrackTrace2String(s2e));
}
==============================================
With the Log set to debug, here is the output:
==============================================
** SSH2Transport : 'peer's version is 'SSH-2.0-3.2.0 SSH Secure Shell
Windows NT Server''
** SSH2Transport : 'peer kex algorithms: diffie-hellman-group1-sha1'
** SSH2Transport : 'peer host key algorithms: ssh-dss'
** SSH2Transport : 'peer enc. alg. cli2srv:
aes128-cbc,3des-cbc,twofish128-cbc,cast128-cbc,twofish-cbc,blowfish-cbc,aes192-cbc,aes256-cbc,twofish192-cbc,twofish256-cbc,arcfour'
** SSH2Transport : 'peer enc. alg. srv2cli:
aes128-cbc,3des-cbc,twofish128-cbc,cast128-cbc,twofish-cbc,blowfish-cbc,aes192-cbc,aes256-cbc,twofish192-cbc,twofish256-cbc,arcfour'
** SSH2Transport : 'peer mac alg. cli2srv:
hmac-sha1,hmac-sha1-96,hmac-md5,hmac-md5-96'
** SSH2Transport : 'peer mac alg. srv2cli:
hmac-sha1,hmac-sha1-96,hmac-md5,hmac-md5-96'
** SSH2Transport : 'peer comp. alg. cli2srv: none,zlib'
** SSH2Transport : 'peer comp. alg. srv2cli: none,zlib'
** SSH2Transport : 'our kex algorithms:
diffie-hellman-group1-sha1,diffie-hellman-group-exchange-sha1'
** SSH2Transport : 'our host key algorithms: ssh-dss'
** SSH2Transport : 'our enc. alg. cli2srv:
aes128-cbc,3des-cbc,twofish128-cbc,cast128-cbc,twofish-cbc,blowfish-cbc,aes192-cbc,aes256-cbc,twofish192-cbc,twofish256-cbc,arcfour'
** SSH2Transport : 'our enc. alg. srv2cli:
aes128-cbc,3des-cbc,twofish128-cbc,cast128-cbc,twofish-cbc,blowfish-cbc,aes192-cbc,aes256-cbc,twofish192-cbc,twofish256-cbc,arcfour'
** SSH2Transport : 'our mac alg. cli2srv:
hmac-sha1,hmac-sha1-96,hmac-md5,hmac-md5-96'
** SSH2Transport : 'our mac alg. srv2cli:
hmac-sha1,hmac-sha1-96,hmac-md5,hmac-md5-96'
** SSH2Transport : 'our comp. alg. cli2srv: none,zlib'
** SSH2Transport : 'our comp. alg. srv2cli: none,zlib'
** SSH2Transport : 'KEX algorithm chosen: diffie-hellman-group1-sha1'
** SSH2Transport : 'same KEX guessed? true'
** SSH2Transport : 'first KEX follows? false'
** SSH2Transport : 'server's signature verified'
** SSH2Transport : 'new transmitter context (aes128-cbc,hmac-sha1,none)'
** SSH2Transport : 'new receiver context (aes128-cbc,hmac-sha1,none)'
** SSH2UserAuth : 'server accepted: ssh-userauth'
** SSH2UserAuth : 'failure continuation: publickey,password (partial:
false)'
** SSH2UserAuth : 'our remaining methods: password'
** SSH2UserAuth : 'trying method: password'
** SSH2UserAuth : 'successful authentication with password'
** SSH2Channel : 'open confirmation, ch. #0, init-winsz = 10000,
max-pktsz = 8192'
** SSH2StreamChannel : 'starting ch. #0 (session) transmitter'
** SSH2StreamChannel : 'starting ch. #0 (session) receiver'
false
** SSH2StreamChannel : 'exiting ch. #0 (session) receiver, 0 bytes rx'
** SSH2Channel : 'closing ch. #0 (session)'
** SSH2StreamChannel : 'exiting ch. #0 (session) transmitter, 0 bytes tx'
** SSH2Channel : 'open confirmation, ch. #1, init-winsz = 10000,
max-pktsz = 8192'
** SSH2StreamChannel : 'starting ch. #1 (session) transmitter'
** SSH2StreamChannel : 'starting ch. #1 (session) receiver'
Oops: java.io.IOException: SSHSCP.remoteConnect, failed to run: scp -f
-v d:\Cubes\aCube.txt
at com.mindbright.sshcommon.SSHSCP1.remoteConnect(SSHSCP1.java:373)
at com.mindbright.sshcommon.SSHSCP1.copyToLocal(SSHSCP1.java:144)
at com.metlife.ins.ereport.test.SSHClient.main(SSHClient.java:100)
** SSH2Channel : 'closing ch. #1 (session)'
** SSH2Transport : 'disconnect: '
** SSH2StreamChannel : 'exiting ch. #1 (session) receiver, 0 bytes rx'
** SSH2StreamChannel : 'exiting ch. #1 (session) transmitter, 0 bytes tx'
Process terminated with exit code 0