#!/usr/bin/env pythonimport paramikohostname= ' was1 ' port=22username= ' MQM ' password= ' Redhat ' cmd= ' function printline { echo "--------------------------------------------------------------" } if [ -d /tmp/test ]then printline check_test_direcho "/tmp/test exist" Elsemkdir /tmp/testfi cat /etc/fstab>/tmp/test/fstab.bkprintline ls_testls /tmp/testprintline cat_fstab.bkcat /tmp/test/fstab.bkprintline ip_check/sbin/ifconfig ' if __name__== ' __main__ ':p aramiko.util.log _to_file (' Paramiko.log ') S=paramiko. Sshclient () S.set_missing_host_key_policy (Paramiko. Autoaddpolicy ()) #s. Load_system_host_keys () s.connect (Hostname,port,username,password) stdin,stdout,stderr=s.exec _command (CMD) print stdout.read () PRINT STDERR.READ () S.close ()
Note: The remote host must be able to resolve the host name of the host where the Python program is located, or the program executes very slowly.
Use Python to execute multi-line shell scripts on a remote computer