Wednesday, February 01, 2023

Mapping Status Codes

 Here's a possible design for mapping status codes between two systems:

  1. Define a mapping table: Create a table that maps the internal status codes of the given system to the external status codes. This table should be easily accessible and modifiable.

  2. Receive status code: The system should receive a status code from the internal system and perform a lookup in the mapping table to determine the corresponding external status code.

  3. Error handling: If the received status code is not found in the mapping table, the system should return an error indicating that the status code is not recognized.

  4. Update mapping table: As the internal system evolves, new status codes may be added. The mapping table should be updated to include these new codes, ensuring that the mapping is up-to-date.

  5. Logging: The system should log the received status code, the corresponding external status code, and any errors encountered during the mapping process. This log will be useful for debugging and tracking the status code mapping process.

  6. Integration with external system: The external status code should be sent to the external system and used by that system to determine the status of the given system.

  7. Automated tests: The mapping process should be thoroughly tested, including testing the error handling and the correctness of the mapping table. Automated tests should be in place to ensure that the mapping process works as expected.

applescript that runs nmap command for a given port and ip

 Here's an example of an AppleScript that will run the nmap command to check the status of a specified port on a given IP address:


set target_ip to "192.168.1.100"

set target_port to "80"


do shell script "nmap -p " & target_port & " " & target_ip


This script sets two variables, target_ip and target_port, with the desired IP address and port to check. The do shell script command then runs the nmap command with the specified IP and port. The output of the nmap command will be displayed in the AppleScript Results window.


nmap on Mac OS

 Install it using brew and run the nmap command similar to Ubuntu.

It is possible to run a command on a MacOS terminal to check if a port is open on a Ubuntu 20.04 server. You can use the nmap tool to scan the server from the MacOS terminal.


Here is an example of how to check if port 80 is open on the Ubuntu 20.04 server with IP address 192.168.1.100:


nmap -p 80 192.168.1.100


The output will show the status of port 80 on the specified server, including whether it is open, closed, or filtered.

How to check if ports are open and active on Ubuntu 20.04 server?

 To check if ports are open and active on an Ubuntu 20.04 server, you can use the nmap tool.


Here is an example of how to check if a specific port is open on a server:


nmap -p [port_number] [server_ip_or_hostname]


For example, to check if port 80 is open on the server with IP address 192.168.1.100:


nmap -p 80 192.168.1.100


You can also check for a range of ports by specifying the range of port numbers separated by a hyphen:


nmap -p [port_start]-[port_end] [server_ip_or_hostname]


For example, to check if ports 80 to 90 are open on the server with IP address 192.168.1.100:


nmap -p 80-90 192.168.1.100


The output will show the status of the port, including whether it is open, closed, or filtered.

AppleScript that opens a given URL in the command line on a Chrome browser

Here is an example AppleScript that opens a given URL in the Chrome browser:


on run argv

  set url_string to item 1 of argv

  tell application "Google Chrome"

    activate

    open location url_string

  end tell

end run


You can save this script as an application and run it from the command line by passing in the URL as an argument. For example:


osascript /path/to/script.scpt "https://www.example.com"


This script activates the Chrome browser and opens the specified URL in a new tab.

 

What tasks can be automated using AppleScript to improve productivity of software engineers?

 AppleScript is a scripting language that can automate tasks and control applications on a Mac. Here are some examples of tasks that software engineers can automate using AppleScript to improve productivity:

  1. Building and running code: Automate the process of building and running code by creating a script that opens the terminal, changes to the project directory, and runs the necessary build and run commands.

  2. Automating testing: Automate the process of running tests by creating a script that opens the terminal, changes to the project directory, and runs the necessary test commands.

  3. Opening frequently used applications and files: Automate the process of opening frequently used applications and files by creating a script that launches multiple applications and opens specific files with a single command.

  4. Automating Git operations: Automate the process of performing Git operations such as committing changes, pushing to remote repositories, and checking the status of the repository by creating a script that runs Git commands in the terminal.

  5. Automating file organization: Automate the process of organizing and moving files by creating a script that sorts and moves files based on specified criteria, such as file type or creation date.

  6. Automating repetitive tasks: Automate repetitive tasks by creating a script that performs a set of actions, such as copying and pasting text or filling out a form, with a single command.

By automating these tasks, software engineers can save time and increase their productivity by reducing the amount of manual effort required to perform routine tasks.

AppleScript to SSH

Create a file called ssh_server_name.scpt in a desired folder with the contents:

do shell script "open -a Terminal /bin/bash ~/Documents/server.sh"

To create an icon that you can click to run an AppleScript file. Here are the steps to do it:

  1. Save the AppleScript file with a .scpt extension to your desired location.
  2. Right-click on an empty area of your Desktop and select "New" > "Script".
  3. A new file with a .scpt extension will be created on your Desktop.
  4. Open the newly created file in the AppleScript Editor.
  5. Copy and paste the contents of your original AppleScript file into this new file.
  6. Save the new file.
  7. Right-click on the newly created file and select "Get Info".
  8. In the Info window, click on the icon in the top left corner.
  9. Press "Cmd + V" to paste the copied icon, or select a new icon from your computer.
  10. Close the Info window.
  11. Your new script file should now have the custom icon you selected.

Now you can double-click the icon on your Desktop to run the script.






Script to SSH into Servers

 Here is an example of a bash script that uses the ssh command to connect to a remote server at a specified IP address and password:


#!/bin/bash


# define the variables for IP and password

ip="192.168.1.100"

password="mypassword"


# use the ssh command to connect to the server

sshpass -p "$password" ssh -o StrictHostKeyChecking=no user@$ip


This script uses the sshpass command to pass the password to the ssh command. The -p flag specifies the password, and the -o StrictHostKeyChecking=no option is used to disable host key checking, which allows the script to connect to the server without prompting for confirmation.


You can replace the "user" with the username you want to use to connect to the remote server and replace the IP address and password with the actual values you want to use.


It's important to note that, hardcoding the password in a script is not a good practice, as it can be easily accessed by someone with access to the script. You should consider using ssh-key based authentication or storing the password in an encrypted file, and decrypting it only when needed.


You should also be careful when running scripts like this, as it can pose a security risk if used improperly.


The script I provided uses the sshpass command, which is not installed by default on macOS. sshpass is a command-line utility that allows you to provide the password for the ssh command non-interactively. Therefore, the script will not work as is on macOS.


However, the script can be modified to work on macOS by using the -A flag of the ssh command, which enables agent forwarding. This allows your local ssh-agent to handle the ssh-authentication and forwards your ssh-key to the remote host. This way you don't need to provide the password.


#!/bin/bash


# define the variables for IP

ip="192.168.1.100"


# use the ssh command to connect to the server

ssh -A user@$ip


But this approach has its own security risks, for instance, it requires the ssh-agent to be running and your ssh-key to be added to the ssh-agent. Also, you should be sure that you trust the remote host and it's network, as the agent forwarding exposes your ssh-key to the remote host and its network.


You should also consider using ssh-key based authentication, which is more secure and eliminates the need to use a password or a script that passes the password.


Hardcoding the password in a script is generally not considered a good practice, as it can be easily accessed by someone with access to the script. If the script falls into the wrong hands, the password could be used maliciously.


Additionally, hardcoding the password in the script can also pose a security risk if the password is not changed frequently, or if the password is used for other purposes as well.


That being said, if you still want to run the script on Mac OS and you are comfortable with the security risks involved, you can use the expect command in the script to automatically enter the password when prompted by the ssh command.


Here is an example of how you could use expect to run the ssh command and provide the password automatically:


#!/bin/bash


# define the variables for IP and password

ip="192.168.1.100"

password="mypassword"


# use the expect command to connect to the server

expect -c "

    spawn ssh user@$ip

    expect \"password:\"

    send \"$password\r\"

    interact

"


This script uses the expect command to spawn the ssh command and wait for the "password:" prompt. When prompted, it sends the value of the password variable to the server. The interact command allows the user to interact with the shell once the password has been sent.


Keep in mind that this script still has security risks, as the password is visible in plaintext and anyone who has access to the script will be able to see it.


It's important to note that, hardcoding the password in a script is not a good practice, as it can be easily accessed by someone with access to the script. You should consider using ssh-key based authentication or storing the password in an encrypted file, and decrypting it only when needed.


Here is an example of a bash script that uses the expect command to run the ssh command and provide the password automatically on macOS:


#!/bin/bash


# define the variables for IP and password

ip="192.168.1.100"

password="mypassword"


# use the expect command to connect to the server

expect -c "

    spawn ssh user@$ip

    expect \"password:\"

    send \"$password\r\"

    interact

"


As you mentioned, you have accepted the security risk of having the password hardcoded in the script, for the purpose of logging from your development machine to a staging server. But, you should be aware that this script can pose a significant security risk if it is shared with others or if the server's security is compromised.


It's important to remember to keep the script in a secure location and to limit access to it to only those who need it. It is also important to change the password frequently and to use a different password for other services.


Also, consider disabling the script or removing the password when it is no longer needed.


As an extra security measure, you can also use a command like chmod 700 script.sh to change the permissions of the script to only allow you, the owner, to execute and read the script.


Also, you should be aware that logging and monitoring the usage of the script is important, as it can give you an idea of who, when and how it was used, this can help you detect any malicious activity or misuse.



Using grep UNIX Command

 The UNIX command to search for a string in a log file is grep.


The basic syntax for using grep to search for a string in a file is:


grep "string" file.log


Where "string" is the text you want to search for, and "file.log" is the name of the log file.


grep is a powerful command and has many options that you can use to customize your search, some useful options are:


-i : case-insensitive search

-v : invert the match, it shows the lines that doesn't match the string

-n : shows the line number where the match occurred

-c : shows the number of matches

-r : search recursively through all subdirectories


You can also use the grep command to search for a string in multiple files at once by specifying multiple file names separated by a space.


grep "string" file1.log file2.log file3.log


It's also possible to use regular expressions with grep command, this allows you to search for patterns in the log file and not just a specific string.


grep -E "pattern" file.log


It's recommended to use the man command to read the manual page of grep and learn more about its functionalities and options.


Compare bugsnag and honeybadger

 Bugsnag and Honeybadger are both error monitoring and management tools that help developers identify and resolve issues in their applications. However, there are some differences between the two:

  1. Error tracking: Both Bugsnag and Honeybadger track errors in real-time, but Honeybadger provides additional features such as detailed error reports and the ability to categorize errors into different projects.

  2. Notification and alerting: Both tools offer notifications via email, SMS, and other channels, but Honeybadger provides additional alerting options such as Slack and PagerDuty.

  3. Integrations: Bugsnag integrates with a wide range of platforms and tools, including popular frameworks such as Ruby on Rails and React, as well as services like GitHub and Jira. Honeybadger, on the other hand, offers a smaller set of integrations but provides a more flexible API for custom integrations.

  4. Pricing: Both Bugsnag and Honeybadger offer flexible pricing plans, but Bugsnag tends to be more expensive for high-volume usage.

  5. Support: Bugsnag provides a comprehensive support package with email, live chat, and telephone support, while Honeybadger offers email support and a knowledge base.

These are some of the key differences between Bugsnag and Honeybadger. The choice between the two will depend on the specific needs and requirements of your project and organization.

How to monitor background jobs?

 Here are some ways to monitor background jobs:

  1. Job management tools: There are many job management tools available that can monitor your background jobs, manage their execution, and send notifications in case of any issues. Some popular tools include Sidekiq, Resque, and Celery.

  2. Monitoring tools: You can use monitoring tools such as Nagios, New Relic, or Datadog to monitor your background jobs and receive notifications in case of any issues.

  3. Logging: You can log the output of your background jobs and check the logs regularly for any errors or unexpected results. This can be done by redirecting the output of your job to a file, or by using a logging library in your application.

  4. Job status tracking: You can track the status of your background jobs by storing the status in a database and periodically checking the status. This can be done by writing a custom script or using a job management tool.

  5. Health checks: You can implement health checks for your background jobs to monitor their status and receive notifications in case of any issues. This can be done by writing a custom script or using a monitoring tool.

These are some of the ways you can monitor your background jobs to ensure they are running correctly and alert you of any issues. The exact method you choose will depend on your specific needs and environment.

How to monitor CRON jobs?

 Here are some ways to monitor CRON jobs:

  1. Logging: You can log the output of your CRON jobs to a file and check the file regularly for any errors or unexpected results. This can be done by appending >> /path/to/logfile.log to the end of your CRON command.

  2. Email notifications: You can have the output of your CRON jobs sent to you via email by appending | mail -s "Cron job output" your@email.com to the end of your CRON command.

  3. Monitoring tools: There are many tools available that can monitor your CRON jobs and send notifications in case of any issues. Some popular tools include Monit, Supervisor, and New Relic.

  4. System logs: You can also monitor system logs to check for any errors or problems with your CRON jobs. On most systems, you can view the logs by running the cat /var/log/syslog command.

  5. Scripts: You can write a script to monitor your CRON jobs and send notifications in case of any issues. This script can be run on a regular basis as a CRON job itself.

These are some of the ways you can monitor your CRON jobs to ensure they are running correctly and alert you of any issues. The exact method you choose will depend on your specific needs and environment.

How to troubleshoot Too many connections error in MySQL?

The "Too many connections" error in MySQL occurs when the maximum number of connections to the MySQL server has been reached. Here are some steps to troubleshoot this error:


Check the maximum number of connections: You can check the maximum number of connections that MySQL is configured to allow by running the following SQL query:


SHOW VARIABLES LIKE 'max_connections';


Monitor current connections: You can see how many connections are currently open by running the following SQL query:


SHOW STATUS LIKE 'Threads_connected';


Increase the maximum number of connections: If the number of connections is close to the maximum, you can increase the maximum number of connections in the my.cnf file by changing the max_connections value.


Check for idle connections: Some connections may remain open but inactive. You can check for these connections by running the following SQL query:


SHOW PROCESSLIST;


Kill idle connections: If you have a large number of idle connections, you can kill them by running the following SQL query:


KILL [connection_id];


Check for slow queries: Slow running queries can also cause a high number of connections. You can check for slow queries by running the following SQL query:


SHOW SLOW QUERIES;


Optimize slow queries: If you find slow running queries, you can optimize them by using indexes, reducing the number of joins, and optimizing the SQL.


Connection pooling: If you are using connection pooling in your application, consider increasing the number of connections in the connection pool to reduce the number of new connections to the database.

Check if you can find the specific cause of the error.

    Tuesday, January 31, 2023

     def substitute(search, replace, text)

      text.gsub(search, replace)

    end


    text = "Hello World, this is a sample text."

    search = "sample"

    replace = "example"


    puts substitute(search, replace, text)

    # Output: "Hello World, this is a example text."

    def replace_keys_with_values(hash, text)

      hash.each do |key, value|

        text.gsub!(key, value)

      end

      text

    end

    replace_keys_with_values({ "key1" => "value1", "key2" => "value2" }, "key1 and key2")

    # returns "value1 and value2"

     

     

    In this example, the substitute method uses the gsub method, which stands for "global substitution", to replace all occurrences of the search string in the text input with the replace string. The gsub method returns a new string with the replacements, and this new string is then returned by the substitute method.

    Monday, January 23, 2023

    How to troubleshoot nil exceptions in Ruby on Rails codebase?

    Troubleshooting nil exceptions in a Ruby on Rails codebase can be a challenging task, but there are several strategies that can help you to find and fix the problem:

    1. Use a debugger: A debugger is a powerful tool that allows you to step through the code line by line and inspect the state of the variables at each step. This can help you to identify the line of code that is causing the exception and the variable that is returning nil.

    2. Check the stack trace: The stack trace is a list of the method calls that led to the exception. It can help you to identify the method that is causing the exception and the arguments that were passed to it.

    3. Check the logs: The Rails logs can provide more information about the exception and can help you to identify the request that caused the exception.

    4. Use a linter or static analysis tool: There are several linters and static analysis tools that can be used in a Ruby on Rails project, such as RuboCop, Brakeman, and Reek. These tools can help you to find potential issues and vulnerabilities in your codebase.

    5. Use the byebug gem: byebug is a powerful gem that provides an interactive debugger for Ruby. It can be used to check the state of the variables and navigate through the code.

    6. Use the binding.pry gem: pry is a powerful gem that provides an interactive shell for Ruby. It can be used to check the state of the variables and navigate through the code.

    7. Test your code: By writing tests and running them you can ensure that the code is working correctly and the test will catch any exception in the future.

    8. Check the documentation and the source code: Sometimes the exception is caused by a bug in a gem or a library that you are using. Check the documentation and the source code of the gem or library to see if there is a fix or a workaround.

    It's important to note that these are just some of the strategies that can be used to troubleshoot nil exceptions in a Ruby on Rails codebase and the approach that you choose will depend on the specific circumstances of the exception.

    Thursday, December 15, 2022

    TRIZ books

     1. Engineering of Creativity Introduction to TRIZ Methodology of Inventive Problem Solving (Semyon D. Savransky)

    2. TRIZ For Dummies (Lilly Haines-Gadd)

    3. Innovation AlgorithmTRIZ, systematic innovation and technical creativity (Genrich Altshuller)

    4. Lean TRIZ how to dramatically reduce product-development costs with this innovative problem-solving tool (Harrington, H. James)

    5. TRIZ for Engineers: Enabling Inventive Problem Solving (Karen Gadd)

    6. Advances and Impacts of the Theory of Inventive Problem Solving The TRIZ Methodology, Tools and Case Studies (Sebastian Koziołek, Leonid Chechurin etc

    7. Innovation on Demand New Product Development Using TRIZ (Victor Fey, Eugene Rivin)

    8. Inventive Thinking through TRIZ A Practical Guide, Second Edition (Michael A. Orloff)

    9. Modern TRIZ A Practical Course with EASyTRIZ Technology (Michael A. Orloff

    10. Modern TRIZ Modeling in Master Programs - Introduction to TRIZ Basics at University and Industry (Orloff, Michael A)

    11. Research and Practice on the Theory of Inventive Problem Solving (TRIZ) Linking Creativity, Engineering and Innovation (Leonid Chechurin)

    12. Simplified TRIZ New Problem Solving Applications for Technical and Business Professionals, 3rd Edition (Kalevi Rantanen, David W. Conley, Ellen R. Domb)

    13. Trimming, Miniaturization and Ideality via Convolution Technique of TRIZ A Guide to Lean and High-level Inventive Design (Saurabh Kwatra, Yuri Salamatov)

    14. TRIZ POWER TOOLS Job 1 Discovering Markets Carving out New Turf in Saturated Markets (Larry Ball and others

    15. TRIZ POWER TOOLS Job 2 Choosing Features What Features Will Excite the Target Market (Larry Ball and others

    16. TRIZ POWER TOOLS Skill 1 Resolving Contradictions The Skill that Will Give You the Confidence to Do the Rest

    17. TRIZ. Theory of Inventive Problem Solving Level 1 (Vladimir Petrov)

    18. Trizics Teach yourself TRIZ, how to invent, innovate and solve impossible technical problems systematically (Gordon Cameron)

    19. Triz the Right Solution at the Right Time A Guide to Innovative Problem Solving - Yuri Salamatov

    Monday, December 12, 2022

    Software

     Just like nature does not have any distinct fields like Physics, Chemistry, Biology etc. The software does not really have distinctions like front end, backend, machine learning etc. The examples used in this book will not have such arbitrary distinctions. So the concepts will be illustrated using devops such as Kubernetes. Because in reality the software is just software and the chosen example illustrate the principle really well. 

    The evolution might have occurred without being aware of these principles in action. The purpose of this book to bring those concepts from the realm of experts and make it available to anyone at any skill level.

    Trends of Technical Evolution

     Migration of function to super systems. Side car in Kubernetes was extracted into the Linux operating system. This is an example of the function migrating from system to super system over time. 

    Sunday, December 11, 2022

    pnpm

     Appended new lines to /Users/bparanj/.zshrc


    Next configuration changes were made:

    export PNPM_HOME="/Users/bparanj/Library/pnpm"

    export PATH="$PNPM_HOME:$PATH"


    To start using pnpm, run:

    source /Users/bparanj/.zshrc