Skip to content

Commit 5455d52

Browse files
committed
[feat] SSH可支持提权
1 parent b9c11ae commit 5455d52

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

Services/SshCredentialService.cs

+23
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,13 @@ public class SshCredentialService : ICredentialService<ConnectionInfo>
1616

1717
private List<string>? _expectedFingerprints;
1818

19+
/// <summary>
20+
/// 此处会影响命令执行的角色。
21+
/// 若为true,则会在登录的时候执行sudo su来以root身份执行,
22+
/// 否则则以当前账号身份执行。
23+
/// </summary>
24+
private bool needSudo = false;
25+
1926
public SshCredentialService(ILogger<SshCredentialService> logger, JSONNode node)
2027
{
2128
_logger = logger;
@@ -77,6 +84,20 @@ public async Task<ResultMsg> Login()
7784
try
7885
{
7986
await _client.ConnectAsync(_loginCancellationTokenSource.Token);
87+
if (needSudo)
88+
{
89+
_logger.LogWarning($"[{GetType()}.Login] sudo needed! Now execute \"sudo su\".");
90+
var sudoResult = await RunCommand("sudo su");
91+
92+
if (!sudoResult.Success)
93+
{
94+
_logger.LogError($"[{GetType()}.Login] \"sudo su\" failed! {sudoResult.Message}");
95+
}
96+
else
97+
{
98+
_logger.LogInformation($"[{GetType()}.Login] \"sudo su\" success!");
99+
}
100+
}
80101
result.Success = true;
81102
result.Message = string.Empty;
82103
tcs.SetResult(result);
@@ -136,6 +157,8 @@ public async Task StartAsync(CancellationToken cancellationToken, JSONNode node)
136157
new PasswordAuthenticationMethod(node["user"].Value, node["password"].Value),
137158
new PrivateKeyAuthenticationMethod(node["user"].Value, new PrivateKeyFile(node["privateKeyPath"].Value)));
138159

160+
needSudo = node["needSudo"];
161+
139162
await Login();
140163
_logger.LogInformation($"[{DateTime.Now}][{GetType()}.StartAsync] Initialized!");
141164
}

0 commit comments

Comments
 (0)