SFTP files to vSphere vcenter Appliance 6.5 or vSphere PSC

When logging in with SSH the default shell on these platforms is some vmware specific menu based system. In order to get a real shell you need to type shell. But this is not possible when using sftp, therefore you need to tell your tool to use a real shell.

In WinSCP this is done by configuring the advanced connection parameters for the sftp protocol connection. Press the advanced button and head for the sftp paragraph. In here you need to specify the sftp-server as your shell in the SFTP server text box.
The sftp-server path may change depending on specific version, find the path on your system by running: “find / -name sftp-server”.

On my system the sftp-server was in /usr/libexec/sftp-server. Therefore i had to specify “shell /usr/libexec/sftp-server” as the SFTP-server.

 

Portable headless Linux box

I have a Raspberry Pi that I’m using in different environments. The easiest way to use the Pi is by just connecting power and network and use SSH, but when theres no screen, it’s not always easy to know the IP address of my Pi.

To make life easier for myself i have put the following lines in a start.sh script and added it to my crontab with the @reboot option. This will make crontab run this script every time the Pi has been rebooted (or shut down)

#!/bin/sh
sleep 30
echo “My IP is: $(ip addr | grep global | awk -F ‘ ‘ ‘{print $2}’)” | mail -s “Raspi reporting in…” someone@somewhere.org

The script will put in a 30 seconds delay to allow the Pi to pickup an address and then mail it for my mail address. Remember to make sure that your Pi is able to send mails, otherwise this will not work.

This will most likely ensure that i will get a mail with the IP address of my Pi, and I  am ready to login with SSH.

 

Stop Windows processes remotely

Some customer called saying that they couldn’t log in to a certain Windows box. vMware told me the VM was using 100% CPU, and it was to busy doing something to let users log in.
Powershell saved the day (or at least saved the VM from beeing reset)
To find the process running wild I used the invoke-command cmdlet, like this:

invoke-command -scriptblok(get-process | sort-property CPU -Descending | select -first 10) -computername “Windows Hostname” | ft -autosize

This gave me a nice list of processes with the one we should focus on in the top. Now all i needed to do was:

invoke-command -scriptblok(stop-process -Id “pid”) -computername “Windows Hostname”

The process was stopped, and CPU usage fell to 0-2% allowing application people to login and investigate what happened.

Check for open outgoing ports

Want to know which ports are allowed (or open) to use in your organization?
You can check all ports with http://portquiz.net/
In bash somthing like this would help you get going:

for i in `seq 1024 65535`; do if nc -z portquiz.net $i; then echo “Port $i Success”; :; fi done

You will now get a fine list of outgoing ports that are open. Remember that just because some port is open, it doesn’t necessarily mean that it is allowed to use it!

 

Test network connection with powershell

Sometimes you need to test the network connection for a range of IP addresses. Normally I would use ping from a commandline, but it could be a hugh task to test more than 20-30 addresses.
Utilizing a bit of powershell and the test-connection command will help us overcome the task in an easy way:

[code language=”powershell”]1..100 | %{write-host -nonewline "Testing 10.0.0.$_ : "; Test-Connection 10.0.0.$_ -quiet -Count 1}[/code]

The above one-liner will test the connection to all hosts in a range from 10.0.0.1 to 10.0.0.100 and giva a False or True status.
Remember that % is a short way of using the foreach loop, so you could use “foreach” instead of %.

Playing with LXD containers

Lately I have been playing around with LXD containers, and its actually pretty cool.

Lets create a new container:

lxc-create -t download -n TestCont

This will download the default Ubuntu image from the official repository and create a container named TestCont.

Fire it up with “lxc-start -n TestCont”, connect to it with “lxc-attach -n TestCont”. Now you are in the shell of your new container.

Install dnsmasq, configure it for DHCP and DNS and assign IP’s to your containers based on Name or MAC address. The entry in the dnsmasq conf file should look something like this:

dhcp-host=TestCont,10.0.0.205

Lets create 10 new containers, have them set to autostart, fire them up, and watch the process go along:

for i in `seq 1 10`; do lxc-copy -n TestCont -N TestCont$i;echo “lxc.start.auto = 1” | tee -a .local/share/lxc/TestCont$i/config;lxc-start -n TestCont$i; done; watch -n 5 lxc-ls -f

Maybe we can also configure the dnsmasq at the same time:

for i in `seq 1 10`; do echo “dhcp-host=Cont$i,10.0.0.$i” | tee -a /etc/dnsmasq.d/static-hosts.conf;lxc-copy -n TestCont -N TestCont$i;echo “lxc.start.auto = 1” | tee -a .local/share/lxc/TestCont$i/config;lxc-start -n TestCont$i; done; watch -n 5 lxc-ls -f

As you can see my dnsmasq conf file is called static-hosts.conf. Now i got 10 new containers with fixed IP addresses in 5 minutes – thats cool 🙂

Before copying the original container I have some tips on what to install in it:

sshd
vim
bash-completion

You could do it like this:

lxc-execute -n TextCont apt install ssh vim bash-completion -y

Now you got a nice base image with ssh for remote access, vim for file editing and bash completion. This image will have a ~420 MB footprint.
Another option is to put your public SSH key in the base image, now where getting somewhere 🙂

 

 

Push adbkey to device.

When flashing mobile devices with custom ROMs, I have often experienced that the initial screen resolution when booting the new image the first time is terrible wrong. When this happens I usually change the screen resolution with the following commands (this is for my Xiaomi Mi Note Pro, find your setting on www.gsmarena.com or another phone site)

adb shell wm size 1440×2560
adb shell wm density 560

This will fix the resolution, but i still have bad resolution in the boot process, I haven’t figured out how to change that yet. If you know please leave a comment.

Sometimes the resolution is so bad that i cant even push the accept button on the USB debug prompt, this means that I cannot use adb to change the screen resolution. If this happens you have to reboot into the recovery (I prefer TWRP) and copy your adbkey.pub to the /data/misc/adb/adb_keys file. You can do it like this:

adb push adbkey.pub /data/misc/adb/adb_keys

Reboot your device and you are now able to use adb to change the screen resolution.

Kill unresponsive VM from ESXi cli

Sometimes a VM can go in an unresponsive mode and you cannot shut it down or reboot it from the vSphere client. When this happens we need to be more persuasive in telling the VM to shut down. Log in to the ESXi server with SSH, find the World ID for the unresponsive VM and kill the process, this is done like this:

esxcli vm process list

This will list all the running VMs on the server, use grep -A2 to filter the VM name and the world ID, like this:

esxcli vm process list |  grep -A2 “VM Name”

Kill the process with this command:

esxcli vm process kill -t=soft -w=”WORLD_ID”

This will kill the process in a “soft” way, use -t=hard to be even more persuasive and as a last resort attempt you can use -t=force. If none of the three shuts down the VM, a reboot of the host is required.
To automate things a bit i assembled this one-liner for easy use:

esxcli vm process kill -t=soft -w=`esxcli vm process list | grep -A2 “VM Name” | grep World | awk ‘{print $3}’`

This will softly shut down the VM with “VM Name”

Add new virtualportgroup to vSwitch on multiple VMHosts with Powercli

If you dont have dvSwitches (Distributed vSwitches) in your vSphere cluster, or dont even have a cluster, you may have to add new portgroups manually, depending on the number of VMHosts this can be a pretty cumbersome task.

Luckily, we can use powercli to automate the task. In the following example i will use the -location parameter to define my tagets in the variable $hosts

Lets go ahead and define the VMhosts we want to target:

$hosts=get-vmhost -location “location”

Then, we create the new portgroup for every VMhost in the location by using a foreach loop:

foreach ($vmhost in $hosts) {Get-VMHost $vmhost | Get-VirtualSwitch -name “vSwitch0” | New-VirtualPortGroup -VLanId “10” -Name “Name”}

This will create a new VirtualPortGroup with VLAN ID 10 named “Name” on vSwitch0 on all the VMHosts in the specified location.

Remove the port again by doing this:

foreach ($vmhost in $hosts) {Get-VMHost $vmhost | Get-VirtualSwitch -name “vSwitch0” | Get-VirtualPortGroup -Name “Name” | Remove-VirtualPortGroup -Confirm:$false}

Remember that -Confirm:$false will remove the port without confirmation! Make sure you have the right targets in the $hosts variable!!!