Def sshRunCmd (hostname, username, password, cmdlist) :
Try:
The client=paramiko. SSHClient ()
Client. Set_missing_host_key_policy (paramiko. AutoAddPolicy ())
# to create SSH connection
Client. The connect (hostname=the hostname, port=22, username=the username, password=password)
# open SSH pipeline
SSH=client. Get_transport (.) open_session ()
SSH. Get_pty ()
SSH. Invoke_shell ()
# executes instructions
For CMD in cmdlist:
SSH. Sendall (CMD)
Time. Sleep (# 2) because the command executed more, don't want to get the sleep time is too long, I tried to sleep to 10 minutes, there is no problem
# I would like to add a judgment, here if the above things has been completed (i.e., no longer continue to produce content), then down, otherwise we are waiting for, the remote host is likely to be more stupid equipment, perform a variety of command, can't use the ending characters, such as ping - c 100 8.8.8.8, I think the way is N seconds no new data will continue to go down
Result=SSH. Recv (102400)
Result=result. Decode (encoding="utf-8", errors='strict')
Print (result)
F=open (path + + hostname '/' + '_' + + ". TXT "today," a ")
# f.w rite (" 111 ")
F.w rite (result)
F. lose ()
Except the Exception as e:
Print (" [% s] % s target failed, the reason is % s "% (datetime. Datetime. Now (), the hostname, STR (e)))
The else:
Print (" [% s] % s target success "% (datetime. Datetime. Now (), the hostname))
Finally:
SSH. The close ()
Client. The close ()
# I would like to add a judgment, here if the above things has been completed (i.e., no longer continue to produce content), then down, otherwise we are waiting for, the remote host is likely to be more stupid equipment, perform a variety of command, can't use the ending characters, such as ping - c 100 8.8.8.8, I think the way is N seconds no new data will continue to go down