How to open a port, say 3389, in iptables to allow incoming traffics to it? There are several cases for this question: ipv4 or ipv6 or both, TCP or UDP or both and which interface? For simplicity, I give commands to allow all (ipv4 and ipv6, TCP and UDP from all interfaces) using port 3389
Read more
Category: QA
Questions and answers.
How to divert connection or packet before routing decision entering the default
Posted onbefore the packets ports (443) entering the firewall i would like to divert incoming packet of server (443) to input chain goes to FORWARD chain<br> so thats the incoming packets 100.43.xx.xx –sport 443 are send it to FORWARD instead of input chain<br> how to configure this in OUTPUT,FORWARD,POST AND PREROUTING CHAIN and this is my
Read more
How to check whether a file of a given path is a block device in Python?
Posted onHow to check and test whether a file of a given path is a block device in Python? This can be Linux specific. You can use the os.stat() function to get the stat of the path. Then use the stat.S_ISBLK() function against the stat’s .st_mode to test whether it is a block device. An example:
Read more
How to get the full path and directory of a Python script itself?
Posted onIn a Python script, how to get the full path and directory of the Python script itself? To get the path of the current file (the script itself), you can use __file__. To resolve any symbolic links in the path, you can use os.path.realpath(). Putting them together, you can do os.path.realpath(__file__) to get the full
Read more
How to test a file or directory exists in C++?
Posted onHow to test a path (a file or directory exists) in C++? In Bash, the [ -f ] and [ -d ] tests can test whether a file or a directory exist. What are the corresponding C++ ways for these tests? To test whether a file or dir (a path) exists, you may call stat()
Read more
How to turn off WiFi and bluetooth easily in iOS 11?
Posted onThe WiFi and bluetooth icons in iOS 11’s control center only disconnect the WiFi and Bluetooth connection being used while leave the WiFi and bluetooth enabled. This costs power. In the Settings app, the WiFi and bluetooth can be disabled totally. But the steps are tedious. How to rurn off WiFi and bluetooth easily in
Read more
How to improve video rendering quality in MPlayer
Posted onMPlayer has lots options for video rendering and filtering. Any suggestions on good MPlayer options that improve video rendering quality nicely? My ~/.mplayer/config is as follows with 2 profiles: [fast] vf=eq2 [default] vf=hqdn3d vo=gl:yuv=3:lscale=5:cscale=5 ao=pulse The default one gives good video quality by using the hqdn3d video filter while the CPU usage is high. If
Read more
How to configure /dev/shm size of Linux?
Posted on/dev/shm is a nice in memory disk on Linux. The default size seems half of the physical memory’s size. How to configure shm size of Linux? And what’s the consequence? To change the configuration for /dev/shm, add one line to /etc/fstab as follows. tmpfs /dev/shm tmpfs defaults,size=8g 0 0 Here, the /dev/shm size is configured
Read more
How to produce a patch file for a specific git commit?
Posted onHow to produce a patch file for a specific git commit so that the changes in that commit can be emailed/sent? git show 550a38b52 generate a quite close content while it is not exactly a patch. You can use this command git format-patch -1 550a38b52 It will generate a file like 0001-<commit-title>.patch in the current
Read more
How to exclude a package from a specific repository only in yum?
Posted onThis post https://www.systutorials.com/1661/making-dnf-yum-not-update-certain-packages/ introduces how to exclude a package from yum. But is it possible to exclude a package from a specific repository only? For example, a repository R1 I am using contains an updated version of gdb while I don’t want to use the gdb from it as I trust the version (although older)
Read more
How to clean the commit history of a repository?
Posted onHow to clean the commit history of a repository in both of my locally cloned copy and the copy on the git server so that only the files after the last commit are left? git works in branches. Here, we assume removing the history of the master branch. One solution is doing as follows. Rename
Read more
How to escape the XML CData section ending sequence `]]>`?
Posted onHow to escape the XML CData section ending sequence ]]>? The ]]> sequence itself may be part of the content in a CData section. One solution is to split the ]]> token into 2 CData sections. <![CDATA[to split the ]]]]><![CDATA[> token ]]>
How to resize a virtual disk of KVM
Posted onI test it for qcow2 format. Other formats are TBA. qemu-img resize kvm1.qcow2 +20G cp kvm1.qcow2 kvm1-orig.qcow2 virt-resize –expand /dev/sda1 kvm1-orig.qcow2 kvm1.qcow2 Reference: https://fatmin.com/2016/12/20/how-to-resize-a-qcow2-image-and-filesystem-with-virt-resize/ I test it for qcow2 format. Other formats are TBA. qemu-img resize kvm1.qcow2 +20G cp kvm1.qcow2 kvm1-orig.qcow2 virt-resize –expand /dev/sda1 kvm1-orig.qcow2 kvm1.qcow2 Reference: https://fatmin.com/2016/12/20/how-to-resize-a-qcow2-image-and-filesystem-with-virt-resize/
How to create a file if not exist and open it in read and write modes in C++?
Posted onHow to create a file if not exist and open it in read and write modes in C++? For example, I would like open a fstream on /tmp/cache to be able to read it and append to it. If the file does not exist yet, create one. A simple code like std::fstream fs(“/tmp/cache”, std::ios::in |
Read more
How to not use concrete types in lambda function parameters in C++11?
Posted onC++11 requires that lambda function parameters be declared with concrete types. This is sometimes annoying. auto is really nice, especially when the type is complex like std::vector<std::string>::iterator is quite long to type. I know C++14 allows auto in lambda functions. But how to not use concrete types in lambda function parameters in C++11? In C++11,
Read more
Checking Whether a String Starts with Another String in C++
Posted onIn many text processing tasks, we often need to check if a given string starts with a specific substring. In this article, we will demonstrate how to achieve this using the std::string::compare() function from the C++ Standard Library. The compare() function has several overloads, but the one of interest for our purpose is: int compare(size_type
Read more
How to generate a password in Shell on Linux?
Posted onAdmins usually need to generate some passwords for others, such as when creating a new user in a Linux system. How to generate a password in Shell on Linux? Several possible and short methods. Here, we generate a password of length 8. Using pwgen: $ pwgen 8 1 dao3PaW9 Password based on base64 (note that
Read more
`readlink -m` equivalent function in Python to get canonical file name
Posted onreadlink -m can get canonical file name by resolving every symlinks in every component of the given path recursively. In Python, the os.readlink() function does not do so. Any equivalent function in Python to the readlink -m command line? Specifically, it does: canonicalize by following every symlink in every component of the given name recursively,
Read more
How to operator[] access element in a const map in C++?
Posted onHow to operator[] access element in a const map in C++? For example, the compiler will report error on this piece of code: #include <iostream> #include <string> #include <map> std::int64_t count(const std::map<std::string, std::int64_t>& map) { return map[“one”] + map[“two”]; } int main () { std::map<std::string, std::int64_t> map = { {“one”, 1}, {“two”, 2} }; std::cout
Read more
How to make Vim indent C++11 lambdas correctly?
Posted onVim seems not indent C++11 lambas very well. How to make Vim indent C++11 lambdas correctly? For this following program, Vim indents it as #include <iostream> #include <string> #include <vector> #include <algorithm> int main () { std::vector<std::string> strs({“one”, “two”}); std::vector<std::string> newstrs; std::transform(strs.begin(), strs.end(), std::back_inserter(newstrs), [](const std::string& s) -> std::string { if (s == “one”) {
Read more