@ThreadSafe public final class ShellUtils extends Object
Modifier and Type | Class and Description |
---|---|
static class |
ShellUtils.ExitCodeException
This is an IOException with exit code added.
|
Modifier and Type | Field and Description |
---|---|
static String |
COMMON_SSH_OPTS
Common shell OPTS to prevent stalling.
|
static String |
MOUNT_COMMAND
a Unix command for getting mount information.
|
static String |
SET_PERMISSION_COMMAND
a Unix command to set permission.
|
static String |
TOKEN_SEPARATOR_REGEX
Token separator regex used to parse Shell tool outputs.
|
Modifier and Type | Method and Description |
---|---|
static CommandReturn |
execCmdWithBackup(ShellCommand cmd,
ShellCommand backupCmd)
Executes a shell command.
|
static String |
execCommand(String... cmd)
Static method to execute a shell command.
|
static CommandReturn |
execCommandWithOutput(String... cmd)
Static method to execute a shell command and tolerate non-zero exit code.
|
static String[] |
getGroupsForUserCommand(String user)
Gets a Unix command to get a given user's groups list.
|
static String[] |
getSetPermissionCommand(String perm,
String filePath)
Returns a Unix command to set permission.
|
static List<UnixMountInfo> |
getUnixMountInfo()
Gets system mount information.
|
static UnixMountInfo |
parseMountInfo(String line) |
static CommandReturn |
scpCommandWithOutput(String hostname,
String fromFile,
String toFile,
boolean isDir)
Static method to execute an scp command to copy a remote file/dir to local.
|
static CommandReturn |
sshExecCommandWithOutput(String hostname,
String... cmd)
Static method to execute a shell command remotely via ssh.
|
public static final String COMMON_SSH_OPTS
public static final String SET_PERMISSION_COMMAND
public static final String MOUNT_COMMAND
public static final String TOKEN_SEPARATOR_REGEX
public static String[] getGroupsForUserCommand(String user)
user
- the user namepublic static String[] getSetPermissionCommand(String perm, String filePath)
perm
- the permission of filefilePath
- the file pathpublic static List<UnixMountInfo> getUnixMountInfo() throws IOException
IOException
public static UnixMountInfo parseMountInfo(String line)
line
- the line to parseUnixMountInfo
public static String execCommand(String... cmd) throws IOException
cmd
- shell command to executeIOException
- in various situations:
1. ShellUtils.ExitCodeException
when exit code is non-zero
2. when the executable is not valid, i.e. running ls in Windows
3. execution interrupted
4. other normal reasons for IOExceptionpublic static CommandReturn execCommandWithOutput(String... cmd) throws IOException
cmd
- shell command to executeIOException
- in various situations:
1. when the executable is not valid, i.e. running ls in Windows
2. execution interrupted
3. other normal reasons for IOExceptionpublic static CommandReturn sshExecCommandWithOutput(String hostname, String... cmd) throws IOException
hostname
- Hostname where the command should executecmd
- shell command to executeIOException
- in various situations:
1. when the executable is not valid, i.e. running ls in Windows
2. execution interrupted
3. other normal reasons for IOExceptionpublic static CommandReturn scpCommandWithOutput(String hostname, String fromFile, String toFile, boolean isDir) throws IOException
hostname
- Hostname where the command should executefromFile
- File path on remote hosttoFile
- File path to copy to on localhostisDir
- Is the file a directoryIOException
- in various situations:
1. when the executable is not valid, i.e. running ls in Windows
2. execution interrupted
3. other normal reasons for IOExceptionpublic static CommandReturn execCmdWithBackup(ShellCommand cmd, ShellCommand backupCmd) throws IOException
cmd
- the primary commandbackupCmd
- a backup optionCommandReturn
with combined outputIOException
Copyright © 2023. All Rights Reserved.