1
- import json , math , hashlib , threading , os , subprocess , sys , time , numbers , base64 , binascii , random , requests
1
+ import json , math , hashlib , threading , os , subprocess , sys , time
2
+ import numbers , base64 , binascii , random , requests , paramiko
2
3
from psutil import Process
3
4
from datetime import datetime
4
5
from random import randrange
@@ -712,7 +713,7 @@ def r_input(self, command):
712
713
return self .nextPC ()
713
714
714
715
# 1 Load a plugin. This is done at compile time.
715
- # 2 Load text from a file
716
+ # 2 Load text from a file or ssh
716
717
def k_load (self , command ):
717
718
self .nextToken ()
718
719
if self .tokenIs ('plugin' ):
@@ -726,7 +727,15 @@ def k_load(self, command):
726
727
if symbolRecord ['hasValue' ]:
727
728
command ['target' ] = symbolRecord ['name' ]
728
729
if self .nextIs ('from' ):
729
- command ['file' ] = self .nextValue ()
730
+ if self .nextIsSymbol ():
731
+ record = self .getSymbolRecord ()
732
+ if record ['keyword' ] == 'ssh' :
733
+ command ['ssh' ] = record ['name' ]
734
+ command ['path' ] = self .nextValue ()
735
+ self .add (command )
736
+ return True
737
+
738
+ command ['file' ] = self .getValue ()
730
739
self .add (command )
731
740
return True
732
741
else :
@@ -735,15 +744,24 @@ def k_load(self, command):
735
744
736
745
def r_load (self , command ):
737
746
target = self .getVariable (command ['target' ])
738
- filename = self .getRuntimeValue (command ['file' ])
739
- try :
740
- with open (filename ) as f : content = f .read ()
741
- except :
742
- content = ''
743
- try :
744
- if filename .endswith ('.json' ): content = json .loads (content )
745
- except :
746
- RuntimeError (self .program , 'Bad or null JSON string' )
747
+ if 'ssh' in command :
748
+ ssh = self .getVariable (command ['ssh' ])
749
+ path = self .getRuntimeValue (command ['path' ])
750
+ sftp = ssh ['sftp' ]
751
+ try :
752
+ with sftp .open (path , 'r' ) as remote_file : content = remote_file .read ().decode ()
753
+ except :
754
+ RuntimeError (self .program , 'Unable to read data' )
755
+ else :
756
+ filename = self .getRuntimeValue (command ['file' ])
757
+ try :
758
+ with open (filename ) as f : content = f .read ()
759
+ except :
760
+ content = ''
761
+ try :
762
+ if filename .endswith ('.json' ): content = json .loads (content )
763
+ except :
764
+ RuntimeError (self .program , 'Bad or null JSON string' )
747
765
value = {}
748
766
value ['type' ] = 'text'
749
767
value ['content' ] = content
@@ -1277,16 +1295,40 @@ def r_send(self, command):
1277
1295
1278
1296
# Set a value
1279
1297
# set {variable}
1298
+ # set {ssh} host {host} user {user} password {password}
1280
1299
# set the elements of {variable} to {value}
1281
1300
# set element/property of {variable} to {value}
1282
1301
def k_set (self , command ):
1283
1302
if self .nextIsSymbol ():
1284
- target = self .getSymbolRecord ()
1285
- if target ['hasValue' ]:
1303
+ record = self .getSymbolRecord ()
1304
+ command ['target' ] = record ['name' ]
1305
+ if record ['hasValue' ]:
1286
1306
command ['type' ] = 'set'
1287
- command ['target' ] = target ['name' ]
1288
1307
self .add (command )
1289
1308
return True
1309
+ elif record ['keyword' ] == 'ssh' :
1310
+ host = None
1311
+ user = None
1312
+ password = None
1313
+ while True :
1314
+ token = self .peek ()
1315
+ if token == 'host' :
1316
+ self .nextToken ()
1317
+ host = self .nextValue ()
1318
+ elif token == 'user' :
1319
+ self .nextToken ()
1320
+ user = self .nextValue ()
1321
+ elif token == 'password' :
1322
+ self .nextToken ()
1323
+ password = self .nextValue ()
1324
+ else : break
1325
+ command ['host' ] = host
1326
+ command ['user' ] = user
1327
+ command ['password' ] = password
1328
+ command ['type' ] = 'ssh'
1329
+ self .add (command )
1330
+ return True
1331
+
1290
1332
return False
1291
1333
1292
1334
token = self .getToken ()
@@ -1415,6 +1457,17 @@ def r_set(self, command):
1415
1457
val ['content' ] = content
1416
1458
self .putSymbolValue (targetVariable , val )
1417
1459
return self .nextPC ()
1460
+
1461
+ elif cmdType == 'ssh' :
1462
+ target = self .getVariable (command ['target' ])
1463
+ host = self .getRuntimeValue (command ['host' ])
1464
+ user = self .getRuntimeValue (command ['user' ])
1465
+ password = self .getRuntimeValue (command ['password' ])
1466
+ ssh = paramiko .SSHClient ()
1467
+ ssh .set_missing_host_key_policy (paramiko .AutoAddPolicy ())
1468
+ ssh .connect (host , username = user , password = password )
1469
+ target ['sftp' ] = ssh .open_sftp ()
1470
+ return self .nextPC ()
1418
1471
1419
1472
# Shuffle a list
1420
1473
def k_shuffle (self , command ):
@@ -1485,6 +1538,12 @@ def r_split(self, command):
1485
1538
1486
1539
return self .nextPC ()
1487
1540
1541
+ def k_ssh (self , command ):
1542
+ return self .compileVariable (command , False )
1543
+
1544
+ def r_ssh (self , command ):
1545
+ return self .nextPC ()
1546
+
1488
1547
# Declare a stack variable
1489
1548
def k_stack (self , command ):
1490
1549
return self .compileVariable (command )
0 commit comments