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自己的配置網頁了。