Using SSH Client

SSH Client Script

  • Running the following script will establish an SSH connection with a server, sends commands and receive the output.

  • Note that ANDi can either send a command synchronously and wait for its output, or send a command asynchronously and stop its execution later on in the script to get its available output (this is useful when dealing with long running SSH commands).

from andi.ssh import SshClient

# this constructor will assume that username is root, port is 22, and password is empty.
# but other values can be specified in the constructor.
with SshClient(ip_address = "192.168.20.206") as client:
    # this will execute the command and waits for the output (the script will hang for 5 seconds in this case)
    print(client.execute_command("echo hello; sleep 5s"))
    # this will start executing the command asynchronously, the script will not hang in this line
    cmd = client.start_command("echo hello; sleep 5s")
    # this will stop the execution of the previous command and get the current output
    print(cmd.get_output())
SSH

Secure Shell.