Linux 跳过ssh和telnet 连接操作服务器
基于socket 的应答式
服务端
#!/usr/bin/env python
# -* - coding: UTF-8 -* -
import socket
import commands
class Rpcservice :
def __init__(self,ip,port):
self.ip = ip
self.port = port
def Fser(self , ip , port) :
sock = socket.socket ( socket.AF_INET , socket.SOCK_STREAM )
sock.bind ( (self.ip , self.port) )
sock.listen ( 1 )
while True :
conn , addr = sock.accept ()
while True :
r = conn.recv ( 4096 )
if r == 'Hi Siri' :
conn.send ( b"遇见对的人,请继续。" )
conn.send ( b"Yes" )
while True :
cmd = conn.recv ( 4096 )
if cmd == 'exit' :
conn.sendall ( "期待下次再见。" )
break
else :
status , output = commands.getstatusoutput ( cmd )
conn.sendall ( output )
if output is not None :
continue
conn.close ()
elif r != 'Hi Siri' and len ( r ) > 0 :
conn.send ( b"有内鬼,终止交易!!!" )
conn.send ( b"No" )
continue
else :
break
conn.close ()
if __name__ == "__main__" :
ip = "192.168.1.30"
port = 61888
rpcservice = Rpcservice ( ip , port )
rpcservice.Fser ( ip , port )
客户端
# -* - coding: UTF-8 -* -
import socket
class Rpcclent :
def __init__(self , ip , port) :
self.ip = ip
self.port = port
def Rclent(self , ip , port) :
# type: (object, object) -> object
sock = socket.socket ( socket.AF_INET , socket.SOCK_STREAM )
sock.connect ( (self.ip , self.port) )
while True :
dd = raw_input ( "请输入暗号,quit退出\n" )
if dd == 'quit' :
sock.close ()
break
else :
sock.sendall ( dd )
print sock.recv ( 4096 )
an = sock.recv ( 4096 )
if an == 'Yes' :
while True :
cmd = raw_input ( "请输入命令,exit退出\n" )
sock.sendall ( cmd )
print sock.recv ( 4096 )
if cmd == 'exit' :
sock.close ()
break
if __name__ == "__main__" :
ip = "192.168.1.30"
port = 61888
rpcclent = Rpcclent ( ip , port )
rpcclent.Rclent ( ip , port )