Python 脚本 - 连接到 SSH 并运行命令

发布于 2024-10-16 03:47:23 字数 253 浏览 2 评论 0原文

我已经知道有 Python 的 ssh 模块,但这不是我正在寻找的。 我想要的是一个 python 脚本来执行以下操作:

  1. >连接到 [ 用户输入 ] SSH 主机
  2. >使用凭据[由用户提供]进行连接
  3. >在 SSH 主机上运行命令 [ telnet 到 [主机 - 用户输入 ]
  4. >在 telnet 会话中选择菜单项

提前致谢,

谨致问候,

I already know there are ssh modules for Python, that's not for what I'm looking for.
What I want to have is an python script to do the following:

  1. > connect to an [ input by user ] SSH host
  2. > connect using the credentials [ provided by the user ]
  3. > run command on the SSH host [ telnet to [host - input by user ]
  4. > Select menu item in the telnet session

Thanks in advance,

Best regards,

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

早乙女 2024-10-23 03:47:23

现在流行的解决方案是 Fabric

now the popular solution is Fabric

朮生 2024-10-23 03:47:23

如果您确实正在寻找一个可以自动执行 CLI 交互的模块,那么 pexpect

If you're actually looking for a module that lets you automate CLI interaction, there's pexpect

剩一世无双 2024-10-23 03:47:23

有很多图书馆可以做到这一点。

  1. Subprocess
  2. Pexpect
  3. Paramiko(最常用)
  4. Fabric
  5. Exscript

您可以查看他们的实现文档。

There are many libraries to do that.

  1. Subprocess
  2. Pexpect
  3. Paramiko (Mostly used)
  4. Fabric
  5. Exscript

You can check their documentation for the implementation.

余生共白头 2024-10-23 03:47:23

我将向您推荐:

import spur

shell = spur.SshShell(hostname="localhost", username="bob", password="password1", missing_host_key=spur.ssh.MissingHostKey.accept)
result = shell.run(["echo", "-n", "hello"])
print(result.output) # prints hello
import paramiko

hostname = 'enter_host'
username = 'enter_user'
password = 'enter_pass'

client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.connect(hostname, username=username, password=password)

stdin, stdout, stderr = client.exec_command('cd /enter/valid/path && ls -l')

for line in stdout:
    print(line.strip('\n'))

client.close()

输出:

-rw-rw-r-- 1 username username 2712 Jan 22 14:49 file_1.ini
-rw-rw-r-- 1 username username 5928 Jan 22 14:50 file_2.log
-rw-rw-r-- 1 username username 1454 Jan 14 23:31 file_3.sql
-rw-rw-r-- 1 username username  337 Jan 14 23:31 file_4.py
drwxrwxr-x 3 username username 4096 Jan 14 23:33 folder_1
drwxrwxr-x 2 username username 4096 Jan 14 23:33 folder_2
-rw-rw-r-- 1 username username 2566 Jan 21 12:38 file_5.md
-rw-rw-r-- 1 username username   63 Jan 14 23:31 file_6.txt
drwxrwxr-x 4 username username 4096 Jan 14 23:31 folder_3
drwxrwxr-x 9 username username 4096 Jan 21 12:38 folder_4
  • 无需库
import subprocess

subprocess.Popen("ssh {user}@{host} {cmd}".format(user=user, host=host, cmd='ls -l'), shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE).communicate()

I will refer you to:

import spur

shell = spur.SshShell(hostname="localhost", username="bob", password="password1", missing_host_key=spur.ssh.MissingHostKey.accept)
result = shell.run(["echo", "-n", "hello"])
print(result.output) # prints hello
import paramiko

hostname = 'enter_host'
username = 'enter_user'
password = 'enter_pass'

client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.connect(hostname, username=username, password=password)

stdin, stdout, stderr = client.exec_command('cd /enter/valid/path && ls -l')

for line in stdout:
    print(line.strip('\n'))

client.close()

output:

-rw-rw-r-- 1 username username 2712 Jan 22 14:49 file_1.ini
-rw-rw-r-- 1 username username 5928 Jan 22 14:50 file_2.log
-rw-rw-r-- 1 username username 1454 Jan 14 23:31 file_3.sql
-rw-rw-r-- 1 username username  337 Jan 14 23:31 file_4.py
drwxrwxr-x 3 username username 4096 Jan 14 23:33 folder_1
drwxrwxr-x 2 username username 4096 Jan 14 23:33 folder_2
-rw-rw-r-- 1 username username 2566 Jan 21 12:38 file_5.md
-rw-rw-r-- 1 username username   63 Jan 14 23:31 file_6.txt
drwxrwxr-x 4 username username 4096 Jan 14 23:31 folder_3
drwxrwxr-x 9 username username 4096 Jan 21 12:38 folder_4
  • No libraries required
import subprocess

subprocess.Popen("ssh {user}@{host} {cmd}".format(user=user, host=host, cmd='ls -l'), shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE).communicate()
扛刀软妹 2024-10-23 03:47:23

您可以使用 vassal 包,它正是为此设计的。

您所需要做的就是安装vassal并执行

from vassal.terminal import Terminal
shell = Terminal(["ssh username@host", "cd scripts", "python foo1.py", "python foo2.py"])
shell.run()

此操作,这将每秒运行该命令一次,并且您可以通过更改sec=0.1使其运行得更快。

You can use the vassal package, which is exactly designed for this.

All you need is to install vassal and do

from vassal.terminal import Terminal
shell = Terminal(["ssh username@host", "cd scripts", "python foo1.py", "python foo2.py"])
shell.run()

This will run the command once every second, and you can make it run faster to change sec=0.1.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文