对上一篇文章的完善,本次实验允许ls,cat,tail,head命令(tail 只允许查看最后N行,不刷新)
整体思路:使用python 的paramiko模块登录服务器并完成命令的执行。服务器的登录可以使用ssh互信,也可以使用用户密码方式登录(本脚本使用的方法)。后者安全性太低,本次实战重配合gzexe加密方式使用。
**脚本使用方法:sh 1.sh**
注意点 t2.py~为gzexe加密t2.py后文件:gzexe t2.py && gzexe t2.py~ 后得到的文件。
脚本代码
t2.py
# -- coding: utf-8 --
import paramiko
def fun1():
''' 选择操作项 '''
print('\033[0;32;40m\t请选择想要执行的操作\033[0m')
print('\033[0;32;40m\t1:执行命令\033[0m')
print('\033[0;32;40m\t2:上传文件(暂未开放)\033[0m')
print('\033[0;32;40m\t3:退出\033[0m')
funA=input("请选择:")
if funA=='1':
fun2()
elif funA=='2':
fun4()
elif funA=='3':
fun4()
def fun2():
'''选择服务'''
print('\033[1;31;40m\t注意:您所能执行的命令只有 ls cat tail more less\033[0m')
print('\033[0;32;40m\t请选择想要操作的服务\033[0m')
print('\033[0;32;40m\t1:xxx\033[0m')
print('\033[0;32;40m\t2:xxx\033[0m')
print('\033[0;32;40m\t3:xxx\033[0m')
print('\033[0;32;40m\t4:xxx \033[0m')
print('\033[0;32;40m\t5:xxx \033[0m')
print('\033[0;32;40m\t6:xxx \033[0m')
print('\033[0;32;40m\t7:xxx \033[0m')
print('\033[0;32;40m\t8:xxx \033[0m')
print('\033[0;32;40m\t9:xxx \033[0m')
print('\033[0;32;40m\t10:xxx \033[0m')
print('\033[0;32;40m\t11:退出\033[0m')
n=input("请输入您的选择")
fun5(n)
def fun3():
pass
def fun4():
exit(0)
def fun5(n):
'''根据选择进入服务器,进入工作目录'''
server4 = '172.16.8.4'
###server4少得可怜的封建士大夫skpasswd4
passwd4 = r'xxx'
server107 = '172.16.8.107'
###server4少得可server118怜的封建士大夫skpasswd4server118skpasswd4
passwd107 = r'xxx'
server3 = '172.16.8.3'
###server4少得可server118怜的封建士大夫skpasswd4server118skpasswd4
passwd3 = r'xxx'
server118 = '172.16.8.118'
###skpaserver118server118sswd118少得可怜的封建士大夫skpasswd118skpasswd118skpasswd118skpasswd118
passwd118 = r'xxx'
if n == '1':
hostname = server118
port = 22
username = 'abc'
password = passwd118
cwd='/home/xxx/logs/' ##设定工作目录
send_cmd = 'cd ' + cwd + ';pwd'
send_cmd2 = 'ls -laht ' + cwd + ' |head -n12|tail -n 11|grep -v ^d ' ##显示当前目录下十个文件
# if n == '1':
# hostname = server107
# port = 9500
# username = 'abc'
# password = passwd107
# cwd = '/home/xxx/logs/'
# send_cmd='cd '+cwd+';pwd'
# send_cmd2='ls -laht '+cwd+' |head -n12|tail -n 11|grep -v ^d '
elif n in ['2', '3', '4', '5']:
hostname = server107
port = 9500
username = 'abc'
password = passwd107
if n == '2':
cwd = '/home/xxx/logs/'
elif n == '3':
cwd = '/home/xxx/logs/'
elif n == '4':
cwd = '/home/xxx/logs/'
elif n == '5':
cwd = '/home/xxx/logs/'
send_cmd = 'cd ' + cwd + ';pwd'
send_cmd2 = 'ls -laht ' + cwd + ' |head -n12|tail -n 11|grep -v ^d '
elif n in ['6', '7', '8']:
hostname = server4
port = 22
username = 'root'
password = passwd4
if n == '6':
cwd = '/home/xxx/logs/'
elif n == '7':
cwd = '/home/xxx/logs/'
elif n == '8':
cwd = '/home/xxx/logs/'
send_cmd = 'cd ' + cwd + ';pwd'
send_cmd2 = 'ls -laht ' + cwd + ' |head -n12|tail -n 11|grep -v ^d '
elif n in ['9', '10', '11']:
hostname = server3
port = 22
username = 'root'
password = passwd3
if n == '9':
cwd = '/home/xxx/logs/'
elif n == '10':
cwd = '/home/xxx/logs/'
elif n == '11':
cwd = '/home/xxx/logs/'
send_cmd = 'cd ' + cwd + ';pwd'
send_cmd2 = 'ls -laht ' + cwd + ' |head -n12|tail -n 11|grep -v ^d '
elif n == '12':
exit(0)
# 创建SSH对象
ssh = paramiko.SSHClient()
# 允许连接不在know_hosts文件中的主机
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
# 连接服务器
ssh.connect(hostname, port, username, password)
# 执行命令
stdin, stdout, stderr = ssh.exec_command(send_cmd)
# 获取命令结果
res, err = stdout.read(), stderr.read()
result = res if res else err
# 执行命令
stdin2, stdout2, stderr2 = ssh.exec_command(send_cmd2)
# 获取命令结果
res2, err2 = stdout2.read(), stderr2.read()
result2 = res2 if res2 else err2
print("您的当前目录是:"+result.decode())
print("您的当前文件有:" + result2.decode())
# print('\033[1;31;40m\t注意:您所能执行的命令只有 ls cat tail head\033[0m')
# send_cmd=input("请输入您的命令,要退出请输入 3 :")
a=fun6(cwd,result2.decode())
while a not in['3','exit','quit']:
# 执行命令
print('send_cmd+'+a)
stdin, stdout, stderr = ssh.exec_command(a)
# 获取命令结果
res, err = stdout.read(), stderr.read()
result = res if res else err
print(result.decode())
a=fun6(cwd,result2.decode())
def fun6(cwd,a2):
'''输入命令'''
print('fun6')
print('当前文件有:'+a2)
print('\033[1;31;40m\t注意:您所能执行的命令只有 ls cat tail head\033[0m')
send_cmd=input("请输入您的命令,要退出请输入 3 :")
print('当前cmd是:'+send_cmd)
s1=send_cmd.lstrip().split()
print('split后'+send_cmd)
print(s1)
if send_cmd in ['3','quit','exit']:
exit(0)
elif s1[0] not in [ 'ls', 'cat', 'tail', 'head','pwd']:
print("命令有误,请重新输入")
fun6(cwd)
elif s1[0]=='pwd':
print('当前命令是'+send_cmd)
return send_cmd
elif s1[0]=='ls':
if r'/' in send_cmd:
print('您无权查看其他目录的文件')
print('当前文件有:'+a2)
return 'ls -laht '+cwd+' |head -n12|tail -n 11|grep -v ^d '
elif len(s1)<2:
return 'ls -laht ' + cwd + ' |head -n12|tail -n 11|grep -v ^d '
else:
if '|' in send_cmd: ###命令中带过滤条件
i = send_cmd.index('|')
si = send_cmd[i:] ###命令中“|”后面的过滤条件
sib = send_cmd[:i] ###命令中过滤条件前面部分
sib2 = sib.split()[:-1]
# print("si:"+si)
# print("sib"+sib)
# print("sib2")
# print(sib2)
s333 = ' '.join(sib2)
# print(s333)
return s333 + " " + cwd +sib.split()[-1] + si
else:
return ' '.join(s1[:-1]) + ' ' + cwd + s1[-1]
else:
if len(s1)<2:
print('请务必要输入您需要查看的文件!!')
fun6(cwd)
elif r'/' in send_cmd:
print('您无权查看其他目录的文件,请务必只查看当前目录的文件')
print('当前文件有:'+a2)
return r'ls -laht '+cwd+' |head -n12|tail -n 11|grep -v ^d '
else:
if '|' in send_cmd:
i = send_cmd.index('|')
si = send_cmd[i:] ###命令中“|”后面的过滤条件
sib = send_cmd[:i] ###命令中过滤条件前面部分
sib2 = sib.split()[:-1]
# print("si:"+si)
# print("sib"+sib)
# print("sib2")
# print(sib2)
s333 = ' '.join(sib2)
# print(s333)
return s333 + " " + cwd +sib.split()[-1] + si
else:
return " " .join(s1[:-1]) + " " + cwd + s1[-1]
fun1()
##tail -n20 catalina.out |grep 2020|grep 9000
1.sh
gzexe -d t2.py~
sleep 1
/bin/bash 2.sh
2.sh
python3 t2.py~
gzexe t2.py
rm -rf t2.py~
mv t2.py~~ t2.py~
上图