在实际的开发中有时会遇到需要在ubuntu上远程登录Windows的电脑去执行一些比较特殊的命令。这个时候就需要使用python的paramiko模块,首先去远程登录,然后再去执行对应的cmd。
paramiko
是一个用于在Python中实现SSH(Secure Shell)协议的模块,它提供了客户端和服务器的功能,使得你能够在网络上安全地执行命令、传输文件等。
你可以使用以下命令使用pip安装paramiko:
pip3 install paramiko
import paramiko
ssh = paramiko.SSHClient()
在连接SSH服务器之前,建议设置主机密钥策略,以便验证远程主机的身份:
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(hostname='your_host', username='your_username', password='your_password')
以cmd为echo "Connection test"来介绍
cmd = 'echo "Connection test"'
stdin, stdout, stderr = ssh.exec_command(cmd)
ssh.close()
import paramiko
try:
# Your paramiko code here
ssh.close()
except paramiko.AuthenticationException:
print("Authentication failed, please verify your credentials")
except paramiko.SSHException as e:
print(f"Unable to establish SSH connection: {e}")
except Exception as e:
print(f"An error occurred: {e}")
要通过SSH连接到Windows电脑,你需要使用SSH客户端,并确保Windows电脑上已启用了OpenSSH服务。
sudo apt-get update
sudo apt-get install openssh-client
ssh username@your-windows-ip
该测试用例首先使用echo "Connection test"
的测试命令去测试远程链接是否确实建立。然后再去执行真是的测试命令,进入到Windows D盘的test目录,去获取test目录下的所有文件和目录,然后输出结果。
import paramiko
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
user = 'xxx'
remote_host = 'yyy'
passwd = 'zzz'
cmd = f'powershell.exe cd D:\\test; Get-ChildItem;'
try:
ssh.connect(remote_host, username = user, password = passwd)
print(user, '@', remote_host, ": connected successfully.")
print(" =================== 1 ======================")
# 执行一个简单的命令(例如:echo)来确认连接确实建立
stdin, stdout, stderr = ssh.exec_command('echo "Connection test"')
# 获取命令输出(如果有)
output = stdout.read().decode('utf-8', errors='ignore').strip()
error = stderr.read().decode('utf-8', errors='ignore').strip()
print(" =================== 2 ======================")
# 根据命令执行结果输出信息
if output:
print("Connection test succeeded:", output)
if error:
print("Connection test had errors:", error)
print(" =================== 3 ======================")
# 执行一个简单的命令(例如:echo)来确认连接确实建立
stdin, stdout, stderr = ssh.exec_command(cmd)
print(" =================== 3 - 1 ======================")
# 获取命令输出(如果有)
output = stdout.read().decode('ISO-8859-1', errors='ignore').strip()
print(" =================== 3 - 2 ======================")
error = stderr.read().decode('utf-8', errors='ignore').strip()
print(" =================== 3 - 3 ======================")
print(" =================== 4 ======================")
# 根据命令执行结果输出信息
if output:
print("Connection test succeeded:", output)
if error:
print("Connection test had errors:", error)
print(" =================== 5 ======================")
# 关闭连接
ssh.close()
except paramiko.AuthenticationException:
print("Authentication failed, please verify your credentials")
except paramiko.SSHException as sshException:
print("Unable to establish SSH connection: %s" % sshException)
except paramiko.BadHostKeyException as badHostKeyException:
print("Unable to verify server's host key: %s" % badHostKeyException)
except Exception as e:
print(e)