Friday, September 10, 2021

Shell script with ps and kill

====Gotta love awk and shell!!!!======

# ps -ef | grep sshd
root      152725       1  0 Jul21 ?        00:00:46 sshd: /usr/sbin/sshd -D [listener] 0 of 10-100 startups
root      408824  152725  0 06:44 ?        00:00:00 sshd: root
root      408949  152725  0 06:44 ?        00:00:00 sshd: root
root      409031  152725  0 06:44 ?        00:00:00 sshd: root
root      409113  152725  0 06:44 ?        00:00:00 sshd: root
root      409194  152725  0 06:44 ?        00:00:00 sshd: root
root      409265  152725  0 06:44 ?        00:00:00 sshd: root
root      409266  152725  0 06:44 ?        00:00:00 sshd: root
root      409407  152725  0 06:44 ?        00:00:00 sshd: root
root      409478  152725  0 06:44 ?        00:00:00 sshd: root
root      409549  152725  0 06:45 ?        00:00:00 sshd: root
root      409630  152725  0 06:45 ?        00:00:00 sshd: root
root      409701  152725  0 06:45 ?        00:00:00 sshd: root
root      409785  152725  0 06:45 ?        00:00:00 sshd: root
root      410949  152725  0 18:56 ?        00:00:00 sshd: root@pts/0
root      411192  411077  0 19:15 pts/0    00:00:00 grep --color=auto sshd

# ps -ef | grep sshd | grep -v -e listener -e color -e pts | awk '{print $2}'

408824
408949
409031
409113
409194
409265
409266
409407
409478
409549
409630
409701
409785

# for pid in $(ps -ef | grep sshd | grep -v -e listener -e color -e pts | awk '{print $2}'); do kill -9  $pid; done

Friday, August 27, 2021

Cumulus....cumulus.....

net add interface swp11 link autoneg on

net add interface swp11 link speed 40000

ip link set dev swp3s0 up

net commit

sudo l1-show swp3s0

sudo cat /etc/lsb-release

sudo ethtool -m swp1

sudo net del interface swp3s0

sudo net add interface swp3 breakout 4x

====sometimes the breakout command doesnt work, then try to nano the ports.conf====

sudo nano /etc/cumulus/ports.conf

/etc/network/interfaces


Thursday, August 12, 2021

EVE with Wireshark remote capture

 Windows Registry Editor Version 5.00
[HKEY_CLASSES_ROOT\capture]
@="URL:UNetLab interface capture"
"URL Protocol"=""
[HKEY_CLASSES_ROOT\capture\shell]
[HKEY_CLASSES_ROOT\capture\shell\open]
[HKEY_CLASSES_ROOT\capture\shell\open\command]
@="\"C:\\Program Files\\Wireshark\\wireshark_wrapper.bat\" %1"

======wireshark_wrapper.bat file=============
========================================================
As long as you get putty works with key, this would be easy, plink will use default putty settings(key in my case) find out the plink file from putty package, and install wireshark with sshtcpdump (ssh remote capture)
=======================================================

@ECHO OFF
SET USERNAME="root"
SET PASSWORD="blablablabla"

SET S=%1
SET S=%S:capture://=%
FOR /f "tokens=1,2 delims=/ " %%a IN ("%S%") DO SET HOST=%%a&SET INT=%%b
IF "%INT%" == "pnet0" SET FILTER=" not port 22"

ECHO "Connecting to %USERNAME%@%HOST%..."

"C:\Program Files\PuTTY\plink.exe" -ssh -pw %PASSWORD% %USERNAME%@%HOST% "tcpdump -U -i %INT% -s 0 -w -%FILTER%" | "C:\Program Files\Wireshark\Wireshark.exe" -k -i -

Friday, July 23, 2021

Netplan config

sysadm@linuxtechi:~$ sudo vi /etc/netplan/00-installer-config.yaml

# This is the network config written by 'subiquity'

network:

  ethernets:

    enp0s3:

      dhcp4: true

  version: 2

Above are the default entries, which shows that interface “enp0s3” is getting the IP from DHCP server. As it is an yaml file, so while making the changes in the file we must follow correct indentation. Add the following lines to the yaml file,


network:

  ethernets:

    enp0s3:

      addresses: [192.168.1.3/24]

      gateway4: 192.168.1.1

      nameservers:

        addresses: [4.2.2.2, 8.8.8.8]

  version: 2

Friday, June 25, 2021

Shell to check process

=== Test if a port is open============
nc -z localhost 6000 || echo "no tunnel open"

====Cron table check================

We can use ps -aux | grep "<our command>" | sed '$ d' to check whether the connection is established or not. Based on this our script could be:

#!/bin/bash
SSH_COMMAND="ssh user@host -fTN -R 2222:127.0.0.1:22 -i $HOME/.ssh/id_rsa"
if [[ -z $(ps -aux | grep "$SSH_COMMAND" | sed '$ d') ]]
then exec $SSH_COMMAND
fi

Call this script my_autossh, place it in ~/bin and make it executable. Then run crontab -e and add the following job:

* * * * * $HOME/bin/my_autossh
If you do not want to use Cron, modify the scrip my_autossh in this way:
#!/bin/bash
SSH_COMMAND="ssh user@host -fTN -R 2222:127.0.0.1:22 -i $HOME/.ssh/id_rsa"
while true; do
    if [[ -z $(ps -aux | grep "$SSH_COMMAND" | sed '$ d') ]]
    then eval $SSH_COMMAND
    else sleep 60
    fi
done

And use nohup to push it into the background:
nohup my_autossh >/dev/null 2>&1 &

Linux KVM

 ======bridge========
brctl addif br0 p3p2
brctl addif br1 em1
ifdown br1
ifup br1

========create VM=======

Convert disk:
qemu-img convert -O qcow2 venice-disk1.vmdk psm.qcow2

Booting from an existing qcow2 image
virt-install \
  --name MyImportedVM \
  --description "Imported virtual machine" \
  --graphics vnc,listen=0.0.0.0 \
  --noautoconsole \  
  --os-type=linux \
  --memory 8192 \
  --vcpus=4 \
  --disk path=/var/lib/libvirt/images/importedVM.qcow2,bus=virtio\
  --boot hd \
  --network bridge:br0,model=virtio


Building a new Windows VM
Windows can be difficult on KVM because it doesn't ship with Virtio drivers. Using the non-virtio disk and network emulation is comparatively slow. Things are made harder because virt-install doesn't let you use --cdrom twice. To mount two iso files at once (so virtio drivers can be installed), use the --disk argument with device=cdrom. Also be sure to use bus=ide so Windows can read it before installing the virtio drivers.

The virtio drivers can be downloaded here

virt-install \
  --name Server2019 \
  --description "Windows Server 2019 Template" \
  --os-type=windows \
  --memory 8192 \
  --vcpus=4 \
  --disk path=/var/lib/libvirt/images/windows2019.qcow2,bus=virtio,size=30 \
  --disk /var/lib/libvirt/isos/WindowsServer2019StandardCore_1909.iso,device=cdrom,bus=ide \
  --disk /var/lib/libvirt/isos/virtio-win.iso,device=cdrom,bus=ide \
  --graphics vnc,listen=0.0.0.0 \
  --noautoconsole \
  --network bridge=br0,model=virtio


When installing the OS through your VNC viewer, no drives will show up.
Click the "Load Driver" button, and browse to the virtio iso that was mounted.
Expand the viostor folder and choose your OS. Click the amd64 subdirectory. Click OK.
RedHat VirtIO SCSI Controller should show up, click Next
Finish the install,Once the install finishes the server will turn off.


[root@localhost ~]# virsh console study01  
[root@localhost ~]# virsh start study01  
[root@localhost ~]# virsh shutdown study01  

#force stop
[root@localhost ~]# virsh destroy study01  

# destroy but keep the qcow2 virtual disk
[root@localhost ~]# virsh undefine study01  

[root@localhost ~]# virsh autostart study01  
[root@localhost ~]# virsh autostart --disable study01 
[root@localhost ~]# virsh suspend study01 
[root@localhost ~]# virsh resume study01 

virsh net-edit default
virsh net-edit somenet
virsh net-destroy somenet
virsh net-start somenet


virt-install   --name PSM   --description "PNSO PSM"   --graphics vnc,listen=0.0.0.0   --noautoconsole   --os-type=linux   --memory 8192   --vcpus=8   --disk path=/root/psm/psm.qcow2,bus=virtio  --boot hd   --network bridge:br0,model=virtio

Linux firewall related settings

============iptables============
#!/bin/bash
iptables -F
iptables -P INPUT DROP
iptables -P OUTPUT ACCEPT
iptables -A INPUT -i lo -j ACCEPT
iptables -A INPUT -i lan0 -j ACCEPT
iptables -A INPUT -i ens21f0 -j ACCEPT
iptables -A INPUT -i ens21f1 -j ACCEPT
iptables -A INPUT -p tcp -m tcp --dport 22 -j ACCEPT
iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT

=============NAT via firewall-cmd=================

vim /etc/sysctl.conf 
net.ipv4.ip_forward = 1
sysctl -p

#define zone
firewall-cmd --permanent --zone=external --change-interface=eth0 
firewall-cmd --permanent --zone=internal --change-interface=eth1

#set NAT masquerade
firewall-cmd --zone=external --add-masquerade --permanent

#NAT rules
firewall-cmd --permanent --direct --passthrough ipv4 -t nat -I POSTROUTING -o em1 -j MASQUERADE -s 172.16.10.0/24
firewall-cmd --reload

#Check:
[root@620 ~]# firewall-cmd  --direct --get-passthroughs ipv4 -t nat -I POSTROUTING -o em1 -j MASQUERADE -s 172.16.10.0/24