Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
H
hpc-workflow-manager
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container Registry
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
FIJI
hpc-workflow-manager
Commits
5f6de754
Commit
5f6de754
authored
7 years ago
by
Jan Kožusznik
Browse files
Options
Downloads
Patches
Plain Diff
closable
parent
7e84feda
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
java-scpclient/src/main/java/cz/it4i/fiji/scpclient/ScpClient.java
+71
-58
71 additions, 58 deletions
...lient/src/main/java/cz/it4i/fiji/scpclient/ScpClient.java
with
71 additions
and
58 deletions
java-scpclient/src/main/java/cz/it4i/fiji/scpclient/ScpClient.java
+
71
−
58
View file @
5f6de754
package
cz.it4i.fiji.scpclient
;
import
java.io.Closeable
;
import
java.io.IOException
;
import
java.io.InputStream
;
import
java.io.OutputStream
;
...
...
@@ -16,21 +17,22 @@ import com.jcraft.jsch.JSchException;
import
com.jcraft.jsch.Session
;
import
com.jcraft.jsch.UserInfo
;
public
class
ScpClient
{
public
class
ScpClient
implements
Closeable
{
public
static
void
main
(
String
[]
args
)
throws
JSchException
,
IOException
{
boolean
u
=
new
ScpClient
(
"salomon.it4i.cz"
,
"koz01"
,
"/home/koz01/.ssh/it4i_rsa"
,
"nejlepsivyzkum"
)
.
upload
(
Paths
.
get
(
"/home/koz01/aaa/vecmath.jar"
),
"/home/koz01/"
);
boolean
d
=
new
S
cpClient
(
"salomon.it4i.cz"
,
"koz01"
,
"/home/koz01/.ssh/it4i_rsa"
,
"nejlepsivyzkum"
)
.
download
(
"/home/koz01/proof"
,
Paths
.
get
(
"/home/koz01/aaa/proof"
)
);
System
.
out
.
println
(
u
);
System
.
out
.
println
(
d
);
try
(
ScpClient
scpClient
=
new
ScpClient
(
"salomon.it4i.cz"
,
"koz01"
,
"/home/koz01/.ssh/it4i_rsa"
,
"nejlepsivyzkum"
))
{
boolean
u
=
scpClient
.
upload
(
Paths
.
get
(
"/home/koz01/aaa/vecmath.jar"
),
"/home/koz01/"
);
boolean
d
=
s
cpClient
.
download
(
"/home/koz01/proof"
,
Paths
.
get
(
"/home/koz01/aaa/proof"
));
System
.
out
.
println
(
u
);
System
.
out
.
println
(
d
);
}
}
private
String
hostName
;
private
String
username
;
private
JSch
jsch
=
new
JSch
();
private
Session
session
;
public
ScpClient
(
String
hostName
,
String
username
,
Identity
privateKeyFile
)
throws
JSchException
{
super
();
...
...
@@ -55,10 +57,12 @@ public class ScpClient {
public
boolean
download
(
String
lfile
,
Path
rfile
)
throws
JSchException
,
IOException
{
Session
session
=
connectionSession
();
// exec 'scp -f rfile' remotely
String
command
=
"scp -f "
+
lfile
;
Channel
channel
=
session
.
openChannel
(
"exec"
);
try
{
// exec 'scp -f rfile' remotely
String
command
=
"scp -f "
+
lfile
;
Channel
channel
=
session
.
openChannel
(
"exec"
);
((
ChannelExec
)
channel
).
setCommand
(
command
);
// get I/O streams for remote scp
...
...
@@ -142,7 +146,7 @@ public class ScpClient {
}
}
finally
{
session
.
disconnect
();
channel
.
disconnect
();
}
return
true
;
}
...
...
@@ -157,69 +161,71 @@ public class ScpClient {
String
command
=
"scp "
+
(
ptimestamp
?
"-p"
:
""
)
+
" -t "
+
rfile
;
Channel
channel
=
session
.
openChannel
(
"exec"
);
((
ChannelExec
)
channel
).
setCommand
(
command
);
try
{
// get I/O streams for remote scp
try
(
OutputStream
out
=
channel
.
getOutputStream
();
InputStream
in
=
channel
.
getInputStream
())
{
channel
.
connect
();
if
(
checkAck
(
in
)
!=
0
)
{
return
false
;
}
if
(
ptimestamp
)
{
command
=
"T "
+
(
file
.
toFile
().
lastModified
()
/
1000
)
+
" 0"
;
// The access time should be sent here,
// but it is not accessible with JavaAPI ;-<
command
+=
(
" "
+
(
file
.
toFile
().
lastModified
()
/
1000
)
+
" 0\n"
);
out
.
write
(
command
.
getBytes
());
out
.
flush
();
if
(
checkAck
(
in
)
!=
0
)
{
return
false
;
}
}
// get I/O streams for remote scp
try
(
OutputStream
out
=
channel
.
getOutputStream
();
InputStream
in
=
channel
.
getInputStream
())
{
channel
.
connect
();
if
(
checkAck
(
in
)
!=
0
)
{
return
false
;
}
// send "C0644 filesize filename", where filename should not include '/'
long
filesize
=
file
.
toFile
().
length
()
;
command
=
"C0644 "
+
filesize
+
" "
;
command
+=
file
.
getFileName
().
toString
();
command
+=
"
\n"
;
if
(
ptimestamp
)
{
command
=
"T "
+
(
file
.
toFile
().
lastModified
()
/
1000
)
+
" 0"
;
// The access time should be sent here,
// but it is not accessible with JavaAPI ;-<
command
+=
(
" "
+
(
file
.
toFile
().
lastModified
()
/
1000
)
+
" 0
\n"
)
;
out
.
write
(
command
.
getBytes
());
out
.
flush
();
if
(
checkAck
(
in
)
!=
0
)
{
return
false
;
}
byte
[]
buf
=
new
byte
[
1024
];
// send a content of lfile
try
(
InputStream
fis
=
Files
.
newInputStream
(
file
))
{
while
(
true
)
{
int
len
=
fis
.
read
(
buf
,
0
,
buf
.
length
);
if
(
len
<=
0
)
break
;
out
.
write
(
buf
,
0
,
len
);
// out.flush();
}
}
// send '\0'
buf
[
0
]
=
0
;
out
.
write
(
buf
,
0
,
1
);
out
.
flush
();
if
(
checkAck
(
in
)
!=
0
)
{
return
false
;
}
// send "C0644 filesize filename", where filename should not include '/'
long
filesize
=
file
.
toFile
().
length
();
command
=
"C0644 "
+
filesize
+
" "
;
command
+=
file
.
getFileName
().
toString
();
command
+=
"\n"
;
out
.
write
(
command
.
getBytes
());
out
.
flush
();
if
(
checkAck
(
in
)
!=
0
)
{
return
false
;
}
byte
[]
buf
=
new
byte
[
1024
];
// send a content of lfile
try
(
InputStream
fis
=
Files
.
newInputStream
(
file
))
{
while
(
true
)
{
int
len
=
fis
.
read
(
buf
,
0
,
buf
.
length
);
if
(
len
<=
0
)
break
;
out
.
write
(
buf
,
0
,
len
);
// out.flush();
}
out
.
close
();
}
// send '\0'
buf
[
0
]
=
0
;
out
.
write
(
buf
,
0
,
1
);
out
.
flush
();
if
(
checkAck
(
in
)
!=
0
)
{
return
false
;
}
out
.
close
();
}
finally
{
channel
.
disconnect
();
session
.
disconnect
();
}
return
true
;
}
private
Session
connectionSession
()
throws
JSchException
{
Session
session
=
jsch
.
getSession
(
username
,
hostName
);
if
(
session
==
null
)
{
session
=
jsch
.
getSession
(
username
,
hostName
);
UserInfo
ui
=
new
P_UserInfo
();
UserInfo
ui
=
new
P_UserInfo
();
session
.
setUserInfo
(
ui
);
session
.
connect
();
session
.
setUserInfo
(
ui
);
}
if
(!
session
.
isConnected
())
{
session
.
connect
();
}
return
session
;
}
...
...
@@ -283,4 +289,11 @@ public class ScpClient {
}
return
b
;
}
@Override
public
void
close
()
{
if
(
session
.
isConnected
())
{
session
.
disconnect
();
}
}
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment