Skip to content

Commit 79353cf

Browse files
committed
update 4.3
add account autosave fix timeout
1 parent c9ffdf5 commit 79353cf

File tree

19 files changed

+201
-51
lines changed

19 files changed

+201
-51
lines changed

README.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@ Install from github
1515
```bash
1616
pip install git+https://github.com/damp11113/PyserSSH.git
1717
```
18-
## Optional Packages
19-
- [damp11113-library](https://github.com/damp11113/damp11113-library)
2018

2119
# Quick Example
2220
```py

setup.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,17 @@
55

66
setup(
77
name='PyserSSH',
8-
version='4.2.1', # update pypi (no update for 4.3)
8+
version='4.3',
99
license='MIT',
1010
author='damp11113',
1111
author_email='[email protected]',
1212
packages=find_packages('src'),
1313
package_dir={'': 'src'},
1414
url='https://github.com/damp11113/PyserSSH',
15-
description="A easy ssh server",
15+
description="python scriptable ssh server library. based on Paramiko",
1616
long_description=long_description,
1717
long_description_content_type='text/markdown',
1818
install_requires=[
1919
"paramiko"
20-
],
21-
extras_require={
22-
"fullsyscom": ["damp11113"]
23-
}
20+
]
2421
)

src/PyserSSH/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
"""
2-
PyserSSH - A SSH server. For more info visit https://github.com/damp11113/PyserSSH
2+
PyserSSH - A Scriptable SSH server. For more info visit https://github.com/damp11113/PyserSSH
33
Copyright (C) 2023-2024 damp11113 (MIT)
44
55
Visit https://github.com/damp11113/PyserSSH
@@ -60,7 +60,7 @@
6060
except:
6161
os.environ["pyserssh_log"] = "NO"
6262

63-
if os.environ["pyserssh_log"]:
63+
if os.environ["pyserssh_log"] == "NO":
6464
logger = logging.getLogger("PyserSSH")
6565
logger.disabled = True
6666

src/PyserSSH/account.py

Lines changed: 48 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
"""
2-
PyserSSH - A SSH server. For more info visit https://github.com/damp11113/PyserSSH
2+
PyserSSH - A Scriptable SSH server. For more info visit https://github.com/damp11113/PyserSSH
33
Copyright (C) 2023-2024 damp11113 (MIT)
44
55
Visit https://github.com/damp11113/PyserSSH
@@ -26,16 +26,45 @@
2626
"""
2727

2828
import pickle
29+
import time
30+
import atexit
31+
import threading
2932

3033
class AccountManager:
31-
def __init__(self, anyuser=False, historylimit=10):
34+
def __init__(self, anyuser=False, historylimit=10, autosave=False, autosavedelay=60, autoload=False, autoloadfile="autosave_session.ses"):
3235
self.accounts = {}
3336
self.anyuser = anyuser
3437
self.historylimit = historylimit
38+
self.autosavedelay = autosavedelay
39+
40+
self.__autosavework = False
41+
self.__autosaveworknexttime = 0
3542

3643
if self.anyuser:
3744
print("history system can't work if 'anyuser' is enable")
3845

46+
if autoload:
47+
self.load(autoloadfile)
48+
49+
if autosave:
50+
self.__autosavethread = threading.Thread(target=self.__autosave)
51+
self.__autosavethread.start()
52+
atexit.register(self.__saveexit)
53+
54+
def __autosave(self):
55+
self.save("autosave_session.ses")
56+
self.__autosaveworknexttime = time.time() + self.autosavedelay
57+
self.__autosavework = True
58+
while self.__autosavework:
59+
if int(self.__autosaveworknexttime) == int(time.time()):
60+
self.save("autosave_session.ses")
61+
self.__autosaveworknexttime = time.time() + self.autosavedelay
62+
63+
def __saveexit(self):
64+
self.__autosavework = False
65+
self.save("autosave_session.ses")
66+
self.__autosavethread.join()
67+
3968
def validate_credentials(self, username, password):
4069
if username in self.accounts and self.accounts[username]["password"] == password or self.anyuser:
4170
return True
@@ -66,11 +95,11 @@ def set_permissions(self, username, new_permissions):
6695
if username in self.accounts:
6796
self.accounts[username]["permissions"] = new_permissions
6897

69-
def save_to_file(self, filename):
98+
def save(self, filename="session.ssh"):
7099
with open(filename, 'wb') as file:
71100
pickle.dump(self.accounts, file)
72101

73-
def load_from_file(self, filename):
102+
def load(self, filename):
74103
try:
75104
with open(filename, 'rb') as file:
76105
self.accounts = pickle.load(file)
@@ -114,12 +143,24 @@ def get_user_sftp_path(self, username):
114143
def get_user_timeout(self, username):
115144
if username in self.accounts and "timeout" in self.accounts[username]:
116145
return self.accounts[username]["timeout"]
117-
return 0
146+
return None
118147

119-
def set_user_timeout(self, username, timeout=0):
148+
def set_user_timeout(self, username, timeout=None):
120149
if username in self.accounts:
121150
self.accounts[username]["timeout"] = timeout
122151

152+
def get_user_last_login(self, username):
153+
if username in self.accounts and "lastlogin" in self.accounts[username]:
154+
return self.accounts[username]["lastlogin"]
155+
return None
156+
157+
def set_user_last_login(self, username, ip, timelogin=time.time()):
158+
if username in self.accounts:
159+
self.accounts[username]["lastlogin"] = {
160+
"ip": ip,
161+
"time": timelogin
162+
}
163+
123164
def add_history(self, username, command):
124165
if not self.anyuser:
125166
if username in self.accounts:
@@ -158,4 +199,4 @@ def get_lastcommand(self, username):
158199
if username in self.accounts and "lastcommand" in self.accounts[username]:
159200
command = self.accounts[username]["lastcommand"]
160201
return command
161-
return None # User or history not found
202+
return None # User or history not found

src/PyserSSH/extensions/XHandler.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,30 @@
1+
"""
2+
PyserSSH - A Scriptable SSH server. For more info visit https://github.com/damp11113/PyserSSH
3+
Copyright (C) 2023-2024 damp11113 (MIT)
4+
5+
Visit https://github.com/damp11113/PyserSSH
6+
7+
MIT License
8+
9+
Permission is hereby granted, free of charge, to any person obtaining a copy
10+
of this software and associated documentation files (the "Software"), to deal
11+
in the Software without restriction, including without limitation the rights
12+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13+
copies of the Software, and to permit persons to whom the Software is
14+
furnished to do so, subject to the following conditions:
15+
16+
The above copyright notice and this permission notice shall be included in all
17+
copies or substantial portions of the Software.
18+
19+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25+
SOFTWARE.
26+
"""
27+
128
import inspect
229
import shlex
330

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
"""
2+
PyserSSH - A Scriptable SSH server. For more info visit https://github.com/damp11113/PyserSSH
3+
Copyright (C) 2023-2024 damp11113 (MIT)
4+
5+
Visit https://github.com/damp11113/PyserSSH
6+
7+
MIT License
8+
9+
Permission is hereby granted, free of charge, to any person obtaining a copy
10+
of this software and associated documentation files (the "Software"), to deal
11+
in the Software without restriction, including without limitation the rights
12+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13+
copies of the Software, and to permit persons to whom the Software is
14+
furnished to do so, subject to the following conditions:
15+
16+
The above copyright notice and this permission notice shall be included in all
17+
copies or substantial portions of the Software.
18+
19+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25+
SOFTWARE.
26+
"""
27+
28+
"""
29+
note
30+
31+
ansi cursor arrow
32+
up - \x1b[A
33+
down - \x1b[B
34+
left - \x1b[D
35+
right - \x1b[C
36+
37+
https://en.wikipedia.org/wiki/ANSI_escape_code
38+
"""

src/PyserSSH/extensions/dialog.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
"""
2-
PyserSSH - A SSH server. For more info visit https://github.com/damp11113/PyserSSH
2+
PyserSSH - A Scriptable SSH server. For more info visit https://github.com/damp11113/PyserSSH
33
Copyright (C) 2023-2024 damp11113 (MIT)
44
55
Visit https://github.com/damp11113/PyserSSH

src/PyserSSH/extensions/moredisplay.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
"""
2-
PyserSSH - A SSH server. For more info visit https://github.com/damp11113/PyserSSH
2+
PyserSSH - A Scriptable SSH server. For more info visit https://github.com/damp11113/PyserSSH
33
Copyright (C) 2023-2024 damp11113 (MIT)
44
55
Visit https://github.com/damp11113/PyserSSH

src/PyserSSH/extensions/processbar.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
"""
2-
PyserSSH - A SSH server. For more info visit https://github.com/damp11113/PyserSSH
2+
PyserSSH - A Scriptable SSH server. For more info visit https://github.com/damp11113/PyserSSH
33
Copyright (C) 2023-2024 damp11113 (MIT)
44
55
Visit https://github.com/damp11113/PyserSSH

src/PyserSSH/extensions/ptop.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
"""
2-
PyserSSH - A SSH server. For more info visit https://github.com/damp11113/PyserSSH
2+
PyserSSH - A Scriptable SSH server. For more info visit https://github.com/damp11113/PyserSSH
33
Copyright (C) 2023-2024 damp11113 (MIT)
44
55
Visit https://github.com/damp11113/PyserSSH

0 commit comments

Comments
 (0)