python su - oracle
import time
import paramiko
import re
def verification_ssh(host,username,password,port,oracle_pwd,cmd):
pattern=re.compile(r'.*\]\s*(\$\s*)$', re.S)
s=paramiko.SSHClient()
s.load_system_host_keys()
s.set_missing_host_key_policy(paramiko.AutoAddPolicy())
s.connect(hostname = host,port=int(port),username=username, password=password)
if username != 'oracle':
ssh = s.invoke_shell()
time.sleep(0.2)
ssh.send('su - oracle \n')
buff = ''
while not buff.endswith('Password: '):
resp = ssh.recv(9999)
buff +=resp
ssh.send(oracle_pwd)
ssh.send('\n')
buff = ''
while not pattern.match(buff):
resp = ssh.recv(9999)
buff +=resp
ssh.send(cmd)
ssh.send('\n')
buff = ''
while not pattern.match(buff):
resp = ssh.recv(9999)
buff +=resp
s.close()
result = buff
else:
stdin, stdout, stderr = s.exec_command(cmd)
result = stdout.read()
s.close()
return result
>>> (host,username,password,port,oracle_pwd,cmd)=('192.168.21.XX', 'chipengfei', 'XXXX', 22, 'YYYY', 'lsnrctl status')
>>> verification_ssh(host,username,password,port,oracle_pwd,cmd)
'lsnrctl status\r\n\r\nLSNRCTL for Linux: Version 10.2.0.5.0 - Production on 19-APR-2013 18:57:05\r\n\r\nCopyright (c) 1991, 2010, Oracle. All rights reserved.\r\n\r\nConnecting to (DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=192.168.21.14)(PORT=1521)))\r\nSTATUS of the LISTENER\r\n------------------------\r\nAlias LISTENER\r\nVersion TNSLSNR for Linux: Version 10.2.0.5.0 - Production\r\nStart Date 21-MAR-2013 22:42:02\r\nUptime 28 days 20 hr. 15 min. 2 sec\r\nTrace Level off\r\nSecurity ON: Local OS Authentication\r\nSNMP OFF\r\nListener Parameter File /oracle/product/10.2.0.5/network/admin/listener.ora\r\nListener Log File /oracle/product/10.2.0.5/network/log/listener.log\r\nListening Endpoints Summary...\r\n (DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=192.168.21.14)(PORT=1521)))\r\n (DESCRIPTION=(ADDRESS=(PROTOCOL=ipc)(KEY=EXTPROC0)))\r\nServices Summary...\r\nService "PLSExtProc" has 1 instance(s).\r\n Instance "PLSExtProc", status UNKNOWN, has 1 handler(s) for this service...\r\nService "st.anhui.wxxr.com.cn" has 1 instance(s).\r\n Instance "wxxrdb", status READY, has 1 handler(s) for this service...\r\nService "st_XPT.anhui.wxxr.com.cn" has 1 instance(s).\r\n Instance "wxxrdb", status READY, has 1 handler(s) for this service...\r\nService "wxxrdb" has 1 instance(s).\r\n Instance "wxxrdb", status UNKNOWN, has 1 handler(s) for this service...\r\nService "wxxrdb.anhui.wxxr.com.cn" has 1 instance(s).\r\n Instance "wxxrdb", status READY, has 1 handler(s) for this service...\r\nService "wxxrdbXDB.anhui.wxxr.com.cn" has 1 instance(s).\r\n Instance "wxxrdb", status READY, has 1 handler(s) for this service...\r\nThe command completed successfully\r\n[oracle@anhuidbs ~]$'
>>> print verification_ssh(host,username,password,port,oracle_pwd,cmd)
lsnrctl status
LSNRCTL for Linux: Version 10.2.0.5.0 - Production on 19-APR-2013 18:57:42
Copyright (c) 1991, 2010, Oracle. All rights reserved.
Connecting to (DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=192.168.21.14)(PORT=1521)))
STATUS of the LISTENER
------------------------
Alias LISTENER
Version TNSLSNR for Linux: Version 10.2.0.5.0 - Production
Start Date 21-MAR-2013 22:42:02
Uptime 28 days 20 hr. 15 min. 40 sec
Trace Level off
Security ON: Local OS Authentication
SNMP OFF
Listener Parameter File /oracle/product/10.2.0.5/network/admin/listener.ora
Listener Log File /oracle/product/10.2.0.5/network/log/listener.log
Listening Endpoints Summary...
(DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=192.168.21.14)(PORT=1521)))
(DESCRIPTION=(ADDRESS=(PROTOCOL=ipc)(KEY=EXTPROC0)))
Services Summary...
Service "PLSExtProc" has 1 instance(s).
Instance "PLSExtProc", status UNKNOWN, has 1 handler(s) for this service...
Service "st.anhui.wxxr.com.cn" has 1 instance(s).
Instance "wxxrdb", status READY, has 1 handler(s) for this service...
Service "st_XPT.anhui.wxxr.com.cn" has 1 instance(s).
Instance "wxxrdb", status READY, has 1 handler(s) for this service...
Service "wxxrdb" has 1 instance(s).
Instance "wxxrdb", status UNKNOWN, has 1 handler(s) for this service...
Service "wxxrdb.anhui.wxxr.com.cn" has 1 instance(s).
Instance "wxxrdb", status READY, has 1 handler(s) for this service...
Service "wxxrdbXDB.anhui.wxxr.com.cn" has 1 instance(s).
Instance "wxxrdb", status READY, has 1 handler(s) for this service...
The command completed successfully
[oracle@anhuidbs ~]$
import paramiko
import re
def verification_ssh(host,username,password,port,oracle_pwd,cmd):
pattern=re.compile(r'.*\]\s*(\$\s*)$', re.S)
s=paramiko.SSHClient()
s.load_system_host_keys()
s.set_missing_host_key_policy(paramiko.AutoAddPolicy())
s.connect(hostname = host,port=int(port),username=username, password=password)
if username != 'oracle':
ssh = s.invoke_shell()
time.sleep(0.2)
ssh.send('su - oracle \n')
buff = ''
while not buff.endswith('Password: '):
resp = ssh.recv(9999)
buff +=resp
ssh.send(oracle_pwd)
ssh.send('\n')
buff = ''
while not pattern.match(buff):
resp = ssh.recv(9999)
buff +=resp
ssh.send(cmd)
ssh.send('\n')
buff = ''
while not pattern.match(buff):
resp = ssh.recv(9999)
buff +=resp
s.close()
result = buff
else:
stdin, stdout, stderr = s.exec_command(cmd)
result = stdout.read()
s.close()
return result
>>> (host,username,password,port,oracle_pwd,cmd)=('192.168.21.XX', 'chipengfei', 'XXXX', 22, 'YYYY', 'lsnrctl status')
>>> verification_ssh(host,username,password,port,oracle_pwd,cmd)
'lsnrctl status\r\n\r\nLSNRCTL for Linux: Version 10.2.0.5.0 - Production on 19-APR-2013 18:57:05\r\n\r\nCopyright (c) 1991, 2010, Oracle. All rights reserved.\r\n\r\nConnecting to (DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=192.168.21.14)(PORT=1521)))\r\nSTATUS of the LISTENER\r\n------------------------\r\nAlias LISTENER\r\nVersion TNSLSNR for Linux: Version 10.2.0.5.0 - Production\r\nStart Date 21-MAR-2013 22:42:02\r\nUptime 28 days 20 hr. 15 min. 2 sec\r\nTrace Level off\r\nSecurity ON: Local OS Authentication\r\nSNMP OFF\r\nListener Parameter File /oracle/product/10.2.0.5/network/admin/listener.ora\r\nListener Log File /oracle/product/10.2.0.5/network/log/listener.log\r\nListening Endpoints Summary...\r\n (DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=192.168.21.14)(PORT=1521)))\r\n (DESCRIPTION=(ADDRESS=(PROTOCOL=ipc)(KEY=EXTPROC0)))\r\nServices Summary...\r\nService "PLSExtProc" has 1 instance(s).\r\n Instance "PLSExtProc", status UNKNOWN, has 1 handler(s) for this service...\r\nService "st.anhui.wxxr.com.cn" has 1 instance(s).\r\n Instance "wxxrdb", status READY, has 1 handler(s) for this service...\r\nService "st_XPT.anhui.wxxr.com.cn" has 1 instance(s).\r\n Instance "wxxrdb", status READY, has 1 handler(s) for this service...\r\nService "wxxrdb" has 1 instance(s).\r\n Instance "wxxrdb", status UNKNOWN, has 1 handler(s) for this service...\r\nService "wxxrdb.anhui.wxxr.com.cn" has 1 instance(s).\r\n Instance "wxxrdb", status READY, has 1 handler(s) for this service...\r\nService "wxxrdbXDB.anhui.wxxr.com.cn" has 1 instance(s).\r\n Instance "wxxrdb", status READY, has 1 handler(s) for this service...\r\nThe command completed successfully\r\n[oracle@anhuidbs ~]$'
>>> print verification_ssh(host,username,password,port,oracle_pwd,cmd)
lsnrctl status
LSNRCTL for Linux: Version 10.2.0.5.0 - Production on 19-APR-2013 18:57:42
Copyright (c) 1991, 2010, Oracle. All rights reserved.
Connecting to (DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=192.168.21.14)(PORT=1521)))
STATUS of the LISTENER
------------------------
Alias LISTENER
Version TNSLSNR for Linux: Version 10.2.0.5.0 - Production
Start Date 21-MAR-2013 22:42:02
Uptime 28 days 20 hr. 15 min. 40 sec
Trace Level off
Security ON: Local OS Authentication
SNMP OFF
Listener Parameter File /oracle/product/10.2.0.5/network/admin/listener.ora
Listener Log File /oracle/product/10.2.0.5/network/log/listener.log
Listening Endpoints Summary...
(DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=192.168.21.14)(PORT=1521)))
(DESCRIPTION=(ADDRESS=(PROTOCOL=ipc)(KEY=EXTPROC0)))
Services Summary...
Service "PLSExtProc" has 1 instance(s).
Instance "PLSExtProc", status UNKNOWN, has 1 handler(s) for this service...
Service "st.anhui.wxxr.com.cn" has 1 instance(s).
Instance "wxxrdb", status READY, has 1 handler(s) for this service...
Service "st_XPT.anhui.wxxr.com.cn" has 1 instance(s).
Instance "wxxrdb", status READY, has 1 handler(s) for this service...
Service "wxxrdb" has 1 instance(s).
Instance "wxxrdb", status UNKNOWN, has 1 handler(s) for this service...
Service "wxxrdb.anhui.wxxr.com.cn" has 1 instance(s).
Instance "wxxrdb", status READY, has 1 handler(s) for this service...
Service "wxxrdbXDB.anhui.wxxr.com.cn" has 1 instance(s).
Instance "wxxrdb", status READY, has 1 handler(s) for this service...
The command completed successfully
[oracle@anhuidbs ~]$
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/27042095/viewspace-758904/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- su - oracle報錯su: cannot set user id: Resource temporarily unavailableOracleAI
- su和su - 的區別
- 禁止su命令
- 【轉】linux下命令su與su - 的區別。Linux
- Linux下su與su -命令的本質區別Linux
- su和sudo命令
- Linux命令su、sudo、sudo su、sudo -i使用和區別Linux
- suse linux 10_su - oracle報ulimit錯誤-cannot modify limit:LinuxOracleMIT
- XIII Open Grodno SU Championship
- su - root could not open sessionSession
- 一次詭異的Oracle使用者無法su問題Oracle
- Linux基礎命令---suLinux
- su命令使用詳解(轉)
- su - xxx Could not create sessionSession
- su軟體怎麼安裝
- su和sudo命令的區別
- linux下su切換oracle使用者命令列前出現-bash-3.2$LinuxOracle命令列
- 解決“su: cannot open session: Permission denied”Session
- su - username -c執行多條命令
- Linux su與sudo的區別Linux
- su的時候報:could not open sessionSession
- Oracle中su切換進去sqlplus登入失敗的問題處理OracleSQL
- Linux 命令 su 和 sudo 的區別Linux
- su和sudo的區別與使用(轉)
- su: 改變圓的平滑度(邊數)
- Linux的wheel組:LINUX下使用者使用su命令切換使用者報錯su: Permission deniedLinux
- Linux– su和sudo 切換使用者Linux
- [20170705]理解linux su命令.txtLinux
- Android定製ROM,內嵌su和xposedAndroid
- Linux su命令和sudo命令的區別Linux
- linux精講——su切換使用者Linux
- vscode-di-ji-kuai-su-pei-zhiVSCodeAI
- Linux 出現 su: Authentication failure 解決辦法LinuxAI
- Linux基礎命令---切換使用者suLinux
- 深度解析!Linux 命令 su 和 sudo 的區別Linux
- 設定su和sudo為不需要密碼密碼
- Linux命令 切換使用者型別 suLinux型別
- Linux禁止非WHEEL使用者使用SU命令Linux