Friday, July 9, 2021

Batch script to find out whether a host is up or down

 

For /f %%i in (testservers.txt) do ping -n 1 %%i >>state.txt

https://superuser.com/questions/733168/batch-script-to-find-out-whether-a-host-is-up-or-down

Sunday, December 13, 2020

Python sys.argv


import sys

if len(sys.argv) > 1:
print (f'\n Bilangan arguments: {len(sys.argv) - 1}')
print (f' Argument List: {str(sys.argv)}\n')
else:
print(f'\n Cuba lagi...')
print(f' Cara guna: {sys.argv[0]} <argument>\n')

Cth

Adli@CyberSec:~$ python adaketak.py

Cuba lagi...
Cara guna: adaketak.py <argument>

Adli@CyberSec:~$

Adli@CyberSec:~$ python adaketak.py saya cuba ya

Bilangan arguments: 3
Argument List: ['adaketak.py', 'saya', 'cuba', 'ya']

Adli@CyberSec:~$

Wednesday, November 4, 2020

Unix Bash Shell Script

..

my_string="A-B"
echo "Input: $my_string"
IFS='-' read -ra my_array <<< "$my_string"
len=${#my_array[@]}
for (( i=0; i<$len; i++ )); do
up=$(($i % 2))
#echo $up
if [ $up -eq 0 ]
then
echo "1. ${my_array[i]} = Cat 1"
elif [ $up -eq 1 ]
then
echo "2. ${my_array[i]} = Cat 2"
fi
done

..

$ ./cuba.sh
Input: A-B
1. A = Cat 1
2. B = Cat 2
$

Tuesday, November 3, 2020

VBA

VBA

Sub mula()
With Sheets("s1")
.Range("A1").AutoFilter Field:=2, Criteria1:="x"
With .AutoFilter.Range
With .Range("A1").Resize(.Rows.Count, 3)
With .SpecialCells(xlCellTypeVisible).EntireRow
.Copy
With Sheets.Add(after:=Sheets(Sheets.Count))
.Name = "Task XYZ"
.Paste
.[A1].Select
End With
With Sheets.Add(after:=Sheets(Sheets.Count))
.Name = "Group JKL"
.Paste
.[A1].Select
End With
End With
End With
.Range("a1").Offset(1).Resize(.Rows.Count - 1, 3).SpecialCells(xlCellTypeVisible).EntireRow.Delete
.AutoFilter
End With
End With
End Sub


sub test
with sheets("s1").cells(1).currentregion
.autofilter 2,"x"
.copy sheets("task xyz").cells(1)
.offset(1).entirerow.delete
.autofilter
end with
end sub

Tuesday, October 20, 2020

SSH_ASKPASS


$ man ssh
...
SSH_ASKPASS

If ssh needs a passphrase, it will read the
passphrase from the current terminal if it was run
from a terminal. If ssh does not have a terminal
associated with it but DISPLAY and SSH_ASKPASS are
set, it will execute the program specified by
SSH_ASKPASS and open an X11 window to read the
passphrase. This is particularly useful when call‐
ing ssh from a .xsession or related script. (Note
that on some machines it may be necessary to redi‐
rect the input from /dev/null to make this work.)
...

Keep in mind that ssh stands for secure shell, and if you store your user, host and password in plain text files you are misleading the tool an creating a possible security gap

$ cat ~/echo_pass
echo toor
$

$ cat ssh_session
export SSH_ASKPASS='~/echo_pass'
setsid ssh root@127.0.0.1

$ chmod u+x echo_pass
$ chmod u+x ssh_session

 ./ssh_session

$ ./ssh_session
Welcome to Ubuntu 18.04.5 LTS (GNU/Linux 4.15.0-121-generic x86_64)

operator@server:~$

https://stackoverflow.com/questions/1340366/how-to-make-ssh-receive-the-password-from-stdin

Monday, October 19, 2020

SSH_ASKPASS

 

#!/bin/bash

hostname="127.0.0.1"
username="u"
password="p"

command='ls -l'

ask="/tmp/ask"

echo "echo $password" > $ask
chmod +x $ask

SSH_OPTIONS='-o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null'

SSH_ASKPASS=$ask
DISPLAY=localhost:0.0

export DISPLAY
export SSH_ASKPASS

setsid ssh $SSH_OPTIONS $username@$hostname $command && echo '\n SSH OK\n' || echo '\nSSH PROBLEM\n'

Thursday, October 15, 2020

Python SSHClient invoke interactive shell

...

import threading, paramiko

class ssh:
shell = None
client = None
transport = None

def __init__(self, address, username, password):
print("Connecting to server on ip", str(address) + ".")
self.client = paramiko.client.SSHClient()
self.client.set_missing_host_key_policy(paramiko.client.AutoAddPolicy())
self.client.connect(address, username=username, password=password, look_for_keys=False)
self.transport = paramiko.Transport((address, 22))
self.transport.connect(username=username, password=password)

thread = threading.Thread(target=self.process)
thread.daemon = True
thread.start()

def closeConnection(self):
if(self.client != None):
self.client.close()
self.transport.close()

def openShell(self):
self.shell = self.client.invoke_shell()

def sendShell(self, command):
if(self.shell):
self.shell.send(command + "\n")
else:
print("Shell not opened.")

def process(self):
global connection
while True:
# Print data when available
if self.shell != None and self.shell.recv_ready():
alldata = self.shell.recv(1024)
while self.shell.recv_ready():
alldata += self.shell.recv(1024)
strdata = str(alldata, "utf8")
strdata.replace('\r', '')
print(strdata, end = "")
if(strdata.endswith("$ ")):
print("\n$ ", end = "")


sshUsername = ''
sshPassword = ''
sshServer = ''


connection = ssh(sshServer, sshUsername, sshPassword)
connection.openShell()
while True:
connection.sendShell('./script.sh')
command = input('$ ')
if command.startswith(" "):
command = command[1:]
connection.sendShell(command)

https://daanlenaerts.com/blog/2016/07/01/python-and-ssh-paramiko-shell/

Related post:

Related Posts with Thumbnails