Th2 102019
Chào các bạn,
Để check thông tin một máy chủ, các bạn thường ssh đến và check các thông số theo ý muốn, việc này đơn giản nhưng số lượng máy chủ càng lớn, công việc càng nhàm. Để giải quyết việc này các bạn hoàn toàn có thể sử dụng chương trình tự động. Ví dụ dưới đây để làm việc đó.
import paramiko
import time
def disable_paging(remote_conn):
'''KhanhNN'''
remote_conn.send("terminal length 0\n")
time.sleep(1)
output = remote_conn.recv(1000)
return output
if __name__ == '__main__':
ip = '192.168.0.36'
username = 'khanhnnvn'
password = '1111111'
remote_conn_pre = paramiko.SSHClient()
remote_conn_pre.set_missing_host_key_policy(
paramiko.AutoAddPolicy())
remote_conn_pre.connect(ip, username=username, password=password, look_for_keys=False, allow_agent=False)
print "SSH connection established to %s" % ip
remote_conn = remote_conn_pre.invoke_shell()
print "Interactive SSH session established"
output = remote_conn.recv(1000)
print output
disable_paging(remote_conn)
remote_conn.send("\n")
remote_conn.send("df -h\n")
remote_conn.send("service --status-all | grep running\n")
time.sleep(2)
output = remote_conn.recv(5000)
print output
Sau khi chạy ta có thông tin như sau:

Chúc các bạn thành công.