Python中的OS特定命令 [英] OS specific command in Python

查看:82
本文介绍了Python中的OS特定命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想编写一个python程序并在其中调用特定于OS的命令。

所以基本上,不要输入命令行参数我想要

它在python程序中让它执行操作。


例如。在我的程序中,我想调用ssh功能,例如

命令行上的功能


ssh Ad***@192.168.2.10 ... ..etc


我怎样才能轻松做到这一点?


我们非常感谢您的帮助。


谢谢

解决方案

di ******** @ gmail.com écrit:

所以基本上,不要输入我想要的命令行参数
将它放在python程序中并让它执行操作。


例如,尝试标准库(IIRC)中的exec()和execfile()
。在我的程序中,我想调用ssh功能,如命令行上的

ssh Ad***@192.168.2.10 ..... etc




当您(通过ssh或telnet)连接到远程计算机时,您需要

类型(手动)

您的用户名和你的密码。编程绝非易事。


如果您需要的是模拟控制台交互,您将需要

获得期望。

Expect允许您根据工具命令语言(又名Tcl)在文本

控制台中编程/模拟用户交互。


但是有其他选择:启动一个写为shell的SSH守护进程

脚本。 (你只需要输入你的登录/传递一次,然后调用

守护进程执行一些命令)


恕我直言,期待是迄今为止最干净的方式。


问候,
$ b $bStéphane


di ******** @ gmail.com 写道:

我想写一个python程序和在其中调用操作系统特定的命令。
所以基本上,不是在命令行参数上输入我想要在python程序中输入它并让它执行操作。
例如。在我的程序中,我想调用ssh功能,如
在命令行上执行

ssh Ad***@192.168.2.10 ..... etc
<我怎么能这么容易地做到这一点?



http://www.python.org/doc/current/li...ubprocess.html


- < br $> b $ b Robert Kern


我开始相信整个世界都是一个谜,一个无害的谜团

可怕的是,我们疯狂地试图解释它,好像它有一个潜在的真相。

- Umberto Eco


di********@gmail.com 写道:< blockquote class =post_quotes>我想编写一个python程序并在其中调用特定于操作系统的命令。
所以基本上,不是在命令行参数上输入我希望
在python中使用它程序和文件它做了行动。


有几种方法可以这样做:

* os.system()如果你只是想发一个命令(那种火与忘记) )

*子进程模块,如果你想访问stdin,stdout和stderr

的已启动命令

。在我的程序中,我想调用ssh功能,如命令行上的

ssh Ad***@192.168.2.10 ..... etc




要与ssh进程交互,有一个很棒的模块叫做pexpect

(有一些如何在python中处理ssh进程的例子)
http://pexpect.sourceforge.net/

OT :paramiko是一个在python中处理SSH2协议的模块
http:// www .lag.net / paramiko /


I want to write a python program and call OS specific commands in it.
So basically, instead of typing in on the command line argument I want
to have it in a python program and let it do the action.

for example. in my program I would want to call the ssh feature like
one does on the command line

ssh Ad***@192.168.2.10 .....etc

How can I do this easily ?

Every help is appreciated.

Thanks

解决方案

di********@gmail.com a écrit :

So basically, instead of typing in on the command line argument I want
to have it in a python program and let it do the action.
Try exec() and execfile() from the standard library (IIRC)

for example. in my program I would want to call the ssh feature like
one does on the command line

ssh Ad***@192.168.2.10 .....etc



When you connect (via ssh or telnet) to a remote machine, you need to
type (manually)
your username and your password. Programming that is never easy.

If what you need is simulating console interaction, you will need to
get Expect.
Expect allows you to program/emulate user interaction in a text
console, based on the Tool Command Language (a.k.a. Tcl).

But there are alternatives : start a SSH daemon written as a shell
script. (you just have to enter your login/pass once, then call the
daemon to execute some commands)

IMHO, Expect is by far the cleanest way.

Regards,
Stéphane


di********@gmail.com wrote:

I want to write a python program and call OS specific commands in it.
So basically, instead of typing in on the command line argument I want
to have it in a python program and let it do the action.

for example. in my program I would want to call the ssh feature like
one does on the command line

ssh Ad***@192.168.2.10 .....etc

How can I do this easily ?



http://www.python.org/doc/current/li...ubprocess.html

--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
-- Umberto Eco


di********@gmail.com wrote:

I want to write a python program and call OS specific commands in it.
So basically, instead of typing in on the command line argument I want
to have it in a python program and let it do the action.
There are several ways to do so :
* os.system() if you just want to launch a command (kind of fire-and-forget)
* the subprocess module if you want to access stdin, stdout and stderr
of the launched command

for example. in my program I would want to call the ssh feature like
one does on the command line

ssh Ad***@192.168.2.10 .....etc



To interact with an ssh process , there is a great module called pexpect
(featuring some examples of how to handle ssh process in python)
http://pexpect.sourceforge.net/
OT : paramiko is a module to handle the SSH2 protocol in python
http://www.lag.net/paramiko/


这篇关于Python中的OS特定命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆