karate:如何使用karate运行SSH命令

huangapple go评论68阅读模式
英文:

karate: How to Run SSH command using karate

问题

I need to run command with ssh in mac ssh test01.dev.test.com and currently could not send password.
Getting below mentioned error.

Output:
working dir :null
Pseudo-terminal will not be allocated because stdin is not a terminal.
permission denied please try again

  • def command =
    """
    function(line) {
    var proc = karate.fork({ redirectErrorStream: false, useShell: true, line: line });
    proc.waitSync();
    karate.set('sysOut', proc.sysOut);
    karate.set('sysErr', proc.sysErr);
    karate.set('exitCode', proc.exitCode);
    }
    """
    Scenario:
  • command('ssh test01.dev.test.com')
英文:

I need to run command with ssh in mac ssh test01.dev.test.com' and currently could not send password .
Getting below mentioned error.

Output:
working dir :null
Pseudo -terminal will not be allocated because stdin is not a terminal.
permission denied please try again

  • def command =
    """
    function(line) {
    var proc = karate.fork({ redirectErrorStream: false, useShell: true, line: line });
    proc.waitSync();
    karate.set('sysOut', proc.sysOut);
    karate.set('sysErr', proc.sysErr);
    karate.set('exitCode', proc.exitCode);
    }
    """
    Scenario:
  • command('ssh test01.dev.test.com')

答案1

得分: 1

使用Karate的基本CLI功能可能对SSH来说过于有限。我们有一个示例,展示了如何使用Java库并建立SSH连接。可以在这里找到:https://github.com/karatelabs/karate-examples/blob/main/ssh/README.md

  • def config = { user: 'ec2-user', host: '', privateKey: '' }
  • def SshSession = Java.type('karate.SshSession')
  • def ssh = new SshSession(config)
  • ssh.input('pwd')
  • ssh.input('whoami')
  • ssh.input('exit')
英文:

Using the basic CLI capabilities of Karate may be too limiting for SSH. We have a sample that shows how to use a Java library and use an SSH connection. It can be found here: https://github.com/karatelabs/karate-examples/blob/main/ssh/README.md

* def config = { user: 'ec2-user', host: '', privateKey: '' }
* def SshSession = Java.type('karate.SshSession')
* def ssh = new SshSession(config)
* ssh.input('pwd')
* ssh.input('whoami')
* ssh.input('exit')

答案2

得分: -1

Karate主要用于测试Web服务,而不用于管理Shell或命令行进程,比如SSH。它是一个开源工具,将API测试自动化、模拟、性能测试甚至UI自动化整合到一个统一的框架中。它确实可以通过karate.fork()调用命令行进程,但这不是它的主要用途,可能不会完全支持高效处理这些命令。

在您的具体情况下,由于安全原因和需要输入密码或使用SSH密钥,运行SSH命令可能会很棘手。

如果您真的需要这样做,您可能需要考虑其他适合管理命令行或基于终端的进程的方法或工具。

其中一种方法可以是使用像Python与paramiko库这样的语言,允许进行SSH连接和命令执行。以下是您可以做的基本示例:

import paramiko

ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect('test01.dev.test.com', username='yourusername', password='yourpassword')

stdin, stdout, stderr = ssh.exec_command('your command here')

for line in stdout:
    print(line.strip('\n'))

ssh.close()

另一种方法可以是使用expect脚本或像Ansible这样的自动化工具,如果这是较大流程的一部分。

但请记住,从安全角度来看,将密码存储在脚本中通常是一个不好的做法。在生产用例中,您可能希望使用SSH密钥或某种安全的密钥管理方式。

英文:

Karate is primarily used for testing web-services and not for managing shell or command line processes such as SSH. It is an open-source tool that combines API test-automation, mocks, performance-testing and even UI automation into a single, unified framework. It does have the ability to call command line processes via karate.fork() but it's not its primary use-case and may not have full support for handling such commands efficiently.

In your specific case, running SSH commands can be tricky due to security reasons and the need for password entry or the use of SSH keys.

If you really need to do this, you would probably want to consider other approaches or tools that are better suited for managing command line or terminal-based processes.

One of such ways could be using a language like Python with the paramiko library, which allows for SSH connections and command execution. Here's a basic example of what you could do:

import paramiko

ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect('test01.dev.test.com', username='yourusername', password='yourpassword')

stdin, stdout, stderr = ssh.exec_command('your command here')

for line in stdout:
    print(line.strip('\n'))

ssh.close()

Another approach could be to use an expect script, or tools like Ansible for automation if this is part of a larger process.

Do keep in mind though that storing passwords in scripts is generally a bad practice from a security perspective. You might want to use SSH keys, or some form of secure secrets management for production use-cases.

huangapple
  • 本文由 发表于 2023年7月18日 08:19:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/76708818.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定