Saturday, February 8, 2020

iterators in Python?

iterators in Python?

An iterator is an object that contains a countable number of values.
An iterator is an object that can be iterated upon, meaning that you can traverse through all the values.
Iterators are everywhere in Python. They are elegantly implemented within for loops, comprehensions, generators etc. but hidden in plain sight.
Iterator in Python is simply an object that can be iterated upon. An object which will return data, one element at a time.
Technically speaking, Python iterator object must implement two special methods, __iter__() and __next__(), collectively called the iterator protocol.
An object is called iterable if we can get an iterator from it. Most of built-in containers in Python like: listtuplestring etc. are iterables.
The iter() function (which in turn calls the __iter__() method) returns an iterator from them.

Tuesday, August 27, 2019

Differences between SSH and Telnet


SSH is more secure compared to Telnet
SSH encrypts the data while Telnet sends data in plain text
SSH uses a public key for authentication while Telnet does not use any authentication
SSH adds a bit more overhead to the bandwidth compared to Telnet
Telnet has been all but replaced by SSH in almost all uses
SSH and Telnet commonly serve the same purpose

SSH and Netstat tricks for linux users

ssh - secure shell runs on port 22 (by default).


The OpenSSH is the most common implementation of SSH, which allows you to do the same which telnet, FTP or rlogin can do. However, the advantage of using OpenSSH is its encryption technology implemented using algorithms such as DES, 3DES, Blowfish,etc. One thing you must be kept in mind that OpenSSH is not a protocol, it’s just a set of program that uses SSH protocol to improvise the ssh protocol features.



1. sudo netstat -i
This will show all the interfaces.

2.
To check if SSH service is running ?
rajesh@rajesh-laptop:~$ sudo netstat -tupln | grep ssh
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      29209/sshd


3.
rajesh@rajesh-laptop:~$ sudo netstat -tunp
Active Internet connections (w/o servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name   
tcp        0      0 192.168.1.115:33344     52.7.54.80:443          ESTABLISHED 2889/chrome --type=
tcp        0      0 127.0.0.1:55371         127.0.0.1:4369          ESTABLISHED 880/beam.smp       
tcp        0      0 192.168.1.115:39786     54.86.111.152:443       ESTABLISHED 2889/chrome --type=
tcp        0      0 192.168.1.115:48942     104.16.158.106:443      TIME_WAIT   -                 
tcp        0      0 192.168.1.115:47236     20.189.78.37:443        ESTABLISHED 2365/rajesh --pid=2
tcp        0      0 127.0.0.1:55269         127.0.0.1:4369          TIME_WAIT   -                 
tcp        0      0 192.168.1.115:51520     54.209.137.253:443      ESTABLISHED 2889/chrome --type=
tcp        0      0 192.168.1.115:44374     74.125.130.188:5228     ESTABLISHED 2889/chrome --type=
tcp        0      0 192.168.1.115:33760     52.114.75.78:443        ESTABLISHED 2365/rajesh --pid=2
tcp        0      0 192.168.1.115:39720     34.195.30.94:443        ESTABLISHED 2889/chrome --type=
tcp        0      0 192.168.1.115:58140     13.224.21.109:443       ESTABLISHED 2889/chrome --type=
tcp        0      0 192.168.1.115:47308     20.189.78.37:443        ESTABLISHED 2365/rajesh --pid=2
tcp        0      0 192.168.1.115:43078     178.128.122.9:443       TIME_WAIT   -                 
tcp        0      0 192.168.1.115:43772     13.224.17.218:443       ESTABLISHED 2889/chrome --type=
tcp        0      0 192.168.1.115:48432     34.225.241.187:443      ESTABLISHED 2889/chrome --type=
tcp        0      0 192.168.1.115:60824     172.217.167.197:443     ESTABLISHED 2889/chrome --type=
tcp        0      0 192.168.1.115:56912     151.101.120.84:443      ESTABLISHED 2889/chrome --type=
tcp6       0      0 127.0.1.1:4369          127.0.0.1:42783         TIME_WAIT   -                 
tcp6       0      0 127.0.0.1:4369          127.0.0.1:55371         ESTABLISHED 878/epmd           
tcp6       0      0 192.168.1.115:57081     13.75.106.0:443         ESTABLISHED 2634/Preload.js --b
udp        0      0 192.168.1.115:49999     74.125.130.189:443      ESTABLISHED 2889/chrome --type=
udp        0      0 192.168.1.115:41943     172.217.194.189:443     ESTABLISHED 2889/chrome --type=
udp        0      0 192.168.1.115:38174     216.58.200.174:443      ESTABLISHED 2889/chrome --type=
udp        0      0 192.168.1.115:36252     172.217.167.35:443      ESTABLISHED 2889/chrome --type=
udp        0      0 192.168.1.115:41715     172.217.167.46:443      ESTABLISHED 2889/chrome --type=


This show all the TCP and UDP connections with the program name and port

Friday, May 8, 2015

Netstat and ifconfig

I just learned a new way to get network interface information, while I am not a permitted SUDO user.


rajesh@linuxbox:~/code$ sudo ifconfig eth1
eth1      Link encap:Ethernet  HWaddr e0:69:95:88:cb:81
          inet addr:192.168.1.95  Bcast:192.168.1.255  Mask:255.255.255.0
          inet6 addr: fe80::e269:95ff:fe88:cb81/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:24502 errors:0 dropped:0 overruns:0 frame:0
          TX packets:26449 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:27824021 (26.5 MiB)  TX bytes:20140980 (19.2 MiB)
          Interrupt:20 Memory:fe400000-fe420000

rajesh@linuxbox:~/code$ netstat -ie
Kernel Interface table
eth1      Link encap:Ethernet  HWaddr e0:69:95:88:cb:81
          inet addr:192.168.1.95  Bcast:192.168.1.255  Mask:255.255.255.0
          inet6 addr: fe80::e269:95ff:fe88:cb81/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:24509 errors:0 dropped:0 overruns:0 frame:0
          TX packets:26455 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:27824607 (26.5 MiB)  TX bytes:20141704 (19.2 MiB)
          Interrupt:20 Memory:fe400000-fe420000

lo        Link encap:Local Loopback
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:65536  Metric:1
          RX packets:200 errors:0 dropped:0 overruns:0 frame:0
          TX packets:200 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0
          RX bytes:12624 (12.3 KiB)  TX bytes:12624 (12.3 KiB)

rajesh@linuxbox:~/code$

Tuesday, May 5, 2015

What are all the common undefined behaviors that a C++ programmer should know about?

Pointer

  • Dereferencing a NULL pointer
  • Dereferencing a pointer returned by a "new" allocation of size zero
  • Using pointers to objects whose lifetime has ended (for instance, stack allocated objects or deleted objects)
  • Dereferencing a pointer that has not yet been definitely initialized
  • Performing pointer arithmetic that yields a result outside the boundaries (either above or below) of an array.
  • Dereferencing the pointer at a location beyond the end of an array.
  • Converting pointers to objects of incompatible types
  • Using memcpy to copy overlapping buffers.

Buffer overflows

  • Reading or writing to an object or array at an offset that is negative, or beyond the size of that object (stack/heap overflow)

Integer Overflows

  • Signed integer overflow
  • Evaluating an expression that is not mathematically defined
  • Left-shifting values by a negative amount (right shifts by negative amounts are implementation defined)
  • Shifting values by an amount greater than or equal to the number of bits in the number (e.g. int64_t i = 1; i <<= 72 is undefined)

Types, Cast and Const

  • Casting a numeric value into a value that can't be represented by the target type (either directly or via static_cast)
  • Using an automatic variable before it has been definitely assigned (e.g., int i; i++; cout << i;)
  • Using the value of any object of type other than volatile or sig_atomic_t at the receipt of a signal
  • Attempting to modify a string literal or any other const object during its lifetime
  • Concatenating a narrow with a wide string literal during preprocessing

Function and Template

  • Not returning a value from a value-returning function (directly or by flowing off from a try-block)
  • Multiple different definitions for the same entity (class, template, enumeration, inline function, static member function, etc.)
  • Infinite recursion in the instantiation of templates
  • Calling a function using different parameters or linkage to the parameters and linkage that the function is defined as using.

OOP

  • Cascading destructions of objects with static storage duration
  • The result of assigning to partially overlapping objects
  • Recursively re-entering a function during the initialization of its static objects
  • Making virtual function calls to pure virtual functions of an object from its constructor or destructor
  • Referring to nonstatic members of objects that have not been constructed or have already been destructed

Source file and Preprocessing

  • A non-empty source file that doesn't end with a newline, or ends with a backslash (prior to C++11)
  • A backslash followed by a character that is not part of the specified escape codes in a character or string constant (this is implementation-defined in C++11).
  • Exceeding implementation limits (number of nested blocks, number of functions in a program, available stack space ...)
  • Preprocessor numeric values that can't be represented by a long int
  • Preprocessing directive on the left side of a function-like macro definition
  • Dynamically generating the defined token in a #if expression

To be classified

  • Calling exit during the destruction of a program with static storage duration

Tuesday, April 28, 2015

Memory Alignment

Hardware is complex; this is a simplified explanation.
A typical modern computer might have a 32-bit data bus. This means that any fetch that the CPU needs to do will fetch all 32 bits of a particular memory address. Since the data bus can't fetch anything smaller than 32 bits, the lowest two address bits aren't even used on the address bus, so it's as if RAM is organised into a sequence of 32-bit words instead of 8-bit bytes.
When the CPU does a fetch for a single byte, the read cycle on the bus will fetch 32 bits and then the CPU will discard 24 of those bits, loading the remaining 8 bits into whatever register. If the CPU wants to fetch a 32 bit value that is not aligned on a 32-bit boundary, it has several general choices:
  • execute two separate read cycles on the bus to load the appropriate parts of the data word and reassemble them
  • read the 32-bit word at the address determined by throwing away the low two bits of the address
  • read some unexpected combination of bytes assembled into a 32-bit word, probably not the one you wanted
  • throw an exception
Various CPUs I have worked with have taken all four of those paths. In general, for maximum compatibility it is safest to align all n-bit reads to an n-bit boundary. However, you can certainly take shortcuts if you are sure that your software will run on some particular CPU family with known unaligned read behaviour. And even if unaligned reads are possible (such as on x86 family CPUs), they will be slower.

Ref: http://stackoverflow.com/questions/1584267/understanding-word-alignment/1584282#1584282

Friday, April 24, 2015


This program prints "Empty String".



int a[]="\0";
if (printf("%s", a))
printf("Non Empty");
else
printf("Empty");


 


This is not an Empty String as size of the sting is 2 (Two).