python通过ssh运行linux下的sh脚本,带参数,可以用来给clash的配置文件换ip等操作。

import paramiko
import os

def sshclient_execmd(hostname, port, username, password, execmd):
    s = paramiko.SSHClient()
    s.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    s.connect(hostname=hostname, port=port, username=username, password=password)
    stdin, stdout, stderr = s.exec_command (execmd)
    s.close()

if '__main__' == __name__:
    hostname = '192.168.2.5'
    port = 22
    username = 'root'
    password = '9989888'
    ip = input("ip: ")
    line=sshclient_execmd(hostname,port,username,password,"./change.sh "+ip+"")