Skip to content

Commit f3c5f2b

Browse files
Release 2.10.0
1 parent 44cad23 commit f3c5f2b

File tree

4 files changed

+207
-252
lines changed

4 files changed

+207
-252
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ extensions:
2626

2727
To run this sample:
2828

29-
See [DefaultAzureCredential](https://github.com/Azure/azure-sdk-for-java/tree/master/sdk/identity/azure-identity#defaultazurecredential) and prepare the authentication works best for you. For more details on authentication, please refer to [AUTH.md](https://github.com/Azure/azure-sdk-for-java/blob/master/sdk/resourcemanager/docs/AUTH.md).
29+
See [DefaultAzureCredential](https://github.com/Azure/azure-sdk-for-java/tree/main/sdk/identity/azure-identity#defaultazurecredential) and prepare the authentication works best for you. For more details on authentication, please refer to [AUTH.md](https://github.com/Azure/azure-sdk-for-java/blob/main/sdk/resourcemanager/docs/AUTH.md).
3030

3131
git clone https://github.com/Azure-Samples/acr-java-manage-azure-container-registry-with-webhooks.git
3232

pom.xml

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -54,12 +54,12 @@
5454
<dependency>
5555
<groupId>com.azure.resourcemanager</groupId>
5656
<artifactId>azure-resourcemanager</artifactId>
57-
<version>2.1.0</version>
57+
<version>2.10.0</version>
5858
</dependency>
5959
<dependency>
6060
<groupId>com.azure</groupId>
6161
<artifactId>azure-identity</artifactId>
62-
<version>1.2.0</version>
62+
<version>1.4.1</version>
6363
</dependency>
6464
<dependency>
6565
<groupId>com.jcraft</groupId>
@@ -76,15 +76,10 @@
7676
<artifactId>docker-java</artifactId>
7777
<version>3.2.1</version>
7878
</dependency>
79-
<dependency>
80-
<groupId>com.github.cverges.expect4j</groupId>
81-
<artifactId>expect4j</artifactId>
82-
<version>1.6</version>
83-
</dependency>
8479
<dependency>
8580
<groupId>com.github.spotbugs</groupId>
8681
<artifactId>spotbugs-annotations</artifactId>
87-
<version>4.0.2</version>
82+
<version>4.2.2</version>
8883
</dependency>
8984
</dependencies>
9085
</project>

src/main/java/com/azure/resourcemanager/samples/SSHShell.java

Lines changed: 1 addition & 115 deletions
Original file line numberDiff line numberDiff line change
@@ -11,32 +11,21 @@
1111
import com.jcraft.jsch.JSchException;
1212
import com.jcraft.jsch.KeyPair;
1313
import com.jcraft.jsch.Session;
14-
import expect4j.Closure;
15-
import expect4j.Expect4j;
16-
import expect4j.ExpectState;
17-
import expect4j.matches.Match;
18-
import expect4j.matches.RegExpMatch;
19-
import org.apache.oro.text.regex.MalformedPatternException;
2014

2115
import java.io.BufferedOutputStream;
2216
import java.io.ByteArrayOutputStream;
2317
import java.io.IOException;
2418
import java.io.InputStream;
2519
import java.io.UnsupportedEncodingException;
2620
import java.nio.charset.StandardCharsets;
27-
import java.util.ArrayList;
2821
import java.util.Hashtable;
29-
import java.util.List;
3022

3123
/**
3224
* Utility class to run commands on Linux VM via SSH.
3325
*/
3426
public final class SSHShell {
3527
private final Session session;
3628
private final ChannelShell channel;
37-
private final Expect4j expect;
38-
private final StringBuilder shellBuffer = new StringBuilder();
39-
private List<Match> linuxPromptMatches = new ArrayList<>();
4029

4130
/**
4231
* Creates SSHShell.
@@ -45,21 +34,9 @@ public final class SSHShell {
4534
* @param port the ssh port
4635
* @param userName the ssh user name
4736
* @param password the ssh password
48-
* @return the shell
4937
* @throws JSchException the JSchException
50-
* @throws IOException the IOException
5138
*/
52-
private SSHShell(String host, int port, String userName, String password)
53-
throws JSchException, IOException {
54-
Closure expectClosure = getExpectClosure();
55-
for (String linuxPromptPattern : new String[]{"\\>", "#", "~#", "~\\$"}) {
56-
try {
57-
Match match = new RegExpMatch(linuxPromptPattern, expectClosure);
58-
linuxPromptMatches.add(match);
59-
} catch (MalformedPatternException malformedEx) {
60-
throw new RuntimeException(malformedEx);
61-
}
62-
}
39+
private SSHShell(String host, int port, String userName, String password) throws JSchException {
6340
JSch jsch = new JSch();
6441
this.session = jsch.getSession(userName, host, port);
6542
session.setPassword(password);
@@ -68,41 +45,6 @@ private SSHShell(String host, int port, String userName, String password)
6845
session.setConfig(config);
6946
session.connect(60000);
7047
this.channel = (ChannelShell) session.openChannel("shell");
71-
this.expect = new Expect4j(channel.getInputStream(), channel.getOutputStream());
72-
channel.connect();
73-
}
74-
75-
/**
76-
* Creates SSHShell.
77-
*
78-
* @param host the host name
79-
* @param port the ssh port
80-
* @param userName the ssh user name
81-
* @param sshPrivateKey the ssh password
82-
* @return the shell
83-
* @throws JSchException the JSchException
84-
* @throws IOException the IOException
85-
*/
86-
private SSHShell(String host, int port, String userName, byte[] sshPrivateKey)
87-
throws JSchException, IOException {
88-
Closure expectClosure = getExpectClosure();
89-
for (String linuxPromptPattern : new String[]{"\\>", "#", "~#", "~\\$"}) {
90-
try {
91-
Match match = new RegExpMatch(linuxPromptPattern, expectClosure);
92-
linuxPromptMatches.add(match);
93-
} catch (MalformedPatternException malformedEx) {
94-
throw new RuntimeException(malformedEx);
95-
}
96-
}
97-
JSch jsch = new JSch();
98-
jsch.setKnownHosts(System.getProperty("user.home") + "/.ssh/known_hosts");
99-
jsch.addIdentity(host, sshPrivateKey, (byte[]) null, (byte[]) null);
100-
this.session = jsch.getSession(userName, host, port);
101-
this.session.setConfig("StrictHostKeyChecking", "no");
102-
this.session.setConfig("PreferredAuthentications", "publickey,keyboard-interactive,password");
103-
session.connect(60000);
104-
this.channel = (ChannelShell) session.openChannel("shell");
105-
this.expect = new Expect4j(channel.getInputStream(), channel.getOutputStream());
10648
channel.connect();
10749
}
10850

@@ -122,45 +64,6 @@ public static SSHShell open(String host, int port, String userName, String passw
12264
return new SSHShell(host, port, userName, password);
12365
}
12466

125-
/**
126-
* Opens a SSH shell.
127-
*
128-
* @param host the host name
129-
* @param port the ssh port
130-
* @param userName the ssh user name
131-
* @param sshPrivateKey the ssh private key
132-
* @return the shell
133-
* @throws JSchException exception thrown
134-
* @throws IOException IO exception thrown
135-
*/
136-
public static SSHShell open(String host, int port, String userName, byte[] sshPrivateKey)
137-
throws JSchException, IOException {
138-
return new SSHShell(host, port, userName, sshPrivateKey);
139-
}
140-
141-
/**
142-
* Runs a given list of commands in the shell.
143-
*
144-
* @param commands the commands
145-
* @return the result
146-
* @throws Exception exception thrown
147-
*/
148-
public String runCommands(List<String> commands) throws Exception {
149-
String output = null;
150-
try {
151-
for (String command : commands) {
152-
expect.expect(this.linuxPromptMatches);
153-
expect.send(command);
154-
expect.send("\r");
155-
expect.expect(this.linuxPromptMatches);
156-
}
157-
output = shellBuffer.toString();
158-
} finally {
159-
shellBuffer.setLength(0);
160-
}
161-
return output;
162-
}
163-
16467
/**
16568
* Executes a command on the remote host.
16669
*
@@ -278,9 +181,6 @@ public void upload(InputStream from, String fileName, String toPath, boolean isU
278181
* Closes shell.
279182
*/
280183
public void close() {
281-
if (expect != null) {
282-
expect.close();
283-
}
284184
if (channel != null) {
285185
channel.disconnect();
286186
}
@@ -289,20 +189,6 @@ public void close() {
289189
}
290190
}
291191

292-
private Closure getExpectClosure() {
293-
return new Closure() {
294-
/**
295-
* @throws Exception thrown Exception
296-
*/
297-
public void run(ExpectState expectState) throws Exception {
298-
String outputBuffer = expectState.getBuffer();
299-
System.out.println(outputBuffer);
300-
shellBuffer.append(outputBuffer);
301-
expectState.exp_continue();
302-
}
303-
};
304-
}
305-
306192
/**
307193
* Automatically generate SSH keys.
308194
*

0 commit comments

Comments
 (0)