Skip to main content

Posts

Showing posts with the label SSH

Linux sudo revoke permissions

 sudo is quite a good tool in Linux world in which you can specify commands with full access without giving root permissions to everything on the system.   However, sudo can also be used to have access or root permissions to everything on the system.   Why use sudo, when it can access everything? Why not just use or switch to root?   When entering or executing a command, sudo is a safe haven to make sure you know exactly what you are doing or it can provide you a second chance to abort the command. If the command being entered isn't the right one, before entering the password or pressing enter "Ctrl + C" can be used to abort the command and stop the execution. When running on a root session, any commands executer after pressing enter. There's no way to cancel or abort the command. That's why sudo is a good practice to use, so any of change of mind before pressing enter can is still possible to cancel. And running in a non-root session, if the command is...

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} ...