FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
telegramPullRequestBot/src/main/java/SshConnection.java at main · Dimus99/telegramPullRequestBot · GitHub
Dimus99
/
telegramPullRequestBot
Public
Notifications
You must be signed in to change notification settings
Fork
2
Star
0
Code
Issues
0
Pull requests
1
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
telegramPullRequestBot
/
src
/
main
/
java
/
SshConnection.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
102 lines (76 loc) · 3 KB
Breadcrumbs
telegramPullRequestBot
/
src
/
main
/
java
/
SshConnection.java
Copy path
File metadata and controls
102 lines (76 loc) · 3 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
import
com
.
jcraft
.
jsch
.*;
import
java
.
io
.*;
public
class
SshConnection
{
private
final
Session
session
;
private
PrintStream
ps
;
private
InputStream
input
;
private
OutputStream
ops
;
private
Channel
channel
;
private
static
final
String
STRICT_HOSTKEY_CHECKIN_KEY
=
"StrictHostKeyChecking"
;
private
static
final
String
STRICT_HOSTKEY_CHECKIN_VALUE
=
"no"
;
private
ChannelSftp
setupJsch
()
throws
JSchException
{
return
(
ChannelSftp
)
session
.
openChannel
(
"sftp"
);
}
public
SshConnection
(
String
user
,
String
password
,
String
host
,
Integer
port
)
throws
JSchException
{
final
JSch
connection
=
new
JSch
();
session
=
connection
.
getSession
(
user
,
host
,
port
);
session
.
setPassword
(
password
);
session
.
setConfig
(
STRICT_HOSTKEY_CHECKIN_KEY
,
STRICT_HOSTKEY_CHECKIN_VALUE
);
System
.
out
.
println
(
"-- Try to connect to the server "
+
host
+
":"
+
port
+
" with user "
+
user
);
session
.
connect
();
System
.
out
.
println
(
"-- Connexion OK"
);
}
public
String
executeCommand
(
String
command
)
{
return
executeCommand
(
command
,
true
);
}
public
String
executeCommand
(
String
command
,
Boolean
isWrite
)
{
StringBuilder
outputBuffer
=
new
StringBuilder
();
try
{
Channel
channel
=
session
.
openChannel
(
"exec"
);
((
ChannelExec
)
channel
).
setCommand
(
command
);
InputStream
commandOutput
=
channel
.
getInputStream
();
channel
.
connect
();
System
.
out
.
println
(
"START WRITE"
);
if
(
isWrite
) {
int
readByte
=
commandOutput
.
read
();
while
(
readByte
!=
0xffffffff
) {
outputBuffer
.
append
((
char
)
readByte
);
readByte
=
commandOutput
.
read
();
}
}
System
.
out
.
println
(
"STOP WRITE"
);
channel
.
disconnect
();
}
catch
(
IOException
ioX
)
{
System
.
out
.
println
(
ioX
.
getMessage
());
return
null
;
}
catch
(
JSchException
jschX
)
{
System
.
out
.
println
(
jschX
.
getMessage
());
return
null
;
}
return
outputBuffer
.
toString
();
}
public
void
sendToServer
(
File
dir
,
String
remoteDir
)
throws
IOException
,
JSchException
,
SftpException
{
String
command
=
"tar -cvf "
+
dir
+
".tar.gz -C "
+
dir
+
" *"
;
Process
process
=
Runtime
.
getRuntime
().
exec
(
command
);
System
.
out
.
println
(
"ZIPPED"
);
ChannelSftp
channelSftp
=
setupJsch
();
channelSftp
.
connect
();
String
localFile
=
dir
+
".tar.gz"
;
executeCommand
(
"mkdir "
+
remoteDir
);
channelSftp
.
put
(
localFile
,
remoteDir
);
channelSftp
.
exit
();
String
unzipCommand
=
"tar -xf "
+
remoteDir
+
dir
.
getName
()+
".tar.gz"
+
" -C "
+
remoteDir
;
System
.
out
.
println
(
unzipCommand
);
executeCommand
(
unzipCommand
);
executeCommand
(
"cd "
+
remoteDir
+
" && rm "
+
dir
.
getName
()+
".tar.gz"
,
false
);
}
}
Back
|
FazBrowse Home
|
New Git URL