Movatterモバイル変換


[0]ホーム

URL:



Code forHow to Execute Shell Commands in a Remote Machine in Python Tutorial


View on Github

execute_commands.py

import paramikohostname = "192.168.1.101"username = "test"password = "abc123"commands = [    "pwd",    "id",    "uname -a",    "df -h"]# initialize the SSH clientclient = paramiko.SSHClient()# add to known hostsclient.set_missing_host_key_policy(paramiko.AutoAddPolicy())try:    client.connect(hostname=hostname, username=username, password=password)except:    print("[!] Cannot connect to the SSH Server")    exit()# execute the commandsfor command in commands:    print("="*50, command, "="*50)    stdin, stdout, stderr = client.exec_command(command)    print(stdout.read().decode())    err = stderr.read().decode()    if err:        print(err)    client.close()

bash.sh

cd Desktopmkdir test_foldercd test_folderecho "$PATH" > path.txt

execute_bash.py

import paramikohostname = "192.168.1.101"username = "test"password = "abc123"# initialize the SSH clientclient = paramiko.SSHClient()# add to known hostsclient.set_missing_host_key_policy(paramiko.AutoAddPolicy())try:    client.connect(hostname=hostname, username=username, password=password)except:    print("[!] Cannot connect to the SSH Server")    exit()# read the BASH script content from the filebash_script = open("script.sh").read()# execute the BASH scriptstdin, stdout, stderr = client.exec_command(bash_script)# read the standard output and print itprint(stdout.read().decode())# print errors if there are anyerr = stderr.read().decode()if err:    print(err)# close the connectionclient.close()

Mastering YOLO - Topic - Top


Join 50,000+ Python Programmers & Enthusiasts like you!



Tags

Practical Python PDF Processing EBook - Contact Us - Middle


New Tutorials

Popular Tutorials


Practical Python PDF Processing EBook - Abdou Author - Bottom







[8]ページ先頭

©2009-2025 Movatter.jp