Skip to main content

Posts

Why won't react re-render on state change?

If you mutate the state directly it won't re-render. Instead of... [cart, setCart] = useState([1,2,3]) const updateCart = (item) => {   prevCart = cart   prevCart.push(item)   setCart(prevCart) } Do this.... const updateCart = (item) => {   let prevCart = []   cart.forEach(item => {       prevCart.push(item)   });   setCart(prevCart) }

Backup dockerized mongodb

### Backup mongo db container on docker host ``` docker run --rm --network [network] --link [db_container]:mongo -v /Users/sam/db_backups:/backup mongo bash -c 'mongodump -u [username] -p [password] --out /backup --host mongo:27017' ``` ### Restore mongodb database ``` docker run --rm --network [network] --link [db_container]:mongo -v /Users/sam/db_backups:/backup mongo bash -c 'mongorestore -u [mong_username] -p [mong_password] /backup --host mongo:27017' ``` The restore will wipe everything that existed before-hand.

Attach existing volume to container with docker-compose

`docker volume create my_vol` in docker-compose.yml, declare volumes with property `external: true`. This tells docker compose not to create volumes. If external = false, then docker compose will prefix the volume declared with the directory name. Example: /home   /sam     /docker       /my_app         - docker-compose.yml The volume name will end up being `my_app_my_vol` Example docker-compose.yml: ``` version: "3" volumes:   my_vol:     external: true services:   my_app:     volumes:       - my_vol:/data <-- attach volume to /data inside container ... ... ```

Setup net-snmp on Ubuntu 12.04

This is the bare minimum to set up snmp on Ubuntu to allow monitoring from Cacti or similar apt-get update apt-get install snmpd snmp-mibs-downloader snmpconf  - Choose /etc/snmp/snmpd.conf  - Choose snmpd.conf  - Choose 3 for 'Access Control'  - Choose 3 for RO community  - Type community name  - Press enter or type allowed network/host  - Press enter to allow all OID mv snmpd.conf /etc/snmp/ service snmpd restart

IPTables example

Allow tcp port 80 from source address 192.168.10/24 iptables -A INPUT -i 10 -P TCP -S 192.168.1.0/24 --dport 80 -j ACCEPT Drop tcp port 80 from range of addresses iptables -A INPUT -i 10 -p tcp --dport 80 -m iprange --src-range 192.168.1.1-192.168.1.254 -j DROP List chain with rule numbers iptables -nL INPUT -v --line-numbers Delete particular rule iptables -D INPUT 10

DHCP option 121

http://tools.ietf.org/html/rfc3442 This is used to add a classless  static route to the DHCP clients. To add option 121 to a Mikrotik DHCP server, it's value is specified in HEX. The format is as follows. 0xnnddddddddgggggggg where n=mask, d=destination, g=gateway. To convert ip address to HEX, you convert each octet, so 192=C0, 168=A8, 55=37, 1=01 You can use a tool such as  http://www.miniwebtool.com/ip-address-to-hex-converter/?ip=192.168.55.1 Example: To add a route to the destination network of 192.168.55.0/24 via gateway 172.16.10.1. /ip dhcp-server option add name=classlessroutes code=121 value=0x18C0A837AC100A01 where 18 is 24 in hex. *note: depending on the subnet mask, you may only need to specify 0-4 octets. In fact only the non-zero, or network portion of the subnet. Here is a table from the RFC. subnet mask Number of octets 0 0 1- 8 1 ...

Mikrotik Bridge Horizon

To achieve similar functionality to Cisco's private VLANS, where all ports are on the same L2 segment, but cannot exchange packets, you can use Mikrotik's Bridge Horizon feature. Basically, every port in a bridge is assigned a horizon value, and RouterOS will only forward frames to other interfaces in the bridge that have different horizon values. This means that you assign the same horizon value to the interfaces that you don't want to be able to communicate. For example, you want to bridge all your customers and use a single /24 subnet and the same gateway. Typically this is bad and poses a huge security risk, not to mention performance issues. If you assign the same horizon value to the customer interfaces, then the router will not forward traffic between customers. Customer A will not be able to ping Customer B. If you had a server, such as an IP-PBX that all customers needed to access, and you were lazy and added it to the bridge, then you would assign a diff...

Mikrotik NAT, access services via external IP from inside the network

The laptop at 192.168.1.10 wants to communicate with the web server on 192.168.1.10 via the external IP address of the Mikrotik router at 1.1.1.1. Say you have a service such as webmail, which has a nat rule to allow access from an external network... /ip firewall nat add chain=dstnat action=dst-nat dst-address=1.1.1.1 protocol=tcp dst-port=888 to-port=80 to-address=192.168.1.10 This works fine of course, so users set up the shortcut to http://1.1.1.1:888 Problem is when they are on the internal network it doesn't work, because the Mikrotik router won't send the reply data back out the same interface. A work-around is to create a src-nat rule directly below the dst-nat rule like this. /ip firewall nat add chain=srcnat action=masquerade src-address=192.168.1.0/24 dst-address=192.168.1.10 Goes something like this.. 1. Client initiates http request to 1.1.1.1:888 2. MT receives and translates destination to 192.168.1.10 as per 1st rule 3. MT then translates the...

Export from LDAP with ldapsearch example

This does an unauthenticated bind and exports the Framed IP Address from OpenLDAP '-x' simple, no SASL '-D' bind as '-w' password '-b' search base (starting point) '-s sub' search sub tree ldapsearch -x -LLL -D 'cn=admin,dc=domain,dc=net' -w password -b 'ou=users,dc=domain,dc=net' -s sub radiusFramedIPAddress >> /var/tmp/radiusUsers.txt

Mikrotik Dot Q-in-Q

How to create a Q-in-Q or 802.1ad double-tagged interface on a Mikrotik Router: Stripped down 802.1ad packet: DST-MAC | SRC-MAC | 802.1Q outer | 802.1Q inner |  PAYLOAD /interface vlan  add interface=ether1 name=outervlan vlan-id=600 add interface=outervlan name=innervlan vlanid=1500 The router will strip the outer vlan tag to expose the inner vlan. vlan 1500 is essentially tunnelled using the outer vlan (600) across the provider network.

MPLS Routing Loop

Certain destination IP addresses are not routable within certain parts of the network, and appear to loop.  This has been narrowed down to the label swap on a Cisco 6500 for the next hop of a GRE tunnel [point2point].  As per the LFIB, the Cisco 6500 is appending label 133 for network 10.10.32.1 and sending it out tun0, but appears to be snatching the packet back again, as it has a local label of 133 for the network 10.10.32.70/32 . ##Traceroute from test router (simplified for readability) 10.10.64.220 and 10.8.8.2 are c6500 interfaces, the latter being the GRE interface. [admin@rtr3] > /tool trace 10.10.32.1  # ADDRESS                                    STATUS                                   1 10.10.32.137      <MPLS:L=873,E=0>     **local-la...

BIND DNS server slave config on ubuntu

apt-get update apt-get install bind9 Edit named.conf.options as usual. Edit named.conf.local and add zone zone "test.net" {         type slave;         file "db.test.net";         masters { 172.16.99.200; }; }; where 172.16.99.200 is the master DNS server. The above relative syntax of the file will be /var/cache/bind/*. If you want to specify a particular location such as... zone "test.net" {         type slave;         file "/etc/bind/zones/db.test.net";         masters { 172.16.99.200; }; }; ...then you may need to modify permissions as well as apparmour to allow writing to the directory. chown -R root:bind /etc/bind/zones chmod -R 770 /etc/bind/zones nano /etc/apparmor.d/usr.sbin.named     add this line somewhere:   /etc/bind/slave/* rw, /etc/init.d/bind9 restart on the slave and check the zone di...

Link Nagios to root of website

By default nagios is accessible at http://nagiosserver/nagios3 . To have it accessible at http://nagiosserver, link the htdocs and stylesheet to /var/www (or wherever the root is located). rm -r /var/www ln -s /usr/share/nagios3/htdocs/ /var/www ln -s /etc/nagios3/stylesheets /var/www You can change /var/www to /var/www/whateeve r, so that it is accessible at http://nagiosserver/whatver

Make tftpd-hpa play nicely with Cisco

The following was tested on Ubuntu 11.10. This is completely insecure, make sure it is confined to a LAN environment, i.e. not publicly accessibly.  apt-get install tftpd-hpa mkdir /tftp chmod 777 /tftp nano /etc/default/tftpd-hpa # /etc/default/tftpd-hpa TFTP_USERNAME="tftp" TFTP_DIRECTORY="/tftp" TFTP_ADDRESS="0.0.0.0:69" TFTP_OPTIONS="--secure --create -v" service tftpd-hpa restart  --create is needed to allow client to create new file. Otherwise you will get error stating 'File does not exist' or similar. --secure is so that the client does not need to specify an absolute path such as /tftp/file. The root of the tftp server becomes TFTP_DIRECTORY. -v is for verbose logging in /var/syslog culv-lns1#copy run tftp Address or name of remote host []? 10.2.2.40 Destination filename [culv-lns1-confg]? !! 1446 bytes copied in 0.168 secs (8607 bytes/sec) _________________________________________________ ...

Backup OpenLDAP Ubuntu

The default database location is /usr/lib/ldap It is recommended to backup to LDIF so it is transportable. Use slapcat to export. slapcat -v -l /backups/ldapbackup.ldif To restore use slapadd. cd /usr/lib/ldap rm * slapadd -l ldapbackup.ldif -v is verbose output -l specifies the ldif format