1

I would like to add a bookmark in nautilus to a sftp server whose ip address changes over time.

I am connecting through sftp to a (google cloud instance) server, inserting in the file explorer the address:

sftp://username@xx.xxx.xx.xx/home/

where xx.xxx.xx.xx is an IP address. I would like to add this as a bookmark in Nautilus, which I can easily do with ctrl +d. However, the ip address keeps changing from day to day. I can retrieve the IP address with a command like:

gcloud compute instances describe instancename --format="get(networkInterfaces[0].accessConfigs[0].natIP)"

Is there any way I can have a dynamic Nautilus bookmark, that would use the command above to create the correct address? Or can I run a command myself to update a static nautilus bookmark?

Thanks!

1

You can add a fake domain pointing to the ip.

Edit /etc/hosts and add

xx.xxx.xx.xx    my_foo_domain

To access then you just insert sftp://your_user@my_foo_domain

This way you don't have to create a shortcut everytime, but you have to edit /etc/hosts everytime. You can create a command to refresh the ip, like (backup your /etc/hosts):

sed -i -E 's/^[.0-9]*[[:blank:]](my_foo_domain)/<your new ip> \1/' /etc/hosts

You can create a function placed in your .bahsrc:

_renew_ip() {
  ip=$(gcloud compute instances describe instancename --format="get(networkInterfaces[0].accessConfigs[0].natIP)")
  sed -i -E "s/^[.0-9]*[[:blank:]]*(my_foo_domain)/$ip \1/" /etc/hosts
}

alias renew_ip='bash -c "$(declare -f _renew_ip); _renew_ip"' 
alias sudo='sudo '

And then you can just call the function:

sudo renew_ip
  • Nice!! How can I plug-in the IP address given by gcloud compute ... into the <your new ip> in the sed call? And also, for the sake of completeness, maybe could mention in post: 1) one would use my_foo_domain in the sftp call, right? 2) editing /etc/hosts requires sudo ? Thanks a lot!! – Matifou Nov 26 '19 at 20:12
  • Editing /etc/hosts requires sudo, yes – guillermo chamorro Nov 26 '19 at 20:30
  • Does the command you mention give a clean ip, no other text? – guillermo chamorro Nov 26 '19 at 20:34
  • Wait, I have to solve the permissions issue for the function to work properly. – guillermo chamorro Nov 26 '19 at 20:55
  • oh wow, niiice! I had to edit the quotation marks as there are outer and inner ones? But when I run the code, it does not execute the ip line, just pates it? I mean I get the line gcloud compute instances... copied in/etc/hosts. I believe the command I use returns just the clean IP, at least it pritns it in the terminal, see description: cloud.google.com/sdk/gcloud/reference/compute/instances/… Thank you so much!! – Matifou Nov 26 '19 at 22:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.