Bash script to make ssh becomes easier - bash

I'm managing hundreds of network devices, and when i want to ssh to a device, I need to type in my jump server "ssh -l $user $ip/dnsname" in terminal.
I have an idea that's write a bash script and run it as a service. When I want to ssh to any device, I just need to type the IP address or DNS device name in the terminal and hit enter. It will execute the ssh command automatically.
But i'm new in bash, could you guys please give me an instruction?

Related

ssh concatenating keyboard characters

I am sshing into a mac running zsh but each character that I type is remembered and repeated. For example, if it type exit, I see eexexiexit on the screen but the command exit is executed.
If I type directly on the remote machine, there is not a problem so it is the communication via ssh that is the issue. I have no idea how to solve. Any suggestions?
I realised that I just needed to ssh into the remove server with the following command: 'TERM=screen-256color ssh ...

SSH Unknown TTY Error Message

When trying to run the command ssh user#ip 'command', I keep receiving this error: can't get tty settingscan't set orig mode. I have tried googling this and searching here but have not found anything referring to this message. I am trying to automate connecting to a Cisco wireless access point (2800 model) using Golang to run a command then exit, but this message pops up each time. I can log in normally using ssh user#ap_ip, but using the shorthand ssh user#ap_ip 'command' or trying the equivalent in Golang gives the error message above. Anyone know what's going on?
ssh without explicit command given will allocate a terminal while ssh with a command given will by default not. It looks like the command you execute needs a terminal. In this case use the -t option, i.e. ssh -t user#ip command. From the documentation:
-t Force pseudo-terminal allocation. This can be used to execute
arbitrary screen-based programs on a remote machine, which can be
very useful, e.g. when implementing menu services. Multiple -t
options force tty allocation, even if ssh has no local tty.

Run Command on Multiple Macs from Server

I am trying to force shutdown multiple mac computers every night which are all connected to a server. I am unsure if the best way to do this is by running a sudo shutdown command through a for loop using IP addresses or ssh'ing. Or any other method. Any advice would be appreciated!
I don't know any better method than ssh.
Generate and install your ssh key on those macs in the root account, in the file /var/root/.ssh/authorized_keys2 of each of them,
Ensure each of your mac has the line "PermitRootLogin yes" uncommented in the file /etc/ssh/sshd_config, if not change it and relaunch sshd.
And finaly use ssh to run the shutdown command.
Here is the command line in bash shell :
for host in host01 host02 host03; do ssh root#$host "shutdown -h"; done

Vagrant: How can you run scripts on the host via commands in the guest shell?

It's possible to open ports, network files, and there are plug-ins that allow for running guest or host [shell] commands during Vagrant's Provisioning process.
What I'd like to do is be able to (perhaps through a bash alias) run a command in the Vagrant guest/VM, and have this execute a command on the host, ideally with a variable being passed on the command line.
Example: In my host I run the Atom editor (same applies to TextMate, whatever). If I want to work on a shared file in the VM, I have to manually open that file from over in the host, either by opening it directly in the editor, or running the 'atom filename' shell command.
I want parity, so while inside the VM, I can run 'atom filename', and this will pass the filename to the 'atom $1' script outside of the VM, in the host, and open it in my host editor (Atom).
Note: We use Salt for Vagrant Provisioning, and NFS for mounting, for what it's worth. And of course, ssh with key.
Bonus question: Making this work with .gitconfig as its merge conflict editor (should just work, if the former is possible, right?).
This is a very interesting use case that I haven't heard before. There isn't a native method of handling this in Vagrant, but this functionality was added to Packer in the form of a local shell provisioner. You could open a GitHub issue on the Vagrant project and propose the same feature. Double check the current list of issues, though, because it's possible someone has beaten you to it.
In the meantime, though, you do have a workaround if you're determined to do this...
Create an ssh key pair on your host.
Use Salt to add the private key in /home/vagrant/.ssh on the box.
Use a shell provisioner to run remote ssh commands on the host from the guest.
These commands would take the form of...
ssh username#192.168.0.1 "ls -l ~"
In my experience, the 192.168.0.1 IP always points back to the host, but your mileage may vary. I'm not a networking expert by any means.
I hope this works for you and I think a local shell provisioner for Vagrant would be a reasonable feature.

Injecting bash prompt to remote host via ssh

I have a fancy prompt working well on my local machine. However, I'm logging to multiple machines, on different accounts via ssh. I would love to have my prompt synchronized everywhere by ssh command itself.
Any idea how to get that? In many cases I'm accessing machines using root account and I can't change permanently any settings there. I want the prompt synchronized.
In principle this is just setting variable PS1.
Try this :
ssh -l root host -t "bash --rcfile /path/to/special/bashrc"
maybe /path/to/special/bashrc can be /tmp/myrc by example

Resources