Skip to main content

Posts

Showing posts from 2022

Unable to login to SSH even with a correct password

 In Linux world, SSH is a life. Especially, now with remote work, data center, servers without graphical interface. If you can't login via SSH,  even with a correct password but other user accounts is able to login. Then the person need to check with HR whether he or she is still on the payroll. :)  😄 Of course, glitch, errors and other issues will always be a part of this technology. Nothing is perfect in this world, so errors, technological issues will always be there it won't go away. So, what will will you do if you are sure that the SSH password and username is correct? Like you can login to Server 1, Server 2 but unable to login with Server 3 using the same credentials? If other users with sudo credentials are still able to login, the sudo account can be used to troubleshoot the specific SSH account that is having the issue. What would be the first thing to do? Well, joke as it seems that "restart works always" but the reality is not far from there. Restart SSH...

Create an admin account on Azure VM to reset forgotten password

How to create an administrator account on Azure VM? On Azure environment, username and password of the authorized account is needed in order to login or connect via Remote Desktop. If the administrator password or the password to RDP has been forgotten, then logging in to the VM will be an issue. How to resolve this kind of issue? PowerShell is a friend for this kind of dilemma. Code snippet below will create a user account, and set the created account as a member of the administrator group. # Create a user account   $username = "azure_admin_22" $password = ConvertTo-SecureString "Dynamic_Admin_User_2022" -AsPlainText -Force New-LocalUser -Name "$username" -Password $password -FullName "$username" -Description "User Description"   # Add the user to "Administrators" groups Add-LocalGroupMember -Group "Administrators" -Member azure_admin_22 User account that will be created is: azure_admin_22 with the pass...

Basic Ansible copy file/files to remote server

Copying files to remote server is needed at times, like deploying a new certificate or just some readme notes or other files that are just necessary in order to complete a task. Working with a single server, is not an issue. However, managing or deploying a file to a multiple servers could be daunting by doing it manually from server to server. Ansible is a good tool to copy files in one go, to multiple servers. Below is a sample code that would copy a file or group of files to specified servers. This can be a run as ansible adhoc playbook on an ansible node controller. {{ item }} <-- is the place holder for all file or files to be copied with_items <-- are the variables that will be placed on {{ item }} and it will be copied to the specified destination         with_items:           - test.crt           - server.crt           - filetest.pem #==========Ansible code below ---   ...

How to uncomment a conf file in Linux via ansible

How to uncomment or enable a setting in a config file in Linux using Ansible? Ansible is quite a good tool,  as it helps to make a lot of changes on a few servers with a minimal effort. Of course, the challenge is you need to know what you want. And also create an Ansible yaml file that will accomplish the task. Example YAML file for Ansible below is able to make some changes on the Linux logrotate.conf file. On Linux logrotate.conf, it has some text like the string below. Note this is just an example and you can tweak it to other settings as desired. # uncomment this if you want your log files compressed #compress The logrotate.conf file, the compress setting is commented. To enable the setting the # or the comment key must be removed. How to remove the comment in order to enable the setting? Well, if there's only a single server do it the simple way. Login to the server and edit the file using vim or nano or other text editor. However, if there are quite a few servers. Ansible ...

Linux SSH using Python and run remote commands

 SSH in Linux using Python and run commands on remote server A simple code snippet in Python to SSH to a remote system on a Linux Enviroment and run some commands Simple code snippet below, to get the serial number host on a remote system and save the output to the local host where the Python was executed. Python code snippet below is just a two-liner, that will SSH to a remote system and execute some commands and save the output locally. Save the output as "ssh_get_serial.py" or any filename as desired, and run as python3 ssh_get_serial.py. import subprocess  subprocess.Popen("ssh {user}@{host} {cmd}".format(user='root', host='server.web01.internal', cmd='s dmidecode -t system | egrep "Serial|Product";hostname > /tmp/server-sn-check.txt '), shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE).communicate() import subprocess <-- module to be imported in Python subprocess.Popen("ssh {user}@{host} {cmd}" {user} ...

How to burn iso to CD or DVD in using Windows command line

Burning ISO is quite common especially if you need a bootable disc to install a software to a laptop, desktop or a server. So, how to burn an ISO image in Windows without a 3rd party software? The answer is yes it is possible. In a Window 10 or Windows 11, laptop or device a USB DVD/CD burner is needed. Of course, a blank DVD or CD is needed as well. How to burn an ISO image in a Windows command line? Open a windows command prompt, press Windows Key + R this will open run box and type "cmd" and press enter. Or simply, search for CMD on Windows menu and click on it. On Windows command prompt type: cd c:\windows\syswow64 on a 64 bit architecture machine. on c:\windows\syswow64 type the full path where the ISO can be found. Example: c:\windows\syswow64\isoburn  VMware-VMvisor-Installer-7.0U3g-20328353.x86_64.iso  So, the syntax is: isoburn path-to-the-iso-file  the burner will be automatically detected by the CLI. Sample image below of the ISOBurn. Once the ISO burn opens, t...

PowerShell add text or string to text file without opening the file

How to add or append a string or any contents to a text file without opening the text file? Reading and writing in PowerShell is quite fundamental but very important. A simple PowerShell snippet code below, that will add or append a string to the text file dynamically. This is quite useful like monitoring a service, or pinging a server and adding the status or the result to a text file that can be checked later. Text file can be easily tampered, so proper permissions should be set who can write and read. So the file can be a source of truth, if ever auditing has to be done. Here's a simple PowerShell snippet that append a string to a text file or dynamically adding a string to a file. $contents_to_add=Get-Date Add-Content   -Path  "c:\temp\xx.rtf" -Value $contents_to_add #This will append a string to the existing content $file_content = get-content -Path  "c:\temp\xx.rtf" Write-Output $file_content  #Read the content of the files Just a short code but does a won...

Python Basics Read Text File and placed into variable

Python code below is a simple and basic code, that will open a text file in read only mode. Code below has been tested using Python3. It just demonstrates using a “print” statement to show the logic; how to open a file in read only mode and place the line that was read to a variable. The logic can be used on advance methods, such as replacing the line with IP Address and supplying a shutdown command to the IP Address. Here’s the code: #Open the file as Read Only with open('mysampledata.txt','r') as file_objHandler: #'mysampledata.txt' <--file name of the text file to be read #'r' <-- tell Python to open file as read only     #Read all lines in the file.     TxtLines = file_objHandler.readlines()     line_counter = 0   for Oneline in TxtLines:         #print(Oneline.strip())     line_counter += 1     print(line_counter, "<-- Line# / This is the data on...

Ping device using Python

Below is a simple Python code that accepts IP Address as input and check or ping the IP Address and shows an output whether its up or down. Some network disable ICMP request, so no matter how you try the system will not reply to the ping request. Code below is great when you're getting hands wet on simple Python script. Here's the code: #!/usr/bin/python3  import subprocess as subps  import sys  def ipcheck(check_ice):    status,result = subps.getstatusoutput("ping -c1 -w2 " + str(check_ice))    if status == 0:       print("Device " + str(check_ice) + " is UP!")       print(status,result)   else:       print("Device " + str(check_ice) + " is DOWN!") ipcheck(str(sys.argv[1]))  #================ Works great on Linux system. For Windows, run on WSL environment. Type: wsl to enable WSL Then change directory to: /mnt/c/Users/<username>/Documents  Or cd /mnt/c/<browse to wh...

Word document reduce size with lots of pictures

Did you ever find yourself, busy finalizing and creating a word document proposal only to find out that the document cannot be send via email? Why cannot send via email? The document size is too large to be attached and sent by email. Yes, this would happen if the document has quite a few high-quality resolution photos or pictures that are really good in quality. Photos or pictures that has high-quality resolution comes with its own draw back. The size of the photo is quite big. Luckily, for word document we can format the picture or photo and optimized it for email attachment or email sharing. If the document is viewed on the computer, the quality of the photo may not be noticeable. Of course, if the document is printed then the quality of the photo can be seen. So, how do we reduce the size of a photo that is inserted on a word document? First, let’s do a step-by-step process on how to insert a picture to a word document. I know this might be quite obvious if you have b...

Set or create a task scheduler using PowerShell or via command prompt

Task Scheduler in Windows or Cron in Linux is a life saver on the Sys Admin world. Why it is a life saver? You can set a scheduled task and forget about it. Of course, provided you have already carefully examined and test multiple times what will be the output when the Task is triggered. Yes, set and forget; once you are confident enough that everything will go smoothly. If there are task that needs to be run at midnight or early hours in the morning, Task Scheduler will come in handy. So, how do we set a Task Scheduler using PowerShell or Command Prompt? Why need to learn both? And not just PowerShell? Well, command prompt is always available on all Windows distribution. While PowerShell might be available on newer Windows system but then some restriction might be in place for security reasons. Therefore, it is good to be familiar with both PowerShell and Command Prompt executable files. PowerShell code snippet below shows how to set or create a Task Scheduler. PowerSh...

How to view save passwords on Microsoft Edge

Saving passwords in the browser is convenient. It will make your browsing smooth, and easy since every time you visit a site you don't need to fill-in or type your password. However, make sure that you are the only one using the computer or else if the computer is shared then everyone can use your password and they can access the websites in which your password is readily available to automatically fill-in every time the website is open on the browser. Yes, convenience and security sometimes doesn’t go together. It might be convenient to do, by saving password on the browser but you need to trade off security or if you don’t saved the password and you need to type every time your password, it might not be easy to do but it is more secured. And also saving password on the browser, would sometimes results to a forgotten password. So, if ever you browse the site in an incognito or private mode. Then you will need to type your password, or if you browse the site on another comput...

How to use hibernate in windows from command line?

Hibernate option in Windows is an option to shutdown any Windows laptop and after turning on again the laptop, all resources that was open before shutting down; such as word document, excel or other application are still available and running. The user doesn't need to re-open the applications. User  can continue where he or she has left off. Cool, feature! Using command line, the cli command shutdown.exe provides an option on how to shutdown a Windows machine to hibernate. The option I believe is available for all Windows edition even Server edition. Why do we need to use hibernate in Windows? Well, there are plenty of reasons. One example, if you have a few browser tabs open but then you need to shutdown the machine and don’t want to re-open all the browser tabs then hibernate is a good option. Or if you’re downloading something and you need to check whether download was completed, or just need to check whether there were some errors during the download operations then hibern...

Parallel Processing example using PowerShell

PowerShell version 7 has option to run tasks in Parallel.  This would mean that task that can be run in Parallel mode and tasks can be done simultaneously and save time.  How to get started or making use of Parallel in PowerShell?  First, PowerShell has to be in version 7.  Version 7 of PowerShell can be downloaded from Microsoft via this link below.  PowerShell 7 download. Choose the version, whether its x86 or x64.  The CPU architecture in your system will determine whether x64 PowerShell can be used or not.  Check out this link to check CPU architecture in your system:  Check if 64 bit processor or 32 bit Sample code below, shows how Parallel processing in PowerShell can be done.    $HostsIPs = '127.0.0.1','8.8.8.8','8.8.4.4','1.1.1.1'    $HostsIPs | ForEach-Object -Parallel {      Test-Connection $_      } -ThrottleLimit 5   The screenshot image below, shows that the commands indee...

Get accounts in O365 with no ATP or Defender license assigned

Advance Threat Protection aka ATP which is now called Microsoft Defender in office 365, is one of the licenses offered by Microsoft. If there are hundred accounts in O365, tracking which account that doesn't have ATP or Microsoft Defender license is just troublesome. Of course, PowerShell will come into rescue for this kind of issue. One liner code below in PowerShell will check which Office 365 accounts does not have ATP or Microsoft Defender license assigned. Get-MsolUser -All | Where-Object {$_.licenses.AccountSkuId -notcontains 'contoso:ATP_ENTERPRISE'} | Select-Object userprincipalname,licenses | export-csv c:\temp\office_365\no_defender_license.csv Replace contoso with your domain. Or run this command to see which licenses are assigned or available in your tenant. Get-MsolAccountSku | select -ExpandProperty ServiceStatus The PowerShell command checks which accounts does not have ATP assigned; which means that if you have 100 of guests or client user...