A. linux环境下,用python脚本远程登录到其他机器执行命令,如果将命令执行的结果返回到当前的机器
result=os.popen("ssh A; grep -r hello ./* ")
print("".join(result))
B. 如何使用python远程登录一个操作系统,并执行某条命令
你可以使用python的pexcpct包通过ssh调用远程服务器指令:
import pxssh
import getpass
try:
s = pxssh.pxssh()
hostname = raw_input('hostname: ')
username = raw_input('username: ')
password = getpass.getpass('password: ')
s.login (hostname, username, password)
s.sendline ('uptime') # run a command
s.prompt() # match the prompt
print s.before # print everything before the propt.
s.sendline ('ls -l')
s.prompt()
print s.before
s.sendline ('df')
s.prompt()
print s.before
s.logout()
except pxssh.ExceptionPxssh, e:
print "pxssh failed on login."
print str(e)
C. 如何利用Ansible在远程机器上执行python脚本
Ansible用来做配置管理是极好的, 但是有时配置完成后会需要基于环境做一些验证工作, 用好pip, script这些模块, 其实我们还是可以很方便的做到上面的事情的.
本文是作者在实践时遇到的一些坑以及对应的解决办法, 记录以供参考.
为了顺利的让我们的python脚本在远程环境上运行起来, 我们需要大致做下面几件事:
D. python 如何远程连接内网的mysql数据库
1、进入mysql,创建一个新用户test:
格式:grant 权限 on 数据库名.表名 用户@登录主机 identified by "用户密码";
grant all privileges on *.* to [email protected] identified by "123456";
或者
grant select,update,insert,delete on *.* to [email protected] identified by "123456";
2、 ./mysqladmin -uroot -ppwd reload
记住:对授权表的任何修改都需要重新reload
这时我们应该可以从192.168.0.2来远程管理192.168.0.1的数据库了
下面就是该脚本radius.py,其中出现三个日期:10天以后的日期future、今天的日期now、用户到期时间userdate,如果userdate <= future 并且 userdate >= now,那么向radreply表中插入一行,向用户提示到期时间,及时缴费;如果userdate < now,那么将该用户的状态设为停机,不允许其再登陆。
#! /usr/local/python/bin/python
# -*- coding: UTF-8 -*-
#引入模块
import MySQLdb
import datetime
#格式化日期,只有相同格式的日期才能进行比较
future = (datetime.date.today() + datetime.timedelta(10)).strftime("%Y-%m-%d")
now = (datetime.date.today()).strftime("%Y-%m-%d")
#这里就是连接远端数据库了
conn = MySQLdb.connect (host = "192.168.0.1",
user = "test",
passwd = "123456",
db = "radius")
cursor = conn.cursor ()
cursor.execute ("SELECT login_name,id,last_date FROM customer where last_date!='' and type='包月' and status='开通'")
E. 如何使用python执行远程shell脚本
最近有个需求就是页面上执行shell命令,第一想到的就是os.system,
代码如下:
os.system('cat /proc/cpuinfo')
但是发现页面上打印的命令执行结果 0或者1,当然不满足需求了。
尝试第二种方案 os.popen()
代码如下:
output = os.popen('cat /proc/cpuinfo')
print output.read()
通过 os.popen() 返回的是 file read 的对象,对其进行读取 read() 的操作可以看到执行的输出。但是无法读取程序执行的返回值)
尝试第三种方案 commands.getstatusoutput() 一个方法就可以获得到返回值和输出,非常好用。
代码如下:
(status, output) = commands.getstatusoutput('cat /proc/cpuinfo')
print status, output
Python Document 中给的一个例子,
代码如下:
>>> import commands
>>> commands.getstatusoutput('ls /bin/ls')
(0, '/bin/ls')
>>> commands.getstatusoutput('cat /bin/junk')
(256, 'cat: /bin/junk: No such file or directory')
>>> commands.getstatusoutput('/bin/junk')
(256, 'sh: /bin/junk: not found')
>>> commands.getoutput('ls /bin/ls')
'/bin/ls'
>>> commands.getstatus('/bin/ls')
'-rwxr-xr-x 1 root 13352 Oct 14 1994 /bin/ls'
F. 如何远程登录Linux机器并运行Python程序
新手对于没有图形界面的linux远程登录及其操作都充满畏惧。这里介绍一个简单的软件。叫作BitViz。简称BV for short。
一、软件安装
这里使用Putty的一个client软件叫作Bv SshClient. 你可以在putty的官网上找到其链接。
Bitvise Tunnelier
Tunnelier is an SSH and SFTP client for Windows. It is developed and supported professionally by Bitvise. Tunnelier is robust, easy to install, easy to use, and supports all features supported by PuTTY, as well as the following:
graphical SFTP file transfer; 图形界面最喜欢
single-click Remote Desktop tunneling;
auto-reconnecting capability;
dynamic port forwarding through an integrated proxy;
an FTP-to-SFTP protocol bridge. 方便的上传下载和删除操作。
Tunnelier is free for personal use, as well as for indivial commercial use inside organizations. You can download Tunnelier here. ownload-area
二、熟悉窗口
下面一个例子,是找到python软件安装位置的演示:
1. 首先,我进入到root
步骤一,进入root
2. 输入/home
步骤二,进入home
3. 点击其中子文件夹,即可找到,方便了。
步骤三,找到python文件夹
三、easy_install python library
Example Of python-setuptools Being Installed:
[root@server ~]# yum install python-setuptools
Yum Command To Install python-setuptools-devel:
[root@server ~]#yum install python-setuptools-devel
在linux 下: 使用方法非常简单,在命令行输入“easy_install 参数”即可。
这比我想象的要方便很多!在windows里,我要cmd-cd & easy_install flickrapi
在ssh的命令窗口,只需输入 easy_install flickrapi
如下图:
easy_install flickrapi
四、run python script as a background process in linux
So, you have a server to which you connect remotely, upload a python script and want to run it and logout from the server keeping the program running. If you frequently work with spiders, you surely want to do it. But how to do it? For example if your script's name is script.py, then the command is:
[root@server ~]# nohup python script.py &
And sometimes you may be interested to see the output is that being generated. Then you should view the nohup.out file! This command can be useful:
[root@server ~]# tail -f nohup.out
看看效果!
G. 如何使用python执行远程shell脚本
可以使用Python的Fabric包来完成这项任务。
既然是谈到Shell脚本,系统应该是Linux/Unix的,远程访问,应该是ssh吧。
Fabric功能是将一个任务通过ssh在多台服务器上执行,而每个任务可以是单条shell指令或是一段python脚本。
Fabric是将Python, Shell和SSH的功能很优雅地结合在了一起,同时自身又非常的轻量,适合大部分服务器群的日常管理工作。
H. 怎么用python登录windows系统
# -*- coding:utf-8 -*-
#! python2
import wmi
def sys_version(ipaddress, user, password):
conn = wmi.WMI(computer=ipaddress, user=user, password=password)
for sys in conn.Win32_OperatingSystem():
print "Version:%s" % sys.Caption.encode("UTF8"),"Vernum:%s" % sys.BuildNumber #系统信息
print sys.OSArchitecture.encode("UTF8") # 系统的位数
print sys.NumberOfProcesses # 系统的进程数
if __name__ == '__main__':
sys_version(ipaddress="ip", user="用户名", password="密码")
I. python 自动登录CSDN的脚本怎么实现
http://docs.python-requests.org/en/latest/
用python requests库就好了,然后使用库里的session
再开个Chrome浏览器打开CSDN登录页面,打开 开发者工具,到网络标签,然后正常登录一次,在开发者工具窗口网络标签下会有发请求的记录,找到登录请求,然后照发就可以了
https://passport.csdn.net/account/login?username=&password=
注意可能需要一些Header添加,比如:
Accept:
text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Content-Type:
application/x-www-form-urlencoded
Origin:
https://passport.csdn.net
Referer:
https://passport.csdn.net/account/login
User-Agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2062.124 Safari/537.36
用requests session登录完后,就可以继续用这个session访问csdn自己的配置网页了。