Prevent Paramiko from exiting

Description

I am trying to write a python script in SSH in my virtual machine and execute some commands.

  • If I do this manually, my session remains open, and I can still see my logs in real time .

  • If I use a script after SSH and automate several commands, it continues to exit and return to my user invitation. I am trying to prevent this.


Video

Manually = https://dl.dropboxusercontent.com/u/56134944/ssh_wag_manually.mov

Via Python script = https://dl.dropboxusercontent.com/u/56134944/ssh_wag_py.mov


This is what I have right now

import paramiko
import time
import sys

# Note
# sudo pip install --user paramiko


def ssh_con (ip, un, pw):
    global client
    client = paramiko.SSHClient()
    client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    print ("Connecting to device/VM: %s" % ip)
    client.connect(ip, username=un, password=pw)


def cmd_io (command):
    global client_cmd
    client_cmd.send("%s \n" %command)
    time.sleep(1)
    output = client_cmd.recv(10000).decode("utf-8")
    print (output)

# ip = raw_input("Enter WAG IP : ")
# ip  = sys.argv[1]

ip = '172.168.200.300'
un = 'xyz'
pw = 'abc'

ssh_con(ip,un,pw)
client_cmd = client.invoke_shell()

print ("SSH CONNECTION ESTABLISHED TO vMEG %s" % ip)
cmd_io ("en")
cmd_io ("terminal monitor")
cmd_io ("debug wag https")
cmd_io ("debug wag httpc")
cmd_io ("debug https")
cmd_io ("debug httpc")
cmd_io ("debug wag kafka")

Result

[local]site#
terminal monitor 
[local]site#
debug wag https 
[local]site#
debug wag httpc 
[local]site#
debug https 
[local]site#
debug httpc 
[local]site#
debug wag kafka 
[local]site#

.... Quitting and return the command prompt ....

──[/Applications/MAMP/htdocs/code/python] 
└── 

How to do it and prevent it?


I open any offers at this moment.

// !

+4
2

, paramiko. , , . , :

import paramiko
import time
import sys
import socket

# Note
# sudo pip install --user paramiko


def ssh_con (ip, un, pw):
    global client
    client = paramiko.SSHClient()
    client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    print ("Connecting to device/VM: %s" % ip)
    client.connect(ip, username=un, password=pw)


def cmd_io (command):
    global client_cmd
    client_cmd.send("%s \n" %command)
    time.sleep(1)
    output = client_cmd.recv(10000).decode("utf-8")
    print (output)

# ip = raw_input("Enter WAG IP : ")
# ip  = sys.argv[1]

ip = '172.168.200.300'
un = 'xyz'
pw = 'abc'

ssh_con(ip,un,pw)
client_cmd = client.invoke_shell()

print ("SSH CONNECTION ESTABLISHED TO vMEG %s" % ip)
cmd_io ("en")
cmd_io ("terminal monitor")
cmd_io ("debug wag https")
cmd_io ("debug wag httpc")
cmd_io ("debug https")
cmd_io ("debug httpc")
cmd_io ("debug wag kafka")

client_cmd.settimeout(1.0)
while True:
    try:
        output = client_cmd.recv(10000).decode("utf-8")
        print (output)
    except socket.timeout:
        pass

. script . , stderr, , , - , 1 .

, , , script. , 100% , - .

+4

script

def write_and_print_logs(log_message):
    with open("debugLogs.txt", 'a') as logs:
        logs.write("{}\n".format(log_message))
    print(log_message)

print(<message>) write_and_print_logs(<message>). , , , .

+1

Source: https://habr.com/ru/post/1656591/


All Articles