Anasayfa / python / Python SSH connection and exec command

Python SSH connection and exec command

Python ssh connection and exec command

import paramiko
import time


def ssh_login(host_name, user_name, my_password):
    ssh = paramiko.SSHClient()
    ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    ssh.connect(hostname=host_name, username=user_name, password=my_password)
    # key_filename='/home/ubuntu/.ssh/mykey.pem'
    return ssh


def ssh_exec_command(ssh: object, komut: str):
    stdin, stdout, stderr = ssh.exec_command(komut)
    for line in stdout.read().splitlines():
        print(line.decode("utf-8"))


def ssh_close(ssh):
    ssh.close()


def main():
    host_name = 'your_ip'
    user_name = 'your_usename'
    my_password = 'your_passs'
    mysample_ssh = ssh_login(host_name, user_name, my_password)
    ssh_exec_command(mysample_ssh, '/etc/rc.d/init.d/mysql stop')
    ssh_exec_command(mysample_ssh, 'myisamchk -r /var/lib/mysql/*/*.MYI')
    ssh_exec_command(mysample_ssh, '/etc/rc.d/init.d/mysql start')
    ssh_close(mysample_ssh)


if __name__ == '__main__':
    main()

 

Hakkında ibrahim

İlgili Makaleler

python locale.Error: unsupported locale setting locale.setlocale(locale.LC_ALL, ‘Turkish_Turkey.1254’)

if os.name == 'nt': locale.setlocale(locale.LC_ALL, 'Turkish_Turkey.1254') yeni windows updateleri ile artık Türkiye (‘Turkish_Türkiye’, ‘1254’)

Bir cevap yazın

E-posta hesabınız yayımlanmayacak. Gerekli alanlar * ile işaretlenmişlerdir